Updating A Twitter Profile Image With PHP
I had an interesting issue come up recently when working on improvements to a Twitter bot I hadn’t used in a while but wanted to start using again. The new feature I was trying to add was to update the profile picture of the Twitter account the bot used based on a certain criteria determined by the bot script. The criteria was easy to work out but it took me a while to figure out exactly how to execute the change in profile pic.
(I’ve written before about creating a Twitter bot so I won’t repeat all of the details here. Just as in the previous post I’m using PHP with Abraham Williams’s PHP library for Twitter OAuth. The balance of this post assumes you’re already logged into Twitter using this library.)
Twitter’s API includes a command called update_profile_image. On the face of it this seems like a simple enough call to make but for some reason I had some trouble getting it to work. After some research and a whole lot of trial and error here’s how I got it to work.
- In reading the API documentation I learned that the only required parameter is the image. But the image must be base64-encoded. (The same requirements for uploading a profile pic on Twitter’s website also exist here: it must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size.) So the first thing I needed was a function to convert a given image to base64 encoding. I don’t remember where I found it but I turned up the following function to do that. (My apologies to the original writer of this function. If someone points out to me who the writer is I’ll gladly cite them in this space.):
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return base64_encode($imgbinary);
}
} - Once I had this function in place it was time to set up the API call to change the pic. First set a variable to the filename of the image to use:
$image = "profile.png";
In this case the image is called “profile.png” but you can use any PNG, JPG or GIF that fits the criteria mentioned above. - Next set a variable to the base64 value of the image. This is where the base64_encode_image function is called.:
$base64 = base64_encode_image ('images/'.$image,'png');
Note that the image must be located in a place where the script can get to it. In this example the image is in a subdirectory called ‘images’. Change the directory name in the script as needed. - And finally make the API call to change the image on Twitter:
$connection->post('account/update_profile_image', array('image' => $base64.';type=image/png;filename='.$image));
Note that in this case I’m setting the image type to PNG. Change this as needed.
It seems very simple but for some reason the last line took some time to figure out. Just sending the base64 encoded image didn’t work. After some Googling and trial and error I figured out that adding the type and filename parameters was needed.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.