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);
?>
Related
Using bellow PHP code I can add text to an image but Now I want to add a background color to image text. Can you tell me how to do this?
Note: The text background needs to bellow the image.
What is now:
What I want:
PHP code:
//Set the Content Type
header('Content-type: image/jpg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Set Path to Font File
$font_path = 'arial.ttf';
// Set Text to Be Printed On Image
$text = "My Content Goes to here";
// Print Text On Image
//$image = imagecreatefromjpg($jpg_image);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
The problem becomes easy, if you don't think of it as a background color, but as drawing a rectangle before writing the text:
$bcolor=imagecolorallocate($jpg_image, 0x00, 0xC0, 0xFF);
imagerectangle($jpg_image, 0, $orig_height*0.8, 0, $orig_height, $bcolor);
before the imagettftext.
EDIT
It was of course imagefilledrectangle, not imagerectangle. Here is the complete script
<?php
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
// Allocate A Color For The background
$bcolor=imagecolorallocate($jpg_image, 0x00, 0xC0, 0xFF);
//Create background
imagefilledrectangle($jpg_image, 0, $orig_height*0.8, $orig_width, $orig_height, $bcolor);
// Set Path to Font File
$font_path = realpath(dirname(__FILE__)).'/arial.ttf';
// Set Text to Be Printed On Image
$text = "New Content Goes to here";
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Print Text On Image
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
//Set the Content Type
header('Content-type: image/jpg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
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);
?>
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');
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?