Page 1 of 2
how to create text on php generated image?
Posted: Tue Apr 11, 2006 12:00 am
by lanny
may i know how to create text which input by user from an xhtml page into php generated image?
thanks in advance.
Posted: Tue Apr 11, 2006 12:36 am
by feyd
Posted: Tue Apr 11, 2006 6:37 am
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
Posted: Tue Apr 11, 2006 10:10 pm
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.
Posted: Sat Apr 22, 2006 11:32 am
by lanny
but i got problem wif gd libraries..cannot call imagecreate() function..
Posted: Sat Apr 22, 2006 12:29 pm
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?
Posted: Sat Apr 22, 2006 1:07 pm
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!
check for newlines
Posted: Sat Apr 22, 2006 1:20 pm
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.
Posted: Sat Apr 22, 2006 1:46 pm
by lanny
but the code for line 8 is "<style type="text/css">"
how to solve it?
Posted: Sat Apr 22, 2006 2:16 pm
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.
re
Posted: Sat Apr 22, 2006 2:21 pm
by Pineriver
feyd | Please use 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
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]
Posted: Sat Apr 22, 2006 8:51 pm
by lanny
but i want the php generated image displayed on another image in the xhtml page..how can solve it/
thanks in advance
Posted: Sat Apr 22, 2006 9:48 pm
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..
Posted: Sat Apr 22, 2006 10:08 pm
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!
Posted: Sat Apr 22, 2006 10:17 pm
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.