Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Updating the Page Cover Image Programmatically

Facebook API: Updating the Page Cover Image Programmatically

25

If you want to update the cover image for your Facebook Page automatically, you can do so using the Facebook PHP SDK. This tutorial shows you how to upload a photo to your Page and then set it as the cover image. You can use this method to automatically rotate between different cover images or promote your business or brand easily.

You’ll need to know your Page Access Token in order to update the cover image. You can find out how to obtain this using this tutorial. Next, you’ll need a cover image we can upload to the page (minimum width is 399px, but recommend size is 851px wide × 315px high) – in my code, I’ve named the image cover.png.

Uploading the Photo

The first step is uploading the photo to the Facebook Page. We do this by first enabling upload support in the PHP SDK (line 10 in code), and setting the page access token (line 13). Setting the access token in this way means all subsequent calls will use this access token (alternatively, you can pass the access_token as a parameter to each call manually).

We upload the photo by making a POST API call to {PAGE_ID}/photos (lines 16-19), passing in the path to the image as source. As my image is in the same directory as the script, I am passing a relative path prefixed with @ (required by Facebook).

If the image has uploaded successfully, Facebook will return an array with the ID of the photo. We’ll need this ID to set the cover image. When a photo is uploaded to a page, an automatic news store is created to inform fans. You can stop Facebook from creating this story by adding the optional no_story parameter to your API call (line 18).

Setting the Cover Image

Once we know the ID of the photo we just uploaded, we need to make a second API call to set the actual cover image, and optionally, its position if it’s larger than 851px width or 315px height. For this, we make a POST API call to {PAGE_ID}, passing in the ID of the uploaded image as cover (lines 22-27).

To prevent Facebook from creating a Cover Image news story on the page, you can pass in the optional no_feed_story parameter. If you plan on changing the cover image often it’s a good idea to disable the functionality to stop spamming your fans.

If the cover image has been changed successfully, the Facebook API will return true, indicating success. You can double-check by visiting your page on Facebook and making sure the new cover image is live.

If you previously uploaded a cover image, you can just change it to that one by using the same API call, but by passing in a different image ID. You can then easily rotate between a number of images by just changing the IDs.

Code

Below is the full code for uploading and setting a photo as the cover image for your Page.

[divider style=”dashed”]

Using Remote Images

If you prefer to upload an image that’s stored remotely to your code (for example, an image hosted by a CDN) you can make a small change to your code to accommodate this. Instead of passing the image using the source parameter, you can use the url parameter:

Facebook, Facebook Graph API, Fan Page, PHP

25 comments on “Facebook API: Updating the Page Cover Image Programmatically”

  1. Harsha M V says:
    March 12, 2014 at 8:19 PM

    Thanks a lot for the reply on SO and a post as well. Would love to connect with you on FB.

    Reply
  2. Alessio Baglioni says:
    April 1, 2014 at 8:22 AM

    Great work! Do you know how to do it also for an event page?

    Reply
  3. markos24ro says:
    June 5, 2014 at 6:05 PM

    I can not make this to work give me an error: Fatal error: Uncaught OAuthException: Invalid OAuth access token. thrown in /public_html/fbcovers/src/base_facebook.php on line 1325

    Reply
    • Niraj Shah says:
      June 5, 2014 at 11:53 PM

      Can’t help you without knowing the code you are using. Please post it on StackOverflow and send me a link.

      Reply
  4. Kasper says:
    August 6, 2014 at 7:28 AM

    Great post! How do I make it change between images automatically?

    Reply
    • Niraj Shah says:
      August 6, 2014 at 10:01 AM

      Change the script to select a random cover image to use on the page, and then use a cronjob to call the script to update the cover image periodically.

      Reply
  5. Awi Feltes says:
    September 26, 2014 at 7:58 PM

    Hello, I’m trying to use your example.
    The photo gets uploaded, but when I try to set it as cover I get this error:

    [Fri Sep 26 14:55:14 2014] [error] [client 190.52.181.20] PHP Fatal error: Uncaught OAuthException: (#324) Requires upload filen thrown in /var/www/ciclon/fb3/src/base_facebook.php on line 1325

    Thanks in advance.

    Reply
    • Niraj Shah says:
      September 29, 2014 at 10:24 AM

      Are you using a valid Photo ID when trying to set the cover photo?

      Reply
  6. Kevin says:
    November 6, 2014 at 3:21 PM

    Is it possible to refine this code to select a photo from a album on your facebook rather than uploading? In my case I would like somthing like this to rotate cover images in a group instead of a page.. pulling from a album on my main facebook account.

    Reply
    • Niraj Shah says:
      November 9, 2014 at 6:18 PM

      Yes it’s quite simple to do. Instead of the upload code in lines 15-19, you can replace this with a API call to the Album of your choice (e.g. https://graph.facebook.com/{album_id}/photos ) and then modify the ID on line 23 to point to valid image from this album.

      Reply
  7. Jeremy Siow says:
    June 26, 2015 at 9:23 AM

    thanks you very much!!

    Reply
  8. SRH says:
    July 2, 2015 at 3:53 PM

    Good Day. Would it be possible to purchase code and step by step setup to schedule changes to FB group cover photos.

    Reply
    • Niraj Shah says:
      September 4, 2015 at 7:19 PM

      Please email me with your requirements and budget. Include details about your environment, customisable settings you wish to include and how the images will be uploaded / sourced.

      Reply
  9. Alvaro says:
    July 14, 2015 at 8:37 AM

    Is it possible to adapt this code to change user profile image?

    Reply
    • Niraj Shah says:
      July 14, 2015 at 9:15 AM

      It is not possible to change the user’s profile image via the API – this can only be changed by the user (to prevent abuse).

      Reply
  10. Partha says:
    July 16, 2015 at 2:08 PM

    Fatal error: Uncaught GraphMethodException: Invalid appsecret_proof provided in the API argument thrown in /home/cparthadhara/public_html/facebook/fb-app/src/base_facebook.php on line 1325

    Reply
    • Niraj Shah says:
      August 14, 2015 at 7:56 PM

      Posting an error isn’t going to get you any closer to an answer. Please elaborate with a question and your source code.

      Reply
  11. Jakub Kontra says:
    November 3, 2015 at 4:41 PM

    Hi, can I get back cover photo ID? Problem was i need to remove previous one.

    Thanks

    Reply
    • Niraj Shah says:
      November 25, 2015 at 1:02 PM

      Find the Photo ID of the photo you previously used and use the API to revert to this.

      Reply
  12. Deepan says:
    August 29, 2016 at 5:18 PM

    Hi, is it possible to change facebook fan page – cover photo through Graph API?

    Reply
    • Niraj Shah says:
      September 12, 2016 at 10:09 PM

      This tutorial is about changing the Facebook fan page cover photo using the Graph API.

      Reply
  13. ede says:
    July 18, 2017 at 11:35 AM

    What about cover video?

    Reply
    • Niraj Shah says:
      September 26, 2017 at 10:50 AM

      Facebook doesn’t have any information on whether setting videos is possible in this way. From my own experimentation, it doesn’t appear to be possible.

      Reply
  14. jnptrnkls says:
    March 11, 2018 at 10:59 AM

    Can it change page profile picture using this method?

    Reply
    • Niraj Shah says:
      March 19, 2018 at 11:52 AM

      Yes you can. But the process of getting the access token for a user account is different. You will need to follow the instructions for “Login with Facebook”.

      Reply

Leave a Reply to Alessio BaglioniCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy