13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

494 Chapter 22 Generating Images<br />

Listing 22.2<br />

Continued<br />

}<br />

while ( $font_size>8 &&<br />

( $height_text>$height_image_wo_margins ||<br />

$width_text>$width_image_wo_margins )<br />

);<br />

if ( $height_text>$height_image_wo_margins ||<br />

$width_text>$width_image_wo_margins )<br />

{<br />

// no readable font size will fit on button<br />

echo 'Text given will not fit on button.';<br />

}<br />

else<br />

{<br />

// We have found a font size that will fit<br />

// Now work out where to put it<br />

$text_x = $width_image/2.0 - $width_text/2.0;<br />

$text_y = $height_image/2.0 - $height_text/2.0 ;<br />

if ($left_text < 0)<br />

$text_x += abs($left_text);<br />

$above_line_text = abs($bbox[7]);<br />

$text_y += $above_line_text;<br />

// add factor for left overhang<br />

// how far above the baseline?<br />

// add baseline factor<br />

$text_y -= 2; // adjustment factor for shape of our template<br />

$white = imagecolorallocate ($im, 255, 255, 255);<br />

imagettftext ($im, $font_size, 0, $text_x, $text_y, $white, $fontname,<br />

$button_text);<br />

Header ('Content-type: image/png');<br />

imagepng ($im);<br />

}<br />

imagedestroy ($im);<br />

?><br />

This is one of the longest scripts we’ve looked at so far. Let’s step through it section by<br />

section.The script begins with some basic error checking <strong>and</strong> then sets up the canvas on<br />

which you’re going to work.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!