How to Use PHP GD to write Text


Actually this isn't created by me but by a friend of mine, so i think it may be usefull..
PHP Code
  1. <?php
  2. header("Content-type: image/gif"); // We have to set the header of the file as its an image
  3.  
  4. $text = @$_GET['text']; // Get the text to write
  5. // The $_GET function grabs the data specified by the ?text parameter on the URL
  6.  
  7. $font = 'Volter.ttf'; // Set the TTF font we want to use
  8. // This is just a variable set, which we will later use in the imagettftext funciton
  9.  
  10. $fontsize = "6.5"; // Set the size of the font we want to use
  11. // This again is just a variable like above.
  12.  
  13.  
  14.  
  15. // Create the image
  16. $size = imagettfbbox($fontsize, 0, $font, $text); // Set the positions of the area of the font
  17. $width = $size[2] + $size[0] + 8;
  18. $height = abs($size[1]) + abs($size[7]); // Get values
  19. // We have the right sizes for the text
  20.  
  21. $im = imagecreate($width, $height);
  22. // Create the image of the right size for the text to fit
  23.  
  24. // Create some colors
  25. $white = imagecolorallocate($im, 255, 255, 255); // Create the colour white using RGB
  26. $black = imagecolorallocate($im, 0, 0, 0); // Create the colour black using RGB
  27.  
  28.  
  29. # The imagecolorallocate uses RGB colours to allocate an individual colour to a string. EG/ $white.
  30.  
  31. // Make a colour transparent
  32. $colourBlack = imagecolorallocate($im, 255, 255, 255); // Set colour black using RBG
  33. imagecolortransparent($im, $colourBlack); // Make colour black transparent
  34.  
  35. # imagecolortransparent makes the pre-made string which has been allocated a colour transparent on the image.
  36.  
  37. // Add the text
  38. imagefttext($im, $fontsize, 0, 0, abs($size[5]), $black, $font, $text);
  39.  
  40. # imagettftext writes the text to the image, the different stages of the funciton are explain below the php code (from php.net)
  41.  
  42. imagegif($im); // Output the image using imagegif()
  43. imagedestroy($im); // Delete the image to keep server load down
  44. ?>

ALl explained inside :D
gbt91's Avatar
Author:
Views:
3,029
Rating:
There are currently no comments for this tutorial, login or register to leave one.