PHP/Graphics Image/imagettfbbox

Материал из Web эксперт
Перейти к: навигация, поиск

Applying TrueType Fonts to an Image

   <source lang="html4strict">

<?php

   $animage = imagecreatefromjpeg ("myImage.jpg")){
   $black = imagecolorallocate ($animage, 0, 0, 0);
   $topleftx = 10;
   $toplefty = 30;
   $bottomrightx = 70;
   $bottomrighty = 90;
     
   $verdana = "C:\WINDOWS\Fonts\verdana.ttf";
   $dimensions = imagettfbbox (14,0,$verdana, $_GET["whattosay"]);
   $strlen = 100;
   $xcoord = 200;
     
   imagettftext($animage, 14, 0, $xcoord, 60, $black, $verdana, $_GET["whattosay"]);
     
   imagejpeg ($animage);
   header ("Content-type: image/jpeg");
     
   imagedestroy ($animage);

?>

 </source>
   
  


The eight elements in the array returned by imagettfbox( )

   <source lang="html4strict">

0 Lower-left corner, X coordinate

1 Lower-left corner, Y coordinate

2 Lower-right corner, X coordinate

3 Lower-right corner, Y coordinate

4 Upper-right corner, X coordinate

5 Upper-right corner, Y coordinate

6 Upper-left corner, X coordinate

7 Upper-left corner, Y coordinate

 </source>
   
  


Using the imagettfbbox() Function

   <source lang="html4strict">

<?php

   define("WIDTH", 300);
   define("HEIGHT", 100);
   define("F_SIZE", 40);
   define("F_ANGLE", 20);
   define("F_FONT", "myfont.ttf");
   define("F_TEXT", "PHP Unleashed");
   $img = imagecreate(WIDTH, HEIGHT);
   $white = imagecolorallocate($img, 255,255,255);
   $black = imagecolorallocate($img, 0,0,0);
   imagerectangle($img, 0,0,WIDTH-1,HEIGHT-1, $black);
   $box = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, F_TEXT);
   $start_x = (WIDTH/2) - (int)(($box[0] + $box[2] + $box[4] + $box[6])/4);
   $start_y = (HEIGHT/2) - (int)(($box[1] + $box[3] + $box[5] + $box[7])/4);
   $polygon = array($box[0]+$start_x,
                    $box[1]+$start_y,
                    $box[2]+$start_x,
                    $box[3]+$start_y,
                    $box[4]+$start_x,
                    $box[5]+$start_y,
                    $box[6]+$start_x,
                    $box[7]+$start_y);
   imagepolygon($img, $polygon, 4, $black);
   imageTTFtext($img, F_SIZE, F_ANGLE, $start_x,
                $start_y, $black, F_FONT, F_TEXT);
   header("Content-Type: image/png");
   imagepng($img);

?>

 </source>