I find a very simple method on web about adding text on an image by PHP.
Reference: Write Text To Image
What u need is the following 3 files and an web server.
- msjh.ttf – The font file for the text
- test.png – The original image
- text_on_image.php – The php file which will display the text.png with text added
text_on_image.php
<?php
/*** set the header for the image ***/
header("Content-type: image/png");
/*** specify an image and text ***/
$im = writeToImage('test.png', 'HelloWorld');
/*** spit the image out the other end ***/
imagepng($im);
/**
*
* @Write text to an existing image
*
* @Author Kevin Waterson
*
* @access public
*
* @param string The image path
*
* @param string The text string
*
* @return resource
*
*/
function writeToImage($imagefile, $text){
/*** make sure the file exists ***/
if(file_exists($imagefile))
{
/*** create image ***/
$im = @imagecreatefrompng($imagefile);
/*** create the text color ***/
$text_color = imagecolorallocate($im, 0, 255, 0);
/*** set the font file ***/
$font_file = './msjh.ttf';
/*** splatter the image with text ***/
imagefttext($im, 20, 0, 25, 150, $text_color, $font_file, $text);
}
else
{
/*** if the file does not exist we will create our own image ***/
/*** Create a black image ***/
$im = imagecreatetruecolor(150, 30); /* Create a black image */
/*** the background color ***/
$bgc = imagecolorallocate($im, 255, 255, 255);
/*** the text color ***/
$tc = imagecolorallocate($im, 0, 0, 0);
/*** a little rectangle ***/
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/*** output and error message ***/
imagestring($im, 1, 5, 5, "Error loading $imagefile", $tc);
}
return $im;
}
?>
You can add text to gif or jpeg by using the imagegif()/imagejpeg() and imagecreatefromgi()/imagecreatefromjpeg() to replace the imagepng() and imagecreatefrompng().
Done =)

Its nice and working for all types of images.
but what about the case in which i have to write text
on an image (with out creating the image ) that is image is physicaly available ,
thanx in advance
Ashwini
LikeLike
What is the source of the image?
LikeLike
sorry for the late response
actualy image is being uploaded in a folder
http://server1PHPtest_ashwini
but it is not showing image
here is the code
header(“Content-type: image/gif”);
$string = “This is my test string.”;
//$font = 2;
//$width = imagefontwidth($font) * strlen($string);
//$height = imagefontheight($font);
$image = imagecreatefromgif(“http://server1/PHP/test_ashwini/logo.gif”);
//$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,100,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);
imagestring ($image,$font,0,0,$string,$black);
imagegif ($image);
imagedestroy($image);
LikeLike
have you tried just simply display an url image?
header("Content-type: image/gif"); imagegif(imagecreatefromgif('http://url/test.gif'));is that ok?
by the way, u didn’t use the imagefttext() to write the text on the image.
LikeLike
hello friend
i am facing other problem
while scling the image (more than 100% mean increasing the width and height of the image ) imahe remains same but a black background appears in extra dimension.
while scaling to less thn hundred it works fine
any help
thanx
LikeLike
how did u scale the image? i used the php imagecopyresized() function to scale and image and didn’t meet any problem.
PHP – imagecopyresized
LikeLike