Hi - thanks for reading my post.
I copied an image creation script from a book about PHP. It works fine on my website but when I run it on my computer (IIS, PHP v5) I get an error. I know my host has PHP v.4.something. I know it seems odd that I wrote code in PHP4 but am using PHP5 but that's a long story I don't want to get into.
My question is: does anyone know what's wrong in the code below? Any suggestions to make it work in PHP5?
$button_text = "{$row['fname']} {$row['lname']}'s Bluewater Home Page";
// create an image of the right background and check size
$im = imagecreatefrompng ("blue-button.png");
$width_image = ImageSX($im);
$height_image = ImageSY($im);
// Our images need an 18 pixel margin in from the edge image
$width_image_wo_margins = $width_image - (2 * 18);
$height_image_wo_margins = $height_image - (2 * 18);
// Work out if the font size will fit and make it smaller until it does
// Start out with the biggest size that will reasonably fit on our buttons
$font_size = 33;
do
{
$font_size--;
// find out the size of the text at that font size
$bbox=imagettfbbox ($font_size, 0, "arial.ttf", $button_text);
$right_text = $bbox[2]; // right co-ordinate
$left_text = $bbox[0]; // left co-ordinate
$width_text = $right_text - $left_text; // how wide is it?
$height_text = abs($bbox[7] - $bbox[1]); // how tall is it?
} while ( $font_size>8 &&
( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
);
if ( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
{
// no readable font size will fit on button
echo "Text given will not fit on button.<BR>";
}
else
{
// We have found a font size that will fit
// Now work out where to put it
$text_x = $width_image/2.0 - $width_text/2.0;
$text_y = $height_image/2.0 - $height_text/2.0 ;
if ($left_text < 0)
$text_x += abs($left_text); // add factor for left overhang
$above_line_text = abs($bbox[7]); // how far above the baseline?
$text_y += $above_line_text; // add baseline factor
$text_y -= 2; // adjustment factor for shape of our template
$white = ImageColorAllocate ($im, 255, 255, 255);
ImageTTFText ($im, $font_size, 0, $text_x, $text_y, $white, "arial.ttf",
$button_text);
Header ("Content-type: image/png");
ImagePng ($im, "{$row['fname']}{$row['lname']}.png");
}
ImageDestroy ($im);