how to create text on php generated image?

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

how to create text on php generated image?

Post by lanny »

may i know how to create text which input by user from an xhtml page into php generated image?
thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Code: Select all

<?php
if($_POST['text2write']){
$im = imagecreate(100, 30);
// white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 5, 0, 0, $_POST['text2write'], $textcolor);
header("Content-type: image/png");
imagepng($im);
} else {
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
Text to write on image: <input name="text2write" value="" type="text" /><br />
<input type="submit" name="submit" value="Write It!" />
</form>
<?php
}
?>
Should do exactly what you want
lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

Post by lanny »

May i know why i cant load the code above?
Error msg : Parse error: parse error, unexpected T_IF in C:\Web Docs\Assignment\text2write.php on line 4

Thank you.
lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

Post by lanny »

but i got problem wif gd libraries..cannot call imagecreate() function..
User avatar
themurph
Forum Commoner
Posts: 76
Joined: Wed Apr 19, 2006 1:56 pm
Contact:

Post by themurph »

check the output of

print_r(get_loaded_extensions());

You probably do not have the gd extension loaded

Are you using windows php distro?
lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

Post by lanny »

i have checked in my command prompt by typing php -m
the list of php modules shows i have gd included.

but now another problem arise is, another error message is diplayed which is as below:
"Warning: Cannot modify header information - headers already sent by (output started at C:\Web Docs\Assignment\Designshirt.php:8) in C:\Web Docs\Assignment\Designshirt.php on line 231
‰PNG "

where the line 231 is " header("Content-type: image/png");"

any solutions for it?thanks in advance!
Pineriver
Forum Commoner
Posts: 50
Joined: Fri Aug 15, 2003 5:24 pm

check for newlines

Post by Pineriver »

Check that you dont have blank lines at the top of your script, also check that only the image code is being executed when you are calling the image.
lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

Post by lanny »

but the code for line 8 is "<style type="text/css">"
how to solve it?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Either you generate (x)html with tags in and use the content-type: text/html OR
Generate an image with imagedata and the content-type: imagemimetype...
But not a combination of both.
Pineriver
Forum Commoner
Posts: 50
Joined: Fri Aug 15, 2003 5:24 pm

re

Post by Pineriver »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


best thing would be to do is put the image code on top and use exit()

Code: Select all

<?php 
if($_POST['text2write']){ 
$im = imagecreate(100, 30); 
// white background and blue text 
$bg = imagecolorallocate($im, 255, 255, 255); 
$textcolor = imagecolorallocate($im, 0, 0, 255); 
imagestring($im, 5, 0, 0, $_POST['text2write'], $textcolor); 
header("Content-type: image/png"); 
imagepng($im); 
exit();
} 
?>
HTML CODE HERE
There must be nothing outputted to the browser before you generate the image


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

Post by lanny »

but i want the php generated image displayed on another image in the xhtml page..how can solve it/
thanks in advance
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

save it somewhere.. and in your html page you add an <img tag that points to that file...
Or generate it on the fly, and add an <img tag that points to your script that generates it..
lanny
Forum Newbie
Posts: 24
Joined: Thu Apr 06, 2006 10:09 am

Post by lanny »

but the php generated image 's text is user input....where user will enter the text on a textbox n press submit on a xhtml page..then the text with php generated image will be loaded on a 'shirt image' at the xhtml page...any solutions for that?
thanks in advance!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

timvw wrote: Or generate it on the fly, and add an <img tag that points to your script that generates it..
Since you didn't quite get it, try doing something like

Code: Select all

echo '<img src="tshirtimage.php?text='.$_POST['tshirt_text'].'">';

Either pass the text to the image through the image tag, or have the script read the content directly. I would do some validating first before passing it though. To read the image as an image source, use readfile(). The user comments should be more than helpful.
Post Reply