Posted on May 1, 2007
First, let me begin by apologizing for putting Virb and MySpace in one sentence. That won't happen again.
Last week, I came up with a quick solution to include my Twitter status on my Virb profile. This way, all my info is consolidated nicely within one profile. For now, Virb makes this a bit hard because Javascript cannot be included in profiles and rightfully so!
So, to get around that, I simply decided to have my web server query my Twitter status from time to time (every 10-15 minutes) and if there's a change, create a new image accordingly. The image is then simply plugged into my Virb profile and loaded from my server upon every viewing.
Requirements: PHP 5 (or PHP 4 with a custom JSON module) with both GD library and FreeType compiled with it. Most hosting companies will have both available. If not, ask them. You may not even need FreeType if you decide to use one of few built in fonts (ugly). FreeType is needed to be able to use custom fonts when generating custom images using the GD library.
We first need to get the data from Twitter and they make it easy enough with two options: JSON and XML. Being a Javascript developer, I swear by JSON and since PHP 5 has all the necessary functionality built in to parse a JSON string, let's use that.
$url = 'http://twitter.com/statuses/user_timeline/771362.json'; // Get the JSON URL to your account
$curl_handle = curl_init(); // Open up a CURL connection
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); // Timeout connecting in 2 seconds
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 20); // Timeout getting data in 20 seconds (since Twitter has been slow with growing popularity)
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Get data back
$buffer = curl_exec($curl_handle); // Execute the request and get data back
curl_close($curl_handle); // Close the connection!
if ($buffer) { // If data is returned
$data = json_decode($buffer); // Decode JSON string
$twitter_when = date('n/j/Y g:ia',abs(strtotime($data[0]->created_at . " GMT"))); // When was last tweet posted?
$twitter_text = $data[0]->text; // Last tweet
} else { // Otherwise
die("Couldn't fetch data"); // Error occurred while fetching, most likely a timeout
}
We now have the time of the last tweet and the actual tweet in variables $twitter_when (UNIX timestamp) and $twitter_text respectively.
Note: You need to update $url variable to have your userid (mine is '7713622). It's tricky to find it out on Twitter. I will post an update when I figure out a good way to do so.
We need a way to display this new data on Virb and since no Javascript is allowed, we will generate a new image with this content and then include the image in our Virb profiles.
//$im = imagecreatetruecolor(560, 72); // Uncomment this and delete the next line to create a blank image
$im = imagecreatefrompng("virb.png"); // This creates an image from an existing PNG image
$white = imagecolorallocate($im, 173, 173, 173); // Define some colors to use
$grey = imagecolorallocate($im, 100, 100, 100); // Color for side the date/time
$twitter_text = wordwrap($twitter_text,72); // Because my image is only 560 pixels wide, text can run out of the boundaries and GD library has nothing to remedy that, so this function will wrap the text after 72 characters. This may differ for your image.
$font = 'SomeFont.otf'; // Custom font. You may use one of four or five standard fonts, but they're ugly
imagefttext($im, 10, 0, 10, 20, $white, $font, $twitter_text, array('linespacing'=>1.7)); // Write out to the image Twitter status with appropriate positioning
imagefttext($im, 9, 0, 11, 61, $grey, $font, $twitter_when, array('linespacing'=>1.7)); // Same with date/time of last tweet
imagepng($im,"twitter.png"); // Create twitter.png
imagedestroy($im); // Free up some memory
We now have an image that was generated on the fly by PHP that includes our last tweet. This image can now be safely included anywhere you'd like, even your website.
I didn't go into describing the functionality of each function and its parameters, so make sure you look them up on php.net for better reference.
Instead of refreshing the PHP file yourself every time you update your Twitter status, simply setup a cron job on your web server. I have mine setup to run the PHP script every 10 minutes.
Also, to save some server resources and not update the image if Twitter status hasn't changed, I also modified the code a bit to save the latest tweet in a text file. If upon PHP file execution Twitter status hasn't changed, it won't waste resources and generate a new image, but rather simply stops the execution.
My cron job looks like this (the location of PHP may be different on your web server):
/usr/bin/php5 -q /home/10416/domains/arthero.com/html/twitter_update.php
Download the final file below (and rename to .php):
» Twitter status into image
Look at the final result:
» Final image included in Virb
» Template image used to create it
Note: The code won't work right away unless you specify a custom font and change the function to use default font!
Loading comments...