Use of img tag to capture image created from user input

images php coding issues or problems here.

Moderators: macek, egami, gesf

Aden
New php-forum User
New php-forum User
Posts: 1
Joined: Mon May 16, 2011 2:46 am

Use of img tag to capture image created from user input

Postby Aden » Mon May 16, 2011 2:53 am

I have the following html file to gather comments from users:

<html>
<head><title>myscripthtml</title>
<script type="text/javascript">
function getComments()
{
return true;
}
</script>
</head>
<body>
Provide comments
<form name="cmt" action="myscript2.php" method="GET" onSubmit="return getComments()"/>
<input type="text" name="cmment"/>
<input type="submit" value="submit comments"/>
</form>
</body>
</html>

The ‘myscript2.php’ file is set out below and creates an image BUT I can’t get that image to load using the img tag: <img src=”myscript2.php”/>.

I understood that having created an image with php it was possible to load it using the img tag and it certainly works where the comment is hard coded. But as soon as I get the comment from input from the user all I get is a placeholder instead of the image.


Here is how I try to load the image using an img tag:

<html>
<body>
<img src="myscript2.php"/>
</body>
</html>

and here is myscript2.php:
<?php
// Path to our font file
$font = 'arial.ttf';
$fontsize = 10;
$quotes = $_GET['cmment'];
// get the quote and word wrap it
$quote = wordwrap($quotes,20,"<br />\n");
// Create image
$image = imagecreatefrompng('baseimage.png');
// pick color for the background
$bgcolor = imagecolorallocate($image, 100, 100, 100);
// pick color for the text
$fontcolor = imagecolorallocate($image, 255, 255, 255);
// fill in the background with the background color
imagefilledrectangle($image, 200, 100, 400, 200, $bgcolor);
// x,y coords for imagettftext defines the baseline of the text: the lower-left corner
// so the x coord can stay as 0 but you have to add the font size to the y to simulate
// top left boundary so we can write the text within the boundary of the image
$x = 0;
$y = $fontsize;
imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $quote);
// tell the browser that the content is an image
header('Content-type: image/png');
// output image to the browser
imagepng($image);
// delete the image resource
imagedestroy($image);
?>

Can anyone advise me how to load such an image?

Many thanks
Aden

Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest