Month: December 2009

Create Twitter Counter Badge using CodeIgniter

I know this is one of those “re-inventing the wheel’” kind of tutorials but I had nothing better to do during my Eid vacation 😛

Things you will need

1. Download and install the excellent CodeIgniter Curl Library

2. You will need an image which will contain your follower count.

The Code

In your Controller class add the following methods

function MyTwitterCounterBadge($username)
    $config['source_image'] = './images/YOUR_BUTTON_IMAGE.gif';
    $config['wm_text'] = $this->_getTwitterCounter($username);
    $config['wm_type'] = 'text';
    $config['wm_font_path'] = './system/fonts/arlrdbld.ttf';
    $config['wm_font_size'] = '12';
    $config['wm_font_color'] = 'ffffff';
    $config['wm_vrt_alignment'] = 'top';
    $config['wm_hor_alignment'] = 'center';
    $config['wm_hor_offset'] = '10';
    $config['wm_vrt_offset'] = '10';
    $config['dynamic_output'] = 'TRUE'; // DO NOT CHANGE



function _getTwitterCounter($username)
    $count = 0;
    $json = $this->curl->simple_get("$username.json");
    if( $json )
        $obj = json_decode($json);
        $count = $obj -> {'followers_count'};
    return $count;


Change the values of wm_font_path, wm_font_size, wm_font_color, wm_vrt_alignment, wm_hor_alignment, wm_hor_offset and wm_vrt_offset to fit your needs. Please refer to the CodeIgniter user guide to know more about these settings.

Now all you have to do is include an image tag in your view page that points to the controller function above

<img src="<?=BASE_URL?>CONTROLLER_NAME/MyTwitterCounterBadge/TWITTER_USER_NAME">


And that’s all there is to it. Now you have your own personalized twitter counter badge!

Happy Programming!