Login

Dynamic Twitter Signature Images with PHP

Twitter is the latest and greatest craze in social networking sites. As such, many developers and businesses are using it as a marketing technique. Today we’re going to come up with a method for creating a dynamic signature image for forum posts and emails that will automatically load your Twitter feed and display your latest Twitter status. We’ll be using PHP to grab the content along with GD2 support to create the image.

I’m going to be presenting the code in this article in an object-oriented fashion using PHP 5. If you’re not already using object-oriented programming techniques it’s important that you begin doing that. You will be able to take advantage of more advanced features in PHP while building more robust applications. In my opinion, it’s one of the subtle differences between a true programmer and a simple scripter.

The first step in creating this project was determining what size to make the final image. I considered the standard IAB ad unit sizes for banners, but they were either too big or too small for our purposes. The 60 pixel height on a full banner (also called impact banner) was perfect, but a width of 480 pixels takes up a bit more real estate than I would like for a low profile signature image. The half banner cuts that width in half, but then doesn’t allow much room for both a user’s avatar and text.

So I decided to shy away from a standard ad unit and compromise between the two, settling on a width of 380 pixels and a height of 60 pixels. With this size a user’s avatar fits nicely inside the height and still allows about 300 pixels in width for the addition of text. With the capacity to house four lines, this fits the Twitter status’ character length specification pretty nicely, as you can see in the live example below.

Once I decided on a unit size for the image, I needed to build a background image that was the proper size. This is a simple background that will provide a nice- looking backdrop for the image as opposed to having it over a single background color. You can use Gimp or any other image creation software to create your own, or download the one I created in Photoshop here.

{mospagebreak title=Building the class}

With all of the basics out of the way, it’s time to dive into some PHP and begin building a class. We’re going to create a class called TwitterSignature. This class will load the background and user feed, create the signature image, and then output it to the browser as an image. This will allow us to simply point the HTML source of an image to this PHP file and have it render in any page, as you’ll see later.

class SignatureImage

{

private $screen_name;

private $profile_image;

private $status_text;

private $local_avatar;

public function __construct($name, $bg_image, $adir)

{

}

}

Here’s the basic structure of the entire class. We first create a few class-level variables that we will use as we start adding functions. $screen_name, $profile_image, $status_text, and $local_avatar are all strings that will contain the Twitter screen name, URL to the Twitter avatar, the XML status feed, and local avatar directory path, respectively. I’ll explain each of these in more detail as we go along.

Notice also that I’m using a constructor for this class. All of the class’s functions will be called internally through this constructor. There won’t be any need to call functions when the class is actually implemented.

The constructor accepts three required parameters: the Twitter screen name to process, the location of the background image, and the local directory where avatars are/will be stored. The Twitter screen name is the most important, as we’ll need that to access the Twitter feed.

Twitter provides an API with which you can access a user’s status feed in XML format. This is available from a public access URL in the form of:

http://twitter.com/statuses/user_timeline/screenname.xml?count=1

Replace screenname with the user’s Twitter screen name. Adding the query string “count=1” forces the API to return only the latest status message, slimming down the request a bit. Since we’re only interested in the latest status, it shortens both the response time and the request length, which increases the performance of our script. A user feed looks something like this:

The first programming step to creating our image is pretty obvious. We need to retrieve the Twitter feed and parse the information that we need. To do that, we’ll create a function called fetchUserInfo. We’ll also create a function called curlRequest that will act as a wrapper function for the CURL process that will retrieve the external site content.

This function is much simpler than it looks initially. It accepts a string containing the Twitter screen name. That name is used to create a feed URL as described earlier, and then a request is made using $this->curlRequest (to be created) that returns the XML contents of the Twitter feed. Next, we use the SimpleXMLElement class to parse the XML content and assign the information we need to class-level variables. Of course, this is all of little worth if we don’t add the curlRequest function that performs the request for the feed.

If you’ve used CURL in the past, this function will look pretty basic. First it checks that CURL is actually available, and then makes a request to the URL provided. It returns the response from that request or a Boolean false if there’s an error.

Throughout these code samples, I’ve added error checking but left only comments as to the error. We’ll be going back later to add proper error handling.

Now we can take advantage of our function by adding a call to $this->fetchUserInfo to the class’s constructor. While we’re at it, we’ll add a call to $this->fetchAvatar, which is the function we’ll be creating next to load the user’s avatar image. The constructor now looks like this.

public function __construct($name, $bg_image, $adir)

{

$this->fetchUserInfo(strtolower($name));

$this->fetchAvatar($this->profile_image, $adir);

}

You may have also noticed that I’m using the strtolower function to force the user name to be lower-case. This aids in feed caching, which is a feature we will be adding later. For now, it just adds uniformity when creating the feed URLs.

{mospagebreak title=Grabbing the avatar image}

One of the details we were able to parse from the XML feed was the location of the user’s Twitter avatar. We’re going to display this on our signature image, so we’ll need to make another CURL request to grab the image and store a copy of it locally.

private function fetchAvatar($url, $adir)

{

$fname = end(explode(‘/’, $url));

$adir = preg_match(‘#^(.*?)/$#i’, $url) ? $adir : "{$adir}/";

$fname = $adir . $fname;

if ( !file_exists($fname) ) {

$img = $this->curlRequest($url);

$fp = fopen($fname, ‘w’);

fwrite($fp, $img);

fclose($fp);

}

$this->local_avatar = $fname;

}

This function is pretty basic. It uses the curlRequest function to retrieve the avatar image and write it to a local file. You’ll see a couple of really nifty PHP techniques employed in the first two lines.

The first technique is a quick method of parsing the image file name from a URL. You’ll recall that URLs have several parts, all delimited by a forward slash. PHP’s explode function can create an array from a string based on a string delimiter. This very quickly creates an array of each of the URL parts. The end function then returns the last element in the array (being the file name portion of the URL). This is a quick and dirty technique for returning the end portion of a delimited string.

The second technique combines a regular expression match with the ternary operator. It checks the avatar directory string for a trailing slash and returns either the path if it includes one or the path with a slash appended to it. This nice one-liner ensures that our path always contains a trailing slash.

Recapping

Our Twitter SignatureImage class is nearing completion. We’ve added all of the functions to retrieve a user’s Twitter feed information and copy the user’s avatar image. In the second part of this article series we’ll take a look at building and rendering the actual signature image.

We’re also going to look at adding appropriate error handling and a file caching feature. The latter is important since Twitter limits the number of API requests a single IP may request in a specific amount of time. Caching will store a copy of the XML feed locally to prevent making too many requests to the API service.

I’ll also be demonstrating a quick method of tracking how many impressions your image gets which is useful if you’re using your image in forum signature. And of course, I’ll show you how to actually implement the SignatureImage class. Until next time, keep coding!