Skip to content

Tareq Hasan

Create your own twitter image signature

Many days ago I built two image signatures as my forum signature. One of the signatures shows the latest post from my blog and another shows my latest twitter update. I was inspired to do that signature by seeing Shiplu Vai’s signature. Originally he was using them and I copied the concept :D

Anyway, here is my latest blog post signature Blog Update Signature and here is my twitter signature Twitter image signature

I will show you the process to create your own twitter image signature like me.

1. At first we will get the latest twitter update by this

$username = "tareq_cse"; //Twitter Username
$url      = "http://twitter.com/statuses/user_timeline/$username.json?count=1"; //json request url
$json     = json_decode( file_get_contents( $url ) );
$status   = $json[0]->text; //get the status from array/object

2. Now we will get the time distance from now and the twitted time and will format it

$tweeted  = strtotime( $json[0]->created_at ); //get the time as timestamp
$diff     = time() - $tweeted; //get the time difference

//Get the time distance
if ( $diff > 86400 ) {
    $diff = round( $diff / (60 * 60 * 24) );

    if ( $diff > 1 )
        $date = "$diff days ago";
    else
        $date = "$diff day ago";

} else if ( $diff > 3599 ) {

    $diff = round( $diff / 3600 );

    if ( $diff > 1 )
        $date = "$diff hours ago";
    else
        $date = "$diff hour ago";

} else {

    $diff = round( $diff / 60 );

    if ( $diff > 1 )
        $date = "$diff minutes ago";
    else
        $date = "$diff minute ago";
}

3. So now we got our formatted twitter text

$status = "Tareq: $status ($date)";

4. Now it’s time to create our image

//Now print as a image
header( "Content-type: image/png" );
$image      = imagecreate( 700, 16 ); //creating a demo image
$background = imagecolorallocate( $image, 255, 255, 255 ); // defining the background of demo image
$textcolor  = imagecolorallocate( $image, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) ); //random text color
imagestring( $image, 3, 16, 1, $status, $textcolor ); //creating the final image

imagepng( $image );
imagedestroy( $image );

We are done :)

Now run the page and you will get the latest twitter status as an image signature.

So here is the full code

<?php
$username = "tareq_cse"; //Twitter Username
$url      = "http://twitter.com/statuses/user_timeline/$username.json?count=1"; //json request url
$json     = json_decode( file_get_contents( $url ) );
$status   = $json[0]->text; //get the status from array/object
$tweeted  = strtotime( $json[0]->created_at ); //get the time as timestamp
$diff     = time() - $tweeted; //get the time difference

//Get the time distance
if ( $diff > 86400 ) {
    $diff = round( $diff / (60 * 60 * 24) );

    if ( $diff > 1 )
        $date = "$diff days ago";
    else
        $date = "$diff day ago";

} else if ( $diff > 3599 ) {

    $diff = round( $diff / 3600 );

    if ( $diff > 1 )
        $date = "$diff hours ago";
    else
        $date = "$diff hour ago";

} else {

    $diff = round( $diff / 60 );

    if ( $diff > 1 )
        $date = "$diff minutes ago";
    else
        $date = "$diff minute ago";
}

$status = "Tareq: $status ($date)";

//Now print as a image
header( "Content-type: image/png" );
$image      = imagecreate( 700, 16 ); //creating a demo image
$background = imagecolorallocate( $image, 255, 255, 255 ); // defining the background of demo image
$textcolor  = imagecolorallocate( $image, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) ); //random text color
imagestring( $image, 3, 16, 1, $status, $textcolor ); //creating the final image

imagepng( $image );
imagedestroy( $image );

Hope you enjoyed the post :D

8 thoughts on “Create your own twitter image signature”

  1. thanks, i relay need it. but the problem is i can't use bangla status. it i use bangla status some higibiji :D font print. plz solved it.

    http://devbd.co.cc/twitter.php

  2. Thats the problem we face. PHP's GD library has not full support for complex script like bangla. Although we can see bangla in image by setting bangla font, but it's not clear and broken with the 'আ-কার', ই-কার', 'যুক্তাক্ষর'. I am looking for this solution also

  3. ভাই আমার ওয়েভ সাইট ঠিকানাটা একটু এডিট করে দেন
    .cc.co হবে

    1. I haven't shown it here. I described the simple way. If you need to use the image, you need a blank png/jpg/gif image, and then there is a function to create from that image. Like imagecreatefrompng() creates any image from a given png image.