Home arrow PHP arrow Page 2 - Dynamic Twitter Signature Images with PHP

Building the class - PHP

Twitter is the latest and greatest craze in social networking sites. As such, many developers and businesses are using it as a marketing technique. Today we’re going to come up with a method for creating a dynamic signature image for forum posts and emails that will automatically load your Twitter feed and display your latest Twitter status. We’ll be using PHP to grab the content along with GD2 support to create the image.

  1. Dynamic Twitter Signature Images with PHP
  2. Building the class
  3. Parsing the XML feed
  4. Grabbing the avatar image
By: Nilpo
Rating: starstarstarstarstar / 3
August 11, 2009

print this article



With all of the basics out of the way, it’s time to dive into some PHP and begin building a class.  We’re going to create a class called TwitterSignature.  This class will load the background and user feed, create the signature image, and then output it to the browser as an image.  This will allow us to simply point the HTML source of an image to this PHP file and have it render in any page, as you’ll see later.

class SignatureImage


    private $screen_name;

    private $profile_image;

    private $status_text;

    private $local_avatar;   

    public function __construct($name, $bg_image, $adir)




Here’s the basic structure of the entire class.  We first create a few class-level variables that we will use as we start adding functions.  $screen_name, $profile_image, $status_text, and $local_avatar are all strings that will contain the Twitter screen name, URL to the Twitter avatar, the XML status feed, and local avatar directory path, respectively.  I’ll explain each of these in more detail as we go along. 

Notice also that I’m using a constructor for this class.  All of the class's functions will be called internally through this constructor.  There won’t be any need to call functions when the class is actually implemented.

The constructor accepts three required parameters: the Twitter screen name to process, the location of the background image, and the local directory where avatars are/will be stored.  The Twitter screen name is the most important, as we’ll need that to access the Twitter feed.

Twitter provides an API with which you can access a user’s status feed in XML format.  This is available from a public access URL in the form of:


Replace screenname with the user’s Twitter screen name.  Adding the query string “count=1” forces the API to return only the latest status message, slimming down the request a bit.  Since we’re only interested in the latest status, it shortens both the response time and the request length, which increases the performance of our script.  A user feed looks something like this:

<?xml version="1.0" encoding="UTF-8"?>

<statuses type="array">


  <created_at>Thu Apr 16 04:48:43 +0000 2009</created_at>


  <text>Just added myself to the http://wefollow.com twitter directory under:  #technology #windows #guru</text>

  <source>&lt;a href=&quot;http://wefollow.com&quot;&gt;WeFollow&lt;/a&gt;</source>










    <location>Salem, Ohio</location>

    <description>Ask the Windows Guru!</description>











    <created_at>Mon Mar 23 02:56:34 +0000 2009</created_at>



    <time_zone>Eastern Time (US &amp; Canada)</time_zone>









>>> More PHP Articles          >>> More By Nilpo

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: