Page 573 - Beginning PHP 5.3
P. 573
Chapter 17: Generating Images with PHP
Figure 17-24
How It Works
First, the script creates a blank image and allocates two colors:
<?php
$textImage = imagecreate( 200, 120 );
$white = imagecolorallocate( $textImage, 255, 255, 255 );
$black = imagecolorallocate( $textImage, 0, 0, 0 );
Then, the string is drawn on the image by calling imagefttext():
imagefttext( $textImage, 16, 0, 10, 50, $black, “/usr/share/fonts/truetype/
ttf-bitstream-vera/Vera.ttf”, “Vera, 16 pixels” );
Finally, the script outputs the resulting image:
header( “Content-type: image/png” );
imagepng( $textImage );
imagedestroy( $textImage );
?>
To demonstrate how to draw rotated text, change the line:
imagefttext( $textImage, 16, 0, 10, 50, $black, “/usr/share/fonts/truetype/
ttf-bitstream-vera/Vera.ttf”, “Vera, 16 pixels” );
to:
imagefttext( $textImage, 16, -30, 10, 30, $black, “/usr/share/fonts/
truetype/ttf-bitstream-vera/Vera.ttf”, “Vera, 16 pixels” );
All that has changed is that you’re now drawing the text in a –30-degree direction, rather than zero
degrees. The direction of rotation is counterclockwise, so a negative number angles the text in a
clockwise direction. If zero degrees is three o’clock, then 30 degrees in a clockwise direction is the four
o’clock position on a clock. You can see the results in Figure 17-25.
535
9/21/09 2:48:49 PM
c17.indd 535
c17.indd 535 9/21/09 2:48:49 PM