Text on image
Moderator: General Moderators
Text on image
Hey,
Can somebody direct me to a good exampl efor writing text on image.
What I need is:
1/ Load a blank canvas (gif of 500*500 pixel)
2/ Write text on it from a text file.
3/ Save new image with anme of original text file.
I need to do this for 50+ text file, so 50+ images...
thansk
s
Can somebody direct me to a good exampl efor writing text on image.
What I need is:
1/ Load a blank canvas (gif of 500*500 pixel)
2/ Write text on it from a text file.
3/ Save new image with anme of original text file.
I need to do this for 50+ text file, so 50+ images...
thansk
s
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
I've written a couple of articles related to this:
http://www.ooer.com/index.php?section=php&id=9 - Starting out with GD
http://www.ooer.com/index.php?section=php&id=18 - Truetype text in GD
Might help a little.
http://www.ooer.com/index.php?section=php&id=9 - Starting out with GD
http://www.ooer.com/index.php?section=php&id=18 - Truetype text in GD
Might help a little.
Thanks guys, it's a good start...trying n00B example as
<?php
$val = file("test.txt");
$num = $val[0];
$num++;
$fp = fopen( "test.txt", "w" );
fwrite( $fp, $num );
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
header ("Content-type: image/png");
$img_handle = ImageCreate (400, 110) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 255, 255, 255);
ImageTTFText ($img_handle, 9, 0, 5, 20, $txt_color, "ARIAL.ttf",
"IP : ".$_SERVER['REMOTE_ADDR']);
ImageTTFText ($img_handle, 9, 0, 5, 40, $txt_color, "ARIAL.ttf",
"HostName : ".$hostname);
ImageTTFText ($img_handle, 9, 0, 5, 60, $txt_color, "ARIAL.ttf",
"Referer : ".$_SERVER['HTTP_REFERER']);
ImageTTFText ($img_handle, 9, 0, 5, 80, $txt_color, "ARIAL.ttf",
"User Agent : ".$_SERVER['HTTP_USER_AGENT']);
ImageTTFText ($img_handle, 9, 0, 5, 100, $txt_color, "ARIAL.ttf",
"You are viewer number $num");
ImagePng ($img_handle);
?>
I am getting an error:
The image “test.php” cannot be displayed, because it contains errors.
The font is in the correct place, the test.txt file is in the correct place, all files have 777 permissions...
My GD with Freetype is enabled...
What am I missing?
s
<?php
$val = file("test.txt");
$num = $val[0];
$num++;
$fp = fopen( "test.txt", "w" );
fwrite( $fp, $num );
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
header ("Content-type: image/png");
$img_handle = ImageCreate (400, 110) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 255, 255, 255);
ImageTTFText ($img_handle, 9, 0, 5, 20, $txt_color, "ARIAL.ttf",
"IP : ".$_SERVER['REMOTE_ADDR']);
ImageTTFText ($img_handle, 9, 0, 5, 40, $txt_color, "ARIAL.ttf",
"HostName : ".$hostname);
ImageTTFText ($img_handle, 9, 0, 5, 60, $txt_color, "ARIAL.ttf",
"Referer : ".$_SERVER['HTTP_REFERER']);
ImageTTFText ($img_handle, 9, 0, 5, 80, $txt_color, "ARIAL.ttf",
"User Agent : ".$_SERVER['HTTP_USER_AGENT']);
ImageTTFText ($img_handle, 9, 0, 5, 100, $txt_color, "ARIAL.ttf",
"You are viewer number $num");
ImagePng ($img_handle);
?>
I am getting an error:
The image “test.php” cannot be displayed, because it contains errors.
The font is in the correct place, the test.txt file is in the correct place, all files have 777 permissions...
My GD with Freetype is enabled...
What am I missing?
s
grrr. I should think before asking!
ok this is working, here is my code:
<?php
$font_file = "ARIAL.TTF";
$font_size = 8;
$text = "test.txt";
$fp = fopen($text, "r");
$contents = fread($fp, filesize($text));
// Create the image resource and assign colors to variables (discussed in steps 1 and 5)
header("Content-type: image/png");
$im = ImageCreate (760, 520) or die ("Cannot Create image");
$red = imagecolorallocate($im, 255, 255, 255);
$txt_color = ImageColorAllocate ($im, 0, 0, 0);
ImageTTFText ($im, 9, 0, 5, 20, $txt_color, "ARIAL.TTF",$contents);
// Create and then destroy the image (discussed in steps 5 and 6)
imagepng($im);
imagedestroy($im);
?>
Now I have a small problem....
In my text file, there are no line breaks, meaning that the image created will "cut" some of the text.
Anybody has a solution for this?
Opening the content of text file, looking for "." add a return and save back?
s
ok this is working, here is my code:
<?php
$font_file = "ARIAL.TTF";
$font_size = 8;
$text = "test.txt";
$fp = fopen($text, "r");
$contents = fread($fp, filesize($text));
// Create the image resource and assign colors to variables (discussed in steps 1 and 5)
header("Content-type: image/png");
$im = ImageCreate (760, 520) or die ("Cannot Create image");
$red = imagecolorallocate($im, 255, 255, 255);
$txt_color = ImageColorAllocate ($im, 0, 0, 0);
ImageTTFText ($im, 9, 0, 5, 20, $txt_color, "ARIAL.TTF",$contents);
// Create and then destroy the image (discussed in steps 5 and 6)
imagepng($im);
imagedestroy($im);
?>
Now I have a small problem....
In my text file, there are no line breaks, meaning that the image created will "cut" some of the text.
Anybody has a solution for this?
Opening the content of text file, looking for "." add a return and save back?
s
rehfeld...cool thanks, i am really getting there..
now an other problem, I need top wrap the text after a certain number of characters....
New lines are working, but some sentences are longer than the width of the image, meaning that the text is still cut.
Counting the number of charactyer and inserting a "\r\n"?
s
now an other problem, I need top wrap the text after a certain number of characters....
New lines are working, but some sentences are longer than the width of the image, meaning that the text is still cut.
Counting the number of charactyer and inserting a "\r\n"?
s
thanks all..
working now....
here's the code if people need...
<?php
$font_file = "ARIAL.TTF";
$font_size = 8;
$text = "test.txt";
$fp = fopen($text, "r");
$contents = fread($fp, filesize($text));
$new_text = str_replace ('\n', "", $contents);
$new_text2 = str_replace ('. ', ".\r\n", $new_text);
$new_text3 = wordwrap($new_text2, 130, "\r\n");
// Create the image resource and assign colors to variables (discussed in steps 1 and 5)
header("Content-type: image/png");
$im = ImageCreate (720, 576) or die ("Cannot Create image");
$red = imagecolorallocate($im, 255, 255, 255);
$txt_color = ImageColorAllocate ($im, 0, 0, 0);
//(font size,rotation,align left,align top
ImageTTFText ($im, 8, 0, 5, 15, $txt_color, "ARIAL.TTF",$new_text3);
// Create and then destroy the image (discussed in steps 5 and 6)
imagepng($im);
imagedestroy($im);
?>
working now....
here's the code if people need...
<?php
$font_file = "ARIAL.TTF";
$font_size = 8;
$text = "test.txt";
$fp = fopen($text, "r");
$contents = fread($fp, filesize($text));
$new_text = str_replace ('\n', "", $contents);
$new_text2 = str_replace ('. ', ".\r\n", $new_text);
$new_text3 = wordwrap($new_text2, 130, "\r\n");
// Create the image resource and assign colors to variables (discussed in steps 1 and 5)
header("Content-type: image/png");
$im = ImageCreate (720, 576) or die ("Cannot Create image");
$red = imagecolorallocate($im, 255, 255, 255);
$txt_color = ImageColorAllocate ($im, 0, 0, 0);
//(font size,rotation,align left,align top
ImageTTFText ($im, 8, 0, 5, 15, $txt_color, "ARIAL.TTF",$new_text3);
// Create and then destroy the image (discussed in steps 5 and 6)
imagepng($im);
imagedestroy($im);
?>