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:
Thanks a lot for the reply on SO and a post as well. Would love to connect with you on FB.
Great work! Do you know how to do it also for an event page?
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
Can’t help you without knowing the code you are using. Please post it on StackOverflow and send me a link.
Great post! How do I make it change between images automatically?
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.
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.
Are you using a valid Photo ID when trying to set the cover photo?
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.
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.thanks you very much!!
Good Day. Would it be possible to purchase code and step by step setup to schedule changes to FB group cover photos.
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.
Is it possible to adapt this code to change user profile image?
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).
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
Posting an error isn’t going to get you any closer to an answer. Please elaborate with a question and your source code.
Hi, can I get back cover photo ID? Problem was i need to remove previous one.
Thanks
Find the Photo ID of the photo you previously used and use the API to revert to this.
Hi, is it possible to change facebook fan page – cover photo through Graph API?
This tutorial is about changing the Facebook fan page cover photo using the Graph API.
What about cover video?
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.
Can it change page profile picture using this method?
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”.