Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

The imagettfbbox function returns an array of points that describe a bounding box
around text to be drawn by the imagettftext function. The points are relative to the
leftmost point on the baseline. The array elements correspond to the lower-left, lower-
right, upper-right, and upper-left corners, in that order, as shown in Table 12.3.


This function may not be available, depending on the libraries available when PHP was
compiled.


boolean imagettftext(integer image, integer point_size, integer
angle, integer x, integer y, integer color, string font, string
text)


The imagettftext function uses a TrueType font to draw a string of text. The x and y
arguments refer to the leftmost position of the baseline. The text will radiate from that
point at the given angle, which should be from 0 to 360. An angle of zero represents
normal right-to-left text. The font argument is the full path to a .ttf file.


This function may not be available, depending on the libraries available when PHP was
compiled.


<?
/
Draw text using a TrueType font
Also, draw a box around the text.
/


//set parameters for text
$size = 40;
$angle = 30;
$startX = 50;
$startY = 100;


//create red square
$image = imagecreate(200, 200);
$colorRed = imagecolorallocate($image, 255, 0, 0);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 10, 10, $colorRed);


//get bounding box
$Box = imagettfbbox($size, $angle, "comic.ttf", "PHP");


//move bounding box to starting point (100,100)
for($index = 0; $index count($Box); $index += 2)
{
$Box[$index] += $startX;
$Box[$index+1] += $startY;
}


//draw bounding box
imagepolygon($image, $Box, count($Box)/2, $colorBlack);

Free download pdf