roy mathew roy mathew - 1 year ago 145
PHP Question

How to add text to an image with PHP GD library

I have image creation code in image_creator.

header("Content-Type: image/jpeg");
$im = ImageCreateFromGif("photo.gif");
$black = ImageColorAllocate($im, 255, 255, 255);
$start_x = 10;
$start_y = 20;
Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'verdana.ttf', "text to write");
Imagejpeg($im, '', 100);

The file for image output is image.php and has below code

<img src="http://localhost/image_creator.php"/>


When I run image.php, I just get a blank page. Why is it so?

Answer Source

Use this to add text to image (copied from PHP for Kids)

      //Set the Content Type
      header('Content-type: image/jpeg');

      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg('sunset.jpg');

      // Allocate A Color For The Text
      $white = imagecolorallocate($jpg_image, 255, 255, 255);

      // Set Path to Font File
      $font_path = 'font.TTF';

      // Set Text to Be Printed On Image
      $text = "This is a sunset!";

      // Print Text On Image
      imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);

      // Send Image to Browser

      // Clear Memory
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download