Send up to Four Images and a Message to Twitter with PHP

It was four years ago now I posted “Post to Twitter using OAuth” – an article that described the process of sending a message to Twitter using Abraham Williams’ TwitterOAuth . The article was then updated in 2012 to cater for cosmetic changes in the Twitter Application interface but it’s remained untouched since then – despite consistent traffic. This is the first of new ‘series’ that’ll provide basic code to perform an array of Twitter functions.

You should refer to this article on setting up the necessary application on Twitter.

This article will provide you with a basic function that’ll do what we previously didsend a single message to Twitter – but it’ll also permit you to upload up to four images with your message.

Features of the PHP function are as follows:

  • Send a single message to Twitter.
  • In addition to a message, send up to four images.
  • Images may be local (accessed with a local path) or URL (via http).

Some of the code is written in long-hand because I couldn’t work around a few issues. I’ve written another version that incorporates and this has been sent to subscribers of that service (6 weeks before this post was published). Want stuff first? Like Internoetics on Internoetics .

The Results

In the first example we’ll send four images to Twitter. While not entirely necessary, all remote are stored locally in the default image directory.

The result:

In the second example we’ll send three images; the first two will be accessed via a http request (to another of our websites) and the last image will be referenced by a local path to an alternate directory.

The result:

The remote images will be copied locally (with a new filename) into your default image directory.

Remote images copied to the default image directory

You can obviously send just two images, or one.

Without referencing an image you will send just a status update.

The Code

It’s best to download the zip file below because it includes Abraham Williams’ TwitterOAuth library. You must change the path to autoload.php at the top of your PHP page. You should also alter the default image directory (twice); you’ll have to do it twice because a variation stores remote images elsewhere.

There’s obviously a little bit of messy repetition in the code… but if I work out how to overcome a few issues I encountered while writing it I’ll update it.


The following information is largely sourced from Twitter . Relevant points are reproduced here.

  • Photos can be up to 5MB; animated GIFs can be up to 3MB.
  • Twitter accepts GIF, JPEG, and PNG files. They do not accept BMP or TIFF files.
  • You may include up to 4 photos or 1 animated GIF or 1 video in a Tweet.
  • Your photo will be automatically scaled for display in your expanded Tweet and in your user gallery.
  • You can delete a photo by deleting the Tweet that contains it. Once a Tweet is deleted, the photo will no longer be available from Twitter.
  • Only approved followers will be able to see your photos if your Tweets are protected.
  • Twitter removes the Exif data upon upload. It is not available to those who view your photo on Twitter.
  • Twitter applies API rate limiting that might be restrictive. The code has the option to return an error code if need be.

If you automate your WordPress posts to Twitter you might consider uploading the featured image URL or, perhaps, an image defined by a custom field. While I haven’t done any research (yet) I suspect that an image-based tweet may potentially convert to more traffic – assuming that the image was compelling enough. A tweet is limited to 140-characters with a URL while an image is worth a thousand words, right?

I use a variation of this code to extract details and image location details from a database for an automated account for both Twitter and Facebook – it works well. The full script is forthcoming (via an article available only to subscribers). We’ve also scheduled an email to Twitter script that has similar features.

While I’ve used the inactive Internoetics account for the purpose of examples, my primary Twitter account is at @MartyKhoury .

The Code

Download the twitteroauth library and working example here . Ensure you make the necessary path modifications. If you don’t know how to determine the path refer to this article.

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *