简体   繁体   中英

How to add text to an image with PHP GD library

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?

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);
?>

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);

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);
  $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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM