Specifications

The function ImageCreateFromPNG() takes the filename of a PNG as a parameter, and returns
a new image identifier for an image containing a copy of that PNG. Note that this does not
modify the base PNG in any way. We can use the ImageCreateFromJPEG() and
ImageCreateFromGIF()functions in the same way if the appropriate support is installed.
Generating Images
C
HAPTER 19
19
G
ENERATING
IMAGES
415
The call to ImageCreateFromPNG() only creates the image in memory. To save the
image to a file or output it to the browser, we must call the
ImagePNG() function.
Well come to that in a minute, but we have other work to do with our image first.
NOTE
Fitting the Text onto the Button
We have some text typed in by the user stored in the $button_text variable. What we want to
do is print that on the button in the largest font size that will fit. We do this by iteration, or
strictly speaking, by iterative trial and error.
We start by setting up some relevant variables. The first two are the height and width of the
button image:
$width_image = ImageSX($im);
$height_image = ImageSY($im);
The second two represent a margin in from the edge of the button. Our button images are
beveled, so well need to leave room for that around the edges of the text. If you are using dif-
ferent images, this number will be different! In our case, the margin on each side is around 18
pixels.
$width_image_wo_margins = $width_image - (2 * 18);
$height_image_wo_margins = $height_image - (2 * 18);
We also need to set up the initial font size. We start with 32 (actually 33, but well decrement
that in a minute) because this is about the biggest font that will fit on the button at all:
$font_size = 33;
Now we loop, decrementing the font size at each iteration, until the submitted text will fit on
the button reasonably:
do
{
$font_size--;
24 7842 CH19 3/6/01 3:42 PM Page 415