Enhancing Dynamic Twitter Signature Images with PHP

In my last article we began putting together a solution that will allow us to display dynamic Twitter signature images in forum posts and emails. In this article we’ll continue where we left off by adding the functions that will harness the power of GD to create the actual image.

Before we get too involved, let’s take a moment to look at the code that we’ve built so far.

class SignatureImage

{

    private $screen_name;

    private $profile_image;

    private $status_text;

    private $local_avatar;

   

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

    {

        $this->fetchUserInfo(strtolower($name));

        $this->fetchAvatar($this->profile_image, $adir);

    }

 

    private function fetchUserInfo($name)

    {

        $url = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1";

        $xml = $this->curlRequest($url);

        if ($xml === false) {

            // User feed unavailable.

        }

        $statuses = new SimpleXMLElement($xml);

        if (!$statuses || !$statuses->status) {

            // Invalid user channel.

        }

       foreach ($statuses->status as $status) {

            $this->status_text   = (string) $status->text;

            $this->profile_image = (string) $status->user->profile_image_url;

            $this->screen_name   = (string) $status->user->screen_name;

            break;

        }

    }

 

    private function curlRequest($url)

    {

        if (!extension_loaded(‘curl’)) {

            // PHP extension CURL is not loaded.

        }

        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_HEADER, false);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($curl);

        if (curl_errno($curl) !== 0 || curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {

            $result === false;

        }

        curl_close($curl);

        return $result;

    }

 

    private function fetchAvatar($url, $adir)

    {

        $fname      = end(explode(‘/’, $url));

        $adir       = preg_match(‘#^(.*?)/$#i’, $url) ? $adir : "{$adir}/";

        $fname      = $adir . $fname;

        if ( !file_exists($fname) ) {

            $img = $this->curlRequest($url);

            $fp = fopen($fname, ‘w’);

            fwrite($fp, $img);

            fclose($fp);

        }

        $this->local_avatar = $fname;

    }

}

The code we have so far will accept a user name and retrieve the XML status feed using the Twitter API.  Then it parses that information, assigns some class-level variables, and creates a local copy of the user’s Twitter avatar.  Now we need to assemble these pieces into a single “signature” image.  To do that we’ll create a function called renderImage.  So let’s go ahead and complete the class’s constructor with a call to this function.

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

    {

        $this->fetchUserInfo(strtolower($name));

        $this->fetchAvatar($this->profile_image, $adir);

        $this->renderImage();

    }

{mospagebreak title=Rendering an image} 

The renderImage function we’ll be creating needs to start with the supplied background image.  Then it should add the user’s avatar and the text of their latest Twitter status.  This should be finalized into an image and served to the browser.  Let’s look at the code to do that.

    private function renderImage($bg_image)

    {

        if ( !function_exists(‘gd_info’) ) {

            // No GD support.

        }

        $margin_left = 11;

        $margin_bottom = 6;

        $image = @imagecreatefromjpeg($bg_image);

        if (!$image) {

            // Unable to create image.

        }

        $this->embedAvatar($image, $this->local_avatar, $margin_left, $margin_bottom);

        $string = "{$this->screen_name}: {$this->status_text}";

        $this->embedText($image, $string, $margin_left, $margin_bottom);

        header("Content-type: image/jpeg");

        $created = @imagejpeg($image);

        if (!$created) {

            // Unable to finalize image.

        }

        @imagedestroy($image);

    }

If you’ve ever used GD to create dynamic images in the past, this should look pretty familiar to you.  The ImageCreateFromJpeg function is used to create an image from a background.  Then we make calls to two custom functions, embedAvatar and embedText, to add the user’s avatar and status text to the image.  The image is then finalized with the ImageJpeg function, which creates and outputs a JPEG image.  Notice that I also perform a little cleanup with the ImageDestroy function before ending my method.

{mospagebreak title=Embedding the pieces}

Let’s take a moment to examine the two custom methods that embed the avatar and status text into our image.

    private function embedAvatar(&$image, $avatar, $left, $bottom)

    {

        if (file_exists($avatar)) {

            if (stristr($avatar, ‘.jpg’)) {

                $aimg = @imagecreatefromjpeg($avatar);

            } elseif (stristr($avatar, ‘.png’)) {

                $aimg = @imagecreatefrompng($avatar);

            }

            if (!$aimg) {

                // Unable to create avatar.

            }

            $awidth  = imagesx($aimg);

            $aheight = imagesy($aimg);

            $top     = imagesy($image) – $aheight – $bottom;

            $copied  = @imagecopy($image, $aimg, $left, $top, 0, 0, $awidth, $aheight);

            if (!$copied) {

            // Unable to add avatar to image.

            }

        }

    }

The first of our two custom methods embeds the user’s avatar image that was retrieved from the Twitter web site.  Notice that I’ve implemented some logic here to prevent any unnecessary errors if the image was unavailable.  The entire operation is enclosed in an If structure that first checks that the local image exists.  If it does not, the method will simply return without it.  This allows the signature image to be created without an avatar.

If the image does exist, we grab its dimensions and calculate some padding.  It’s finally embedded using GD’s ImageCopy function.

    private function embedText(&$image, $text, $left, $top, $tmaxw=300, $font=3)

    {

        $fheight = imagefontheight($font);

        $color   = imagecolorallocate($image, 100, 100, 100);

        $lines   = $this->lineWrap($font, $text, $tmaxw);

        $lmax    = floor(imagesy($image) / $fheight);

        $lcount  = (count($lines) > $lmax) ? $lmax : count($lines);

        $ttop    = (imagesy($image) – ($lcount*$fheight)) / 2;

        $tleft   = 2*$left + 48;

        while ((list($num, $line) = each($lines)) && $num < $lcount) {

            imagestring($image, $font, $tleft, $ttop, $line, $color);

            $ttop += $fheight;

        }

    }

The text is embedded similarly using the ImageString function.  This time however, we need to do a little work on the text before we can add it.  Since the string may be too long for the image, it first must be truncated and then word-wrapped into multiple lines.  To do that, we’ll add one final function to this class.

    private function lineWrap($font, $text, $maxwidth)

    {

        $fwidth  = imagefontwidth($font);

        if ($maxwidth != NULL) {

            $maxcharsperline = floor($maxwidth / $fwidth);

            $text = wordwrap($text, $maxcharsperline, "n", 1);

        }

        return explode("n", $text);

    }

Here, the lineWrap function truncates and separates the status text into multiple lines.  Those lines are then returned as an array back to the embedText method, where they are added to the image.  The embedText method contains logic that will auto-center the text vertically based upon the number of lines returned.

{mospagebreak title=Implementing the class}  

Now that we’ve built a class that will produce a Twitter signature image, it’s time to put that class to use.  The easiest way is to add the relevant code to instantiate this class right into the same PHP file so that the file can be loaded all in one go.

isset($_GET['name']) or die(‘You must provide a user name.’);

 

new SignatureImage($_GET['name'], ‘banners/my_banner.jpg’, ‘avatars’, ‘cache’);

Adding the above code anywhere outside of the class will do.  Save the PHP file with a name such as SignatureImage.php and you’re ready to go.  Any time you want to display your signature image, you simply need to add an HTML image reference using the URL to your PHP file.  For example, the URL to display my signature image would look something like this:

http://www.somesite.com/SignatureImage.php?name=WindowsGuru

Don’t forget to add the Twitter screen name to the URL so that the PHP script knows what user to get stats for.  Since the script returns an actual JPEG image, you can add this quite easily using HTML’s IMG tag or VBCode forum tags.

<img src=" http://www.somesite.com/SignatureImage.php?name=WindowsGuru">

       Or

[IMG] http://www.somesite.com/SignatureImage.php?name=WindowsGuru[/IMG]

While we have a fully operational PHP script at this point, let’s not stop there.  Tune in for the third and final installment in this series and I’ll show you how to implement proper object-oriented PHP error handling into this script.  I’ll also show you a quick and effective method of maintaining usage statistics to see how many times your signature image is displayed.  Until next time, keep coding!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye