How to Add Text to an Image with PHP Gd Library

How to add text to an image with PHP GD library

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

Add Text to Image and download PHP-GD

I solved this, Little tweak on code.

 function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

$vnum=generateRandomString(10);
$jpg_image = imagecreatefromjpeg('voucher.jpg');
$white = imagecolorallocate($jpg_image, 20, 16, 6);
$font_path = 'font.TTF';
$text = "Voucher No: ".$vnum;
imagettftext($jpg_image, 15, 0, 85, 680, $white, $font_path, $text);

header('Content-type: image/jpeg');
imagejpeg($jpg_image,"".$vnum.".jpg");
header("Content-disposition: attachment; filename=".$vnum.".jpg");
header('Content-Description: File Transfer');
readfile("".$vnum.".jpg");
imagedestroy($jpg_image);

Center text on image using PHP GD

Check out imagettfbbox: http://www.php.net/manual/en/function.imagettfbbox.php. It will give you the extents of the text you wish to render. Then it's simple arithmetic to center that on your image.

Php GD add text to png image always follow image background color

If you're loading an 8-bit PNG file, chances are that all 256 entries in the color palette have already been allocated. Your call to imagecolorallocate() will return false. That gets converted to 0, which presumably is the index of the background color.

What you should do is call imagecolorstotal() to see if it's less than 256. If the palette is full, convert the image to true color before proceeding.

Writing text on image using GD is not working

I think the font should be the Problem here.
You should include it with a path. Read the documentation at php:imagettftext

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

So you should make sure where the font is. Or try different combinations with leading "/" or without the file extension



Related Topics



Leave a reply



Submit