Page 1 of 1
Text on image
Posted: Fri Dec 24, 2004 7:08 am
by snicolas
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
Posted: Fri Dec 24, 2004 7:49 am
by n00b Saibot
u can see this forum's article
viewtopic.php?t=18485
for a very good eg.
Bye 4 now!
Posted: Fri Dec 24, 2004 8:21 am
by onion2k
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.
Posted: Fri Dec 24, 2004 9:21 am
by snicolas
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
Posted: Fri Dec 24, 2004 9:28 am
by snicolas
Sorry, fixed!
the font name was ARIAL.ttf and on my server it was ARIAL.TTF...thanks guys...
Posted: Fri Dec 24, 2004 9:40 am
by snicolas
again...sorry...
How do I open a text file and display the content of this file in th newly created image?
s
Posted: Fri Dec 24, 2004 9:48 am
by snicolas
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
Posted: Fri Dec 24, 2004 9:49 am
by rehfeld
file_get_contents() or fread() to get the text into a string
after you do that its the same as normal.
you could use wordwrap() to solve you problem of not having new lines
if you want the new lines to be right after each period, the use
$new_text = str_replace ('.', ".\r\n", $text);
Posted: Fri Dec 24, 2004 10:00 am
by snicolas
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
Posted: Fri Dec 24, 2004 10:16 am
by rehfeld
wordwrap()
Posted: Fri Dec 24, 2004 10:56 am
by snicolas
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);
?>