Show the world your Twitter type (using PHP and Google Charts)

Sunday, November 23rd, 2008 at 1:00 pm

I just had a bit of fun with Twitter and the Google charts API. You can now add an image to your blog, web site or wherever and show a picture of what kind of a twitter user you are. All you need to do is embed an image and give it the right source:


For example my user name is codepo8, which would be:

<img src="">

And the resulting image is:

For John Hicks for example it is:

<img src="">

And the resulting image is:

How it is done and how to “change stuff”

You can download the source code and have a play with this (I hope this will not spike my traffic :) so it might go offline if that is the case). There’s really not much magic to this:

First I get the user name and filter out nasties:

$user = $_GET['user'];
$isjs = "/^[a-z|A-Z|_|-|$|0-9|.]+$/";
Then I set the content type to show the image and use cURL to get the information from the user’s twitter page.

$info = array();
$cont = get(''.$user);
I get the information using regular expressions and put them in an associative array:

preg_match_all('/<span id="following_count" class="stats_count numeric">([^>]+)</span>/msi',$cont,$follow);
$info['follower'] = convert($follow[1][0]);
preg_match_all('/<span id="follower_count" class="stats_count numeric">([^>]+)</span>/msi',$cont,$follower);
$info['followed'] = convert($follower[1][0]);
preg_match_all('/<span id="update_count" class="stats_count numeric">([^>]+)</span>/msi',$cont,$updates);
$info['updater'] = convert($updates[1][0]);
The convert function removes the comma punctuation added by twitter and makes sure the values are integers. I then need to determine which of the three values is the highest and define a scaling factor as the Google API only allows values up to 100. I then check what the type of the user is by getting the right array key and change the values for displaying.

$max = max($info);
$convert = 100 / $max ;
foreach($info as $k=>$f){
if($f = $max){
$type = $k;
$disp[$k] = $f * $convert;
I check the type and assemble the display string accordingly:

if($type = ‘updater’){
$t = ’ is an ‘;
	<p>if($type =&nbsp;'follower'){<br />
$t&nbsp;=&nbsp;'&nbsp;is&nbsp;a&nbsp;';<br />
	<p>if($type&nbsp;= &#8216;followed&#8217;){<br />
$t = &#8217; is being &#8216;;<br />
$title = $user . $t . $type;
I assemble the labels array and the values array and add all up to the correct Google charts API url. I use cURL to get the image and echo it out.

	<p>$labels&nbsp;=&nbsp;join($out,'|');<br />
$values&nbsp;=&nbsp;join($disp,',');<br />
$img&nbsp;=&nbsp;get(';chco=336699&#38;'.<br />
'chtt='.urlencode($title).'&#38;chd=t:'.$values.<br />
'&#38;chs=350x100&#38;chl='.$labels);<br />
echo&nbsp;$img;<br />
The rest are the cURL and convert helper functions.

	<p>function&nbsp;convert($x){<br />
$x&nbsp;=&nbsp;str_replace(',','',$x);<br />
$x&nbsp;=&nbsp;(int)$x;<br />
return&nbsp;$x;<br />

You like?

Faster version (one cURL, instead of two)

Instead of setting the PNG header and echoing out the image you can also just set a location header at the end and redirect the URL request to Google’s servers. I guess they have more bandwidth. :)

Tags: , , , , , ,

Share on Twitter