Drawing Text Vertically


You can draw text with imagestringhorizontally, anyway. What about drawing text vertically, as when you want to label the y-axis of a graph? Use the imagestringup function:

 imagestringup(resource image, int font, int x, int y, string s, int color) 

This function draws the string s vertically in the image specified by image at coordinates x, y in color color. If font is 1, 2, 3, 4, or 5, a built-in font is used.

To use this function in code, we can reverse the height and width from the example in the previous chunk to create a vertical image:

 $width = 3 * imagefontheight($font_number); $height = 2 * strlen($text) * imagefontwidth($font_number); 

Now that you're flipping x and y coordinates, you also have to change the - to a + in the calculation of the y position:

 $x_position = ($width - imagefontheight($font_number)) / 2; $y_position = ($height + (strlen($text) * imagefontwidth($font_number))) / 2; 

Then you draw the text using imagestringup, as shown in phpdrawtextup.php, which appears in Example 11.

Example 11. Drawing text vertically, phpdrawtextup.php
 <?php     $font_number = 4;     $text = "No worries.";     $width = 3 * imagefontheight($font_number);     $height = 2 * strlen($text) * imagefontwidth($font_number);     $image = imagecreate($width, $height);     $back_color = imagecolorallocate($image, 200, 200, 200);     $drawing_color = imagecolorallocate($image, 0,   0,   0);     $x_position = ($width - imagefontheight($font_number)) / 2;    $y_position = ($height + (strlen($text) * imagefontwidth($font_number)))/2;     imagestringup($image, $font_number, $x_position, $y_position, $text,         $drawing_color);     header('Content-Type: image/jpeg');     imagejpeg($image);     imagedestroy($image); ?> 

We'll embed phpdrawtextup.php in phpdrawtextup.html like this:

 <HTML>     <HEAD>         <TITLE>Drawing text vertically</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Drawing text vertically             </H1>             <BR>             <IMG src="/books/1/265/1/html/2/phpdrawtextup.php">         </CENTER>     </BODY> </HTML> 

The results, including the vertical text, appear in Figure 12.

Figure 12. Drawing text vertically.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net