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

Rendering an image - 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
 

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.



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