Home arrow PHP arrow Page 3 - Enhancing Dynamic Twitter Signature Images with PHP

Embedding the pieces - 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.

TABLE OF CONTENTS:
  1. Enhancing Dynamic Twitter Signature Images with PHP
  2. Rendering an image
  3. Embedding the pieces
  4. Implementing the class
By: Nilpo
Rating: starstarstarstarstar / 4
August 13, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: