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.

  1. 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);
    }
    }

  2. 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.
  3. 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.
  4. 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.

Tags: , ,

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.

Comments

Man, you are a genius. I’ve been using this library for years, and everybody was talking that Abrahams’ Twitteroauth can’t properly update profile image because of some limitations.

I thought about switching to another library, but it was a big pain in the a** to re-make everything using the new library, so I can’t even tell you how happy I am with your solution.

I am getting error for this:
stdClass Object ( [code] => 123 [message] => There was a problem with your picture. Probably too big )

Can you please help me?

From the Twitter API documentation:

The avatar image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down. Animated GIFs will be converted to a static GIF of the first frame, removing the animation.

Be sure your image meets these specs.

I almost got it, but I can’t figure out what is $connection, due to that I’m using a different way of posting that. What is exactly connection? Thanks!

$connection is the variable representing your connection to the Twitter API.

Leave a comment

(required)

(required)