I found a couple of tutorials to edit image in PHP. However it is showing a black image. I couldn't understand what's the problem here.
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('http://iamsaurav.xyz/images/avatar.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'http://iamsaurav.xyz/images/oswald.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);
?>
If you use the full path to both the image and the font it appears to work fine ~ obviously the paths used here are pertinent to my system so you would need to edit appropriately. The most likely problem is that PHP is looking in the wrong directory for the image.... if the text does not appear then that path is also most likely wrong.
<?php
//Set the Content Type
header('Content-type: image/jpeg');
/* use absolute path */
$jpg_image = imagecreatefromjpeg('C:/Temp2/src.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'c:/wwwroot/inc/fonts/AMAZON.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);
?>
Related
I want to write on this picture some text
but i cant because every time i add header happend this
but i read that i must add header because if i dont add header happend errors, sorry for bad English..
<?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);
?>
Image is not showing,not even the text.I'm not able to find the error.Is there any need of adding extra plugins or library files in it??
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'typesimp.TTF';
// Set Text to Be Printed On Image
$text = "Hello World!!";
// 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);
?>
Have you added the content type?
//Set the Content Type
header('Content-type: image/jpeg');
Hello following this answer, i'm trying to add a text to a image using GD Library, sadly i'm unable to do it, this is my code:
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('serra.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'denmark.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);
?>
This code will Output the image without the text in it.
The serra.jpg and denmark.ttf files are both in the same folder as the script.
Running the gd_info() function on my server i got the following:
Any ideas on what could possible be going on?
Go through the following steps:
if you are working in Unix-based OS check permissions for
'denmark.ttf' file. Make sure that it accessible by your php script
change font path as follows: $font_path = './denmark.ttf';
Get a solution,Check this code.
imagejpeg($jpg_image,'newimage.jpeg');
You are not save your image.
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$source="mk.jpg";
$jpg_image = imagecreatefromjpeg($source);
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'ASMAN.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,'newimage.jpeg');
// Clear Memory
imagedestroy($jpg_image);
?>
I was having some weird problem with the $font_path.
So reading the http://php.net/manual/en/function.imagettftext.php
I saw that i have to add putenv... before the $font_path variable like this:
putenv('GDFONTPATH=' . realpath('.'));
$font_path = 'denmark.ttf';
Now is working fine.
Also i tested the solution presented by Roman, and by just changing the font_path to:
$font_path = './denmark.ttf';
The script worked fine!
Thanks for the help guys!
$font_path = 'denmark.ttf';
Should be
$font_path = 'denmark'; //no need to incluce .ttf when not using backward slash.
The image get loaded to the browser fine but when I try writing some text onto it, the image breaks (like this: http://www.tradenepal.com.np/test.php ). When I comment out imagettftext(), the image does not load again. This happening on my localhost and I use WampServer Version 2.5. I have gone through so many comments on the inetrnet but I can't seem to know what the problem is. Any help would much be appreciated. Thank you. My code:
<?php
//Set content type
header('Content-type: image/jpeg');
// Create image from existing image
$jpgImage = imagecreatefromjpeg('file.jpg');
// Allocate color for text
$white = imagecolorallocate($jpgImage, 255, 255, 255);
// Set Path to Font File
$font = 'arialbd.ttf';
// Text to print to image
$text = 'Testing text output';
// Print Text On Image
imagettftext($jpgImage, 75, 0, 50, 400, $white, $font, $text);
// Send Image to Browser
imagejpeg($jpgImage);
// Clear Memory
imagedestroy($jpgImage);
?>
// Send Image to Browser
imagepng($jpg_image); <------ remove image type png
// Output the image
imagejpeg($jpg_image);
I've tested, it works.
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('file.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'arialbd.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);
?>
okay, so im using this piece of code:
<?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);
?>
and i have GD installed.
but the results show me this:
any idea why?