Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.7     Loading existing images: imagecreatefrompng(), imagecreatefromjpeg(), and getimagesize()

This is NOT the latest copy of this book; click here for the latest version.

resource imagecreatefrompng ( string filename)

resource imagecreatefromjpeg ( string filename)

resource getimagesize ( string filename [, array imageinfo])

Some of the best ways to use the image functions in PHP are with existing images. For example, you can write a script to dynamically create buttons by first loading a blank button image from your hard drive and overlaying text on top. Loading images is remarkably easy, as you might expect, and takes the form of a call to imagecreatefrom*(), where the * is "png", "jpeg", or various other formats. These functions take just one parameter, which is the file to load, and return an image resource for use as we've been doing already.

The first step to creating a customisable button script is to create a blank button using the art package of your choice. See for an example:



Adding text to this button is largely the same as our existing text code, with a few minor changes:

  • The $blue colour is no longer needed, and we will not be using imagecreate()

  • We need to centre the text in the middle of the button

  • The font size needs to come down a little in order to fit the button

So, with that in mind, here's the new script:

<?php
    
if(!isset($_GET['size'])) $_GET['size'] = 26;
    if(!isset(
$_GET['text'])) $_GET['text'] = "Button text";

    
$size = imagettfbbox($_GET['size'], 0, "ARIAL", $_GET['text']);
    
$xsize = abs($size[0]) + abs($size[2]);
    
$ysize = abs($size[5]) + abs($size[1]);

    
$image = imagecreatefrompng("button.png");
    
$imagesize = getimagesize("button.png");
    
$textleftpos = round(($imagesize[0] - $xsize) / 2);
    
$texttoppos = round(($imagesize[1] + $ysize) / 2);
    
$white = ImageColorAllocate($image, 255,255,255);

    
imagettftext($image, $_GET['size'], 0, $textleftpos, $texttoppos, $white, "ARIAL", $_GET['text']);
    
header("content-type: image/png");
    
imagepng($image);
    
imagedestroy($image);
?>

The new function in that script is getimagesize(), which returns the width and height of the image specified in its parameter as an array, with elements 0 and 1 being the width and height respectively. In addition, element 2 is the type of the picture, and will be set to either IMAGETYPE_BMP, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_PSD, IMAGETYPE_SWF, amongst other values. This element is particularly helpful when used with image_type_to_mime_type().

Running that script without any parameters generates the picture shown in Figure 21 , although you can send "text" and "size" if you want to play around. With this script in place, you can generate a whole toolbar of buttons for a website using this one script, simply by changing the "text" value you pass in. Of course, it is not very efficient to keep regenerating the same buttons each time a page is loaded, so if I were you I would save each generated picture as a file named after the text used - that way you can use file_exists() to attempt to load the existing picture and save the extra work.



With just a little work, we can even add a simple shadow to the text. To accomplish this effect, shown in , you need to allocate a new colour for the shadow (I used black), then call imagettftext() twice - once for the shadow, and again for the text itself. I offsetted the shadow by +1 on X and Y, and the text by -1 on X and Y, completing the effect. If you really want to get fancy, you can add a third call to imagettftext() that uses grey and has a slightly smaller offset, giving a smoother shadow effect.







<< 11.2.6 Outputting text: imagettftext() and imagettfbbox()   11.2.8 Colour and image fills: imagefill(), imagefilltoborder(), and imagesettile() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Yasahiro - 06 Sep 2008

it throws a fatal error

Fatal error: Call to undefined function imagettfbbox() in C:\wamp\www\v3d\op\textures\sign.php on line 5

(it has a local path because im hosting my webserver)



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow