Create a dynamic Twitter Signature with PHP.
Today, I’m going to show you how to use PHP and it’s GD library to create a dynamic signature that you can use on forums, blogs, etc, that contains your latest tweet.
This will be a simple script, you can adjust it yourself later.
To begin, you’ll need a background image, we’ll use this one:
Now, we’ll create mysig.php:
First, let’s go over the initial script:
$name = $_GET['name'];
$srcImage = "./twit.png";
$im = imagecreatefrompng($srcImage);
$res = tweetit($name,1);
$string = $res[2].": ".$res[0];
$timg = $res[1];
if( !empty($timg) ){
$timg = copyImg($timg);
if( stristr($timg,".jpg") ){
$stamp = imagecreatefromjpeg($timg);
}else if( stristr($timg,".png") ){
$stamp = imagecreatefrompng($timg);
}
$marge_right = 2;
$marge_bottom = 2;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
}
$black = @imagecolorallocate ($im, 0, 0, 0);
$white = @imagecolorallocate ($im, 255, 255, 255);
$width = imagesx($im);
$height = imagesy($im);
$color = imagecolorallocate($im, 100, 100, 100);
ImageStringWrap($im, 3, 5, 2, $string, $black,320);
header("Content-type: image/png");
imagepng($im);
exit;
So, there are a few steps involved here, we still need to create some functions.
The first function, is a function called tweetit, which returns the latest tweet:
function tweetit($user,$cnt = 1){
$curl = curl_init('http://twitter.com/statuses/user_timeline/' . $user . '.xml?count='.$cnt);
curl_setopt($curl,CURLOPT_HEADER,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$twi = curl_exec($curl);
curl_close($curl);
$tweeters = new SimpleXMLElement($twi);
$latesttweets = count($tweeters);
foreach ($tweeters->status as $twit1) {
$text = (string)$twit1->text;
$img = (string)$twit1->user->profile_image_url;
$name = (string)$twit1->user->screen_name;
break;
}
return array( $text,$img,$name);
}
Next, we have to copy the avatar for your twitter account, this will help save on bandwidth later, and also helps cut down on trying to manipulate remotely hosted images:
function copyImg($url){
$fname = explode("/",$url);
$fname = end($fname);
$fname = "avas/".$fname;
if( !file_exists($fname) ){
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_HEADER,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$twi = curl_exec($curl);
curl_close($curl);
$fp = fopen($fname, 'w');
fwrite($fp, $twi);
fclose($fp);
}
return $fname;
}
The last function, is our custom ImageStringWrap function, this function is used to split your tweet into multiple lines based on image width.
function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth){
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, $x, $y, $line, $color);
$y += $fontheight;
}
}
Now, let’s put it all together:
$name = $_GET['name'];
$srcImage = "./twit.png";
$im = imagecreatefrompng($srcImage);
$res = tweetit($name,1);
$string = $res[2].": ".$res[0];
$timg = $res[1];
if( !empty($timg) ){
$timg = copyImg($timg);
if( stristr($timg,".jpg") ){
$stamp = imagecreatefromjpeg($timg);
}else if( stristr($timg,".png") ){
$stamp = imagecreatefrompng($timg);
}
$marge_right = 2;
$marge_bottom = 2;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
}
$black = @imagecolorallocate ($im, 0, 0, 0);
$white = @imagecolorallocate ($im, 255, 255, 255);
$width = imagesx($im);
$height = imagesy($im);
$color = imagecolorallocate($im, 100, 100, 100);
ImageStringWrap($im, 3, 5, 2, $string, $black,320);
header("Content-type: image/png");
imagepng($im);
exit;
function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth){
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, $x, $y, $line, $color);
$y += $fontheight;
}
}
function tweetit($user,$cnt = 1){
$curl = curl_init('http://twitter.com/statuses/user_timeline/' . $user . '.xml?count='.$cnt);
curl_setopt($curl,CURLOPT_HEADER,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$twi = curl_exec($curl);
curl_close($curl);
$tweeters = new SimpleXMLElement($twi);
$latesttweets = count($tweeters);
foreach ($tweeters->status as $twit1) {
$text = (string)$twit1->text;
$img = (string)$twit1->user->profile_image_url;
$name = (string)$twit1->user->screen_name;
break;
}
return array( $text,$img,$name);
}
function copyImg($url){
$fname = explode("/",$url);
$fname = end($fname);
$fname = "avas/".$fname;
if( !file_exists($fname) ){
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_HEADER,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$twi = curl_exec($curl);
curl_close($curl);
$fp = fopen($fname, 'w');
fwrite($fp, $twi);
fclose($fp);
}
return $fname;
}
This is how the script works, you call it as follows:
<img src="mysig.php?name=freekrai" alt="" />
And you get this:
Pretty cool huh?
When you upload this to your site, your going to want to create a folder called “avas“, and make sure it is writable by setting it to 755 or 777, this will let you store your twitter avatars locally using the copyImg function.









Andi March 27th
Any help diagnosing this error?
Fatal error: Call to undefined function curl_init() in /home/andi/web/the-corrado.net/twitter/mysig.php on line 45
Located here: http://the-corrado.net/twitter/mysig.php
so e.g. http://the-corrado.net/twitter/mysig.php?name=freekrai
I run Lighttpd instead of Apache, which does support cURL, but perhaps I need to tweak the code to get it to work correctly? Bit out of my depth here but I was wondering if this was relevant:
http://ultrahigh.org/2008/11/06/weird-problems-with-curl-d-and-lighttpd/
Roger Stringer March 27th
actually, that sounds more like your PHP doesn’t have curl installed, which doesn’t have anything to do with being in Apache or Lighthttpd.
Curl is the standard accepted method to use these days for connecting with other web sites.
I’d check your PHP install using phpinfo() to see if you have curl installed or not with php.
Add Yours
YOU