Приглашаем посетить
Блок (blok.lit-info.ru)

Section 16.7.  Loading Existing Images

Previous
Table of Contents
Next

16.7. Loading Existing Images

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 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 in creating a customizable button script is to create a blank button (as in Figure 16-9) using the art package of your choice.

Figure 16-9. A blank button saved in PNG format is easy to load into PHP for dynamic modification
Section 16.7.  Loading Existing Images


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

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

  • We need to center the text in the middle of the button.

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

With that in mind, here's the new script:

    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, among 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 16-10, 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 web site 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 usedthat way, you can use file_exists( ) to attempt to load the existing picture and save the extra work.

Figure 16-10. An empty button overlaid with rendered text
Section 16.7.  Loading Existing Images


With just a little work, we can even add a simple shadow to the text, as shown in Figure 16-11. To do this, allocate a new color for the shadow (such as black), then call imagettftext( ) twiceonce for the shadow, and again for the text itself. Offset the shadow by +1 on X and Y, and the text by -1 on X and Y, completing the effect.

Figure 16-11. Drawing text twice to get a shadow
Section 16.7.  Loading Existing Images



Previous
Table of Contents
Next