Text on image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Text on image

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

u can see this forum's article

viewtopic.php?t=18485

for a very good eg.

Bye 4 now!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

Sorry, fixed!
the font name was ARIAL.ttf and on my server it was ARIAL.TTF...thanks guys...
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

again...sorry...

How do I open a text file and display the content of this file in th newly created image?

s
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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);
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

wordwrap()
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post 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);
?>
Post Reply