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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: