I have looked everywhere for an answer to this and am constantly frustrated in the process! Is there anyone who can help please?

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.

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 following ‘myscript2.php’ file creates an image BUT I can’t get that image to load using the img tag:

<img src=”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);
?>

Surely there is a way to do this but I am unable to uncover it.

Thanks

    Welcome to PHPBuilder! When posting PHP or HTML code, please use the board's [noparse]

    ..

    or

    ..

    [/noparse] bbcode tags (respectively) as they make your code much easier to read and analyze.

    Aden1;10980246 wrote:

    The following ‘myscript2.php’ file creates an image

    Have you verified that it does this successfully, e.g. by visiting this PHP script directly and seeing the image in your browser? If not, how do you know that it indeed "creates an image" as expected?

    Aden1;10980246 wrote:

    BUT I can’t get that image to load using the img tag:

    <img src=”myscript2.php”/>

    Is that the exact HTML code you used? If so, note how the syntax highlighting is all wrong (something your code editor should be doing for you...); you've used curly quotes (”) instead of regular ASCII double quotes (").

      Thanks so much for getting back so quickly and sorry for not using the site properly. I hadn't realised and will do so in future.

      To answer your questions: myscript2.php does output an image to the browser. However I need to be able to use that image in an image tag in another script. It is that last part that doesn't work.

      When I use <img src=" script2.php" /> in another script all I get is a placeholder.

      I hope that clarifies things for you.

      Also, My "" error came from the fact that I had typed up my question in a word document to get it as clear as I could, hence the problem.

      I would be so grateful if you could help me.

      Aden1

        is myscript2.php in the same relative folder as the file accessed by the http request? ie if index.html in wwwroot, is the php also in wwwroot? if not it doesn't know where the script is. I personally use defined paths for images... IE <img src="www.example.com/resources/scripts/myscript2.php" />

          yes, all the script are in the same folder as witnessed by the fact that my html file recognises it so that the image can be output to the browser.

          Thanks

          Aden1

            I thought I had found the answer here: http://php.net/manual/en/image.examples-png.php.

            However I don't understand how this is supposed to work. Can somebody tell me, please, what I need to put into the second 'text' in the code to pick up the variable input? As it stands I get the button with 'text' on it, but what I want is the value of $string.

            Thanks

            Aden1

              I think you are referring to the second text in the img src yes? If so thats the string you want to appear on the button.

                Yes I am but how do I get the content of the variable in there? Putting $string in there doesn't work. In the link I provided the writer says to use <img src="button.php?text=text"> to change the text on the button. That second 'text' is input as a variable.

                However that link doesn't explain how to get that variable in there in place of 'text'.

                Thank you once again

                  Write a Reply...