20

I have image creation code in image_creator.

<?php
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);
ImageDestroy($im);
?> 

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

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

</html>

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

5
  • 3
    First of all $black = ImageColorAllocate($im, 255, 255, 255); - this is white, not black. Do you get any errors? Checked the error-log?
    – iMoses
    Nov 7, 2012 at 10:41
  • @iMoses I think the $black is just because hes copied code from php.net: php.net/manual/en/function.imagettftext.php
    – George
    Nov 7, 2012 at 10:43
  • Doesn't make it right or make sense. I'm sure they got it right. It's not a problem, just confusing. The "blank" image might just be white on a white background.
    – iMoses
    Nov 7, 2012 at 10:45
  • I changed from white to black, but still same problem!!I think Imagettftext cannot find "verdana.ttf" file, although everything is in same folder..
    – roy mathew
    Nov 7, 2012 at 10:53
  • 1
    Then try giving the font file as ./verdana.ttf.
    – air4x
    Nov 7, 2012 at 11:00

4 Answers 4

59

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

<?php
//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
imagejpeg($jpg_image);

// Clear Memory
imagedestroy($jpg_image);
?>
4
  • 14
    You just pasted it from phpforkids.com/php/php-gd-library-adding-text-writing.php try to point out problem instead
    – Umair Aziz
    Nov 7, 2012 at 11:07
  • 7
    Please also cite your sources... otherwise it's just plagiarism. I've added in the citation.
    – Ben D
    Jun 15, 2013 at 0:12
  • If I would want to add several lines of text on the same image before sending it to the browser, would several imagettftext() functions would need to be added? Also, how do I save this image to an actual directory in my server so that the user can print it anytime he wants? Jun 15, 2016 at 6:25
  • for some reason that code doesn't work for png, changing of course the file and the function names
    – fpilee
    Jun 13, 2018 at 20:34
5

Problem here is, $black = ImageColorAllocate($im, 255, 255, 255); //<== this not black, its white //for black it should be like,

$black = ImageColorAllocate($im, 0, 0, 0);
0
0
  $img = imagecreatefromjpeg("certificate.jpg");//replace with your image 

  $txt = 'NILESH';//your text
  $fontFile = realpath("arial.ttf");//replace with your font
  $fontSize = 24;
  $fontColor = imagecolorallocate($img, 255, 255, 255);
  $black = imagecolorallocate($img, 0, 0, 0);
  $angle = 0;

  $iWidth = imagesx($img);
  $iHeight = imagesy($img);

  $tSize = imagettfbbox($fontSize, $angle, $fontFile, $txt);
  $tWidth = max([$tSize[2], $tSize[4]]) - min([$tSize[0], $tSize[6]]);
  $tHeight = max([$tSize[5], $tSize[7]]) - min([$tSize[1], $tSize[3]]);

  // text is placed in center you can change it by changing $centerX, $centerY values
  $centerX = CEIL(($iWidth - $tWidth) / 2);
  $centerX = $centerX<0 ? 0 : $centerX;
  $centerY = CEIL(($iHeight - $tHeight) / 2);
  $centerY = $centerY<0 ? 0 : $centerY;

  imagettftext($img, $fontSize, $angle, $centerX, $centerY, $black, $fontFile, $txt);
  imagejpeg($img,'my-certificate.jpg");//save image
  imagedestroy($img);
0

If you do not have a font file at hand, you could still simply use imagecreate:

// simple image to display URL (cut off)
$im = imagecreate(255, 170);

// white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// write the string at the top left
imagestring($im, 5, 0, 0, $url, $textcolor);

// Output the image
header('Content-type: image/png');

ob_start();
imagepng($im);
$image_bin = ob_get_contents();
ob_end_clean();

imagedestroy($im);

echo $image_bin;

exit();

However, it cuts off the text if too long.

Not the answer you're looking for? Browse other questions tagged or ask your own question.