Jump to content

Action Button in PHP?


OtagoHarbour

Recommended Posts

In order to display a PNG image with PHP I use the following code. $im = imagecreatefrompng($OutputFileName); header('Content-Type: image/png'); imagepng($im); imagedestroy($im);It does not work if I include any html in the PHP file. If I avoid the html then it displays the PNG file but I would like a button for the user to click on to go to a different page. I tried the following $im = imagecreatefrompng($OutputFileName); header('Content-Type: image/png'); imagepng($im); imagedestroy($im); ?> <html> <body> echo "<button onclick='header(\"Location: http://localhost/SomeOtherPage.php\&q...'>Return to Menu</button>"; </body> </html> <?phpbut I still just see the image with no button. I thought maybe the button was at the top of the page and was covered by the image so I tried adding echo "<br \><br \><br \><br \><br \>";before $im = imagecreatefrompng($OutputFileName);However, when I did that, nothing showed up. No image and no button.I would be most grateful for a solution to this problem.Thanks,Peter.

Link to comment
Share on other sites

you only need to post one thread per topic. have you looked at the return values of imagecreatefrompng? what do imagepng and imagedestroy do?

Link to comment
Share on other sites

You can not output anything with the image itself, cause it will broke the image file or will not show the rest of the part.cause It cant determine the other things after the image as html content and you can not put anything before the image which break the image.if you want to use dynamic image. you should create one page for image genrating which will evalute only the image. and you can use that by using the url of the file wit 'img' tagor any other tags where image is applicable.eg,<img src='somepage.php' />where somepage.php will genrate the image. the page will be called via get and a corealted image will be displayed.

Link to comment
Share on other sites

$im = imagecreatefrompng($OutputFileName);header('Content-Type: image/png');	imagepng($im);imagedestroy($im);

Setting 'Content-Type: image/png' means this is no longer a php page but a png image file (even if the page ends in .php) and it will be treated as an image file. So no text can be added to the page and the image will always appear in the top left corner.

<?php$pathToFile = "path/to/image.png";$im = imagecreatefrompng($OutputFileName);imagepng($im, $pathToFile);imagedestroy($im);?>

Also, you cannot write php code in HTML. PHP does not work like Javascript and it cannot be run after the page loads. You need to use javascript for this.

<?php$pathToFile = "path/to/image.png";$im = imagecreatefrompng($OutputFileName);imagepng($im, $pathToFile);imagedestroy($im);?><html><body><button id="mybutton" onclick='window.location='asdfasdfasdf.html''>Return to Menu</button></body></html>

And assuming you wanted this image to be the background of the button you would need to use CSS for this.

<style type='text/css'>#mybutton{background-color: transparent;border: 0px;background-image: url('<?php echo $pathToFile; ?>');}</style>

However I do not recommend generating images like this. Generating an image every time you access a page is unnecessarily taxing on the server. If the images will remain constant than you would be better off saving the path to a variable and just print the variable where you need it (if PHP is even required at all). If the image is not static then I would still recommend you only generate it when it changes and not at every page request.

Link to comment
Share on other sites

you only need to post one thread per topic. have you looked at the return values of imagecreatefrompng? what do imagepng and imagedestroy do?
Sorry. I was thinking that it was a different topic but is more closely related to my other thread than I had thought.
Link to comment
Share on other sites

You can not output anything with the image itself, cause it will broke the image file or will not show the rest of the part.cause It cant determine the other things after the image as html content and you can not put anything before the image which break the image.if you want to use dynamic image. you should create one page for image genrating which will evalute only the image. and you can use that by using the url of the file wit 'img' tagor any other tags where image is applicable.eg,<img src='somepage.php' />where somepage.php will genrate the image. the page will be called via get and a corealted image will be displayed.
I guess I should show the full code. What the full PHP file does is make an image from data in an ASCII file and display the result. The code is as follows.<?PHP session_start(); $GivenUserName=$_SESSION['UserName']; $SetName=$_SESSION['SetID']; $ArrayName=$_POST["ArrayID"]; $InputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".cel"; $OutputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".png"; if( !file_exists($OutputFileName) ) { $Executable="e:\\ExecutableDir\\Executable"; $Str=$Executable . " -i " . $InputFileName; echo $Str . "<br \>"; exec($Str); } $im = imagecreatefrompng($OutputFileName); imagepng($im); imagedestroy($im); ?> <html> <body> <button id="mybutton" onclick='window.location='f:\\LocalHostDir\\Menu.php''>Return to Menu</button> </body> </html> <?PHP?>This displays the image but not the button. I could display the image with a separate PHP file but I would like the user to be able to get back to the menu without having to use the back arrow of the browser and then refreshing the screen as is the case with the current setup. I would like a good way for the user to see the result of the processing and then go back to the menu to either select another ASCII file, a different processing function or to go back to a main menu.Thanks for your help,Peter.
Link to comment
Share on other sites

If your script outputs an image, then the only thing it can do is output the image. It tells the browser that everything that it is sending is binary image data, so the browser gets that plus a bunch of text HTML, which it also interprets as binary data, and it ends up corrupting the image. The script can either output an image, or output HTML, but it can't do both at the same time. Have the script output just the image, and nothing else (including whatever command you're running), and use a regular HTML img tag to put it on some other page:<img src="get_image.php">

Link to comment
Share on other sites

$im = imagecreatefrompng($OutputFileName);header('Content-Type: image/png');	imagepng($im);imagedestroy($im);

Setting 'Content-Type: image/png' means this is no longer a php page but a png image file (even if the page ends in .php) and it will be treated as an image file. So no text can be added to the page and the image will always appear in the top left corner.

Thanks very much. I did not know that. I don't want the image as a background. I would like to display the image with some navigation controls, either in the form of a menu of or action buttons.
<?php$pathToFile = "path/to/image.png";$im = imagecreatefrompng($OutputFileName);imagepng($im, $pathToFile);imagedestroy($im);?>

Also, you cannot write php code in HTML. PHP does not work like Javascript and it cannot be run after the page loads. You need to use javascript for this.

<?php$pathToFile = "path/to/image.png";$im = imagecreatefrompng($OutputFileName);imagepng($im, $pathToFile);imagedestroy($im);?><html><body><button id="mybutton" onclick='window.location='asdfasdfasdf.html''>Return to Menu</button></body></html>

I tried something like that, as in my post to birbal, but withimagepng($im, $pathToFile);I got the button but not the image. Withimagepng($im);I got the image without the button.Thanks,Peter.
Link to comment
Share on other sites

First this is actually a syntax error-

<button id="mybutton" onclick='window.location='asdfasdfasdf.html''>Return to Menu</button>

Should be

<button id="mybutton" onclick="window.location='asdfasdfasdf.html'">Return to Menu</button>

My mistake.Also, it's not magical. It doesn't know what you want just because you're thinking it. You haven't written it so it will become a button yet. That was where the CSS came in.

Link to comment
Share on other sites

If your script outputs an image, then the only thing it can do is output the image. It tells the browser that everything that it is sending is binary image data, so the browser gets that plus a bunch of text HTML, which it also interprets as binary data, and it ends up corrupting the image. The script can either output an image, or output HTML, but it can't do both at the same time. Have the script output just the image, and nothing else (including whatever command you're running), and use a regular HTML img tag to put it on some other page:<img src="get_image.php">
That seems to be the consensus. One solution may be to make the image a pop-up. Some users may have pop-ups disabled. Another may be to make it a separate tab. Would that work on any OS and browser? Another may be to write the image to part of the displayed page. Can you do that with something like Ajax.Thank you for your help,Peter.
Link to comment
Share on other sites

You can save the file to disk and then output an img tag with the filename of the new image, or the common way is to just use a regular img tag and point it to the PHP script that generates the image. You have one page with your HTML markup on it, and another script to output the image. You can send information through the query string if you want to:<img src="image.php?ArrayID=123">I don't know what the image is for and why you would need a popup or whatever else, but the thing to keep in mind is that if you have a PHP script which outputs an image, that's the only thing it can do. If it saves the image to disk it can still output HTML or whatever else, but if it's outputting any kind of binary data like an image then it can't send anything that is not part of the binary data.

Link to comment
Share on other sites

First this is actually a syntax error-
<button id="mybutton" onclick='window.location='asdfasdfasdf.html''>Return to Menu</button>

Should be

<button id="mybutton" onclick="window.location='asdfasdfasdf.html'">Return to Menu</button>

My mistake.Also, it's not magical. It doesn't know what you want just because you're thinking it. You haven't written it so it will become a button yet. That was where the CSS came in.

The problems seem to be different between Internet Explorer and FireFox. With FF, I get non-ASCII text, instead of an image, if I do not includeheader('Content-Type: image/png');But the button does show up. When I includeheader('Content-Type: image/png');I see the image properly but there is no button.With IE, I do not seem to needheader('Content-Type: image/png');to display the image but the button never shows up.Thanks again,Peter.
Link to comment
Share on other sites

I don't think you've understood that you cannot have the script send both image data and HTML data. This should be your image script:

<?PHP session_start(); $GivenUserName=$_SESSION['UserName']; $SetName=$_SESSION['SetID']; $ArrayName=$_POST["ArrayID"]; $InputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".cel"; $OutputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".png"; if( !file_exists($OutputFileName) ) { $Executable="e:\\ExecutableDir\\Executable"; $Str=$Executable . " -i " . $InputFileName; exec($Str); } $im = imagecreatefrompng($OutputFileName); imagepng($im); imagedestroy($im); ?>

Since that script uses $_POST, instead of having a form submit to that script you should have it look for that in $_GET instead of $_POST:

<?PHP session_start(); $GivenUserName=$_SESSION['UserName']; $SetName=$_SESSION['SetID']; $ArrayName=$_GET["ArrayID"]; $InputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".cel"; $OutputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".png"; if( !file_exists($OutputFileName) ) { $Executable="e:\\ExecutableDir\\Executable"; $Str=$Executable . " -i " . $InputFileName; exec($Str); } $im = imagecreatefrompng($OutputFileName); header('Content-Type: image/png'); imagepng($im); imagedestroy($im); ?>

So that's the image generation script. The content type header is required. That is the complete and entire script, there is no other code there. No HTML code, nothing else. That's it.Your other script can be where the form submits to. Your form processing script can get the ID from the submitted form, and add it to the URL to get the image.

<html><body><img src="get_image.php?ArrayID=<?php echo $_POST['ArrayID']; ?>"></body></html>

You can do whatever else you want with that image. Put it in a link tag, add onclick event, or whatever you want it to do. The point you seem to be missing is you need more than one script. The script that outputs the image can do nothing except output the image.

Link to comment
Share on other sites

You can save the file to disk and then output an img tag with the filename of the new image, or the common way is to just use a regular img tag and point it to the PHP script that generates the image. You have one page with your HTML markup on it, and another script to output the image. You can send information through the query string if you want to:<img src="image.php?ArrayID=123">I don't know what the image is for and why you would need a popup or whatever else, but the thing to keep in mind is that if you have a PHP script which outputs an image, that's the only thing it can do. If it saves the image to disk it can still output HTML or whatever else, but if it's outputting any kind of binary data like an image then it can't send anything that is not part of the binary data.
Thank you very much for your help. It took me a while to decide exactly how to go about it but this works for me.<html><body><?PHP session_start(); $GivenUserName=$_SESSION['UserName']; $SetName=$_SESSION['SetID']; $ArrayName=$_POST["ArrayID"]; $InputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".cel"; $OutputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".png"; if( !file_exists($OutputFileName) ) { $Executable="e:\\ExecutableDir\\Executable"; $Str=$Executable . " -i " . $InputFileName; echo $Str . "<br \>"; exec($Str); } $_SESSION['ImageToDisplay']=$OutputFileName; ?> <img src="DisplayImage.php?ArrayID=123"> <button id="mybutton" onclick="window.location='http://localhost/Menu.php'">Return to Menu</button> <?PHP?></body></html>DisplayImage.php has the following form.<?PHP session_start(); $OutputFileName=$_SESSION['ImageToDisplay']; $im = imagecreatefrompng($OutputFileName); header('Content-Type: image/png'); imagepng($im); imagedestroy($im);?>Thanks again for your help,Peter.
Link to comment
Share on other sites

I don't think you've understood that you cannot have the script send both image data and HTML data. This should be your image script:
<?PHP session_start(); $GivenUserName=$_SESSION['UserName']; $SetName=$_SESSION['SetID']; $ArrayName=$_POST["ArrayID"]; $InputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".cel"; $OutputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".png"; if( !file_exists($OutputFileName) ) { $Executable="e:\\ExecutableDir\\Executable"; $Str=$Executable . " -i " . $InputFileName; exec($Str); } $im = imagecreatefrompng($OutputFileName); imagepng($im); imagedestroy($im); ?>

Since that script uses $_POST, instead of having a form submit to that script you should have it look for that in $_GET instead of $_POST:

<?PHP session_start(); $GivenUserName=$_SESSION['UserName']; $SetName=$_SESSION['SetID']; $ArrayName=$_GET["ArrayID"]; $InputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".cel"; $OutputFileName="f:\\LocalHostDir\\" . $GivenUserName . "\\" . $SetName . "\\" . $ArrayName . ".png"; if( !file_exists($OutputFileName) ) { $Executable="e:\\ExecutableDir\\Executable"; $Str=$Executable . " -i " . $InputFileName; exec($Str); } $im = imagecreatefrompng($OutputFileName); header('Content-Type: image/png'); imagepng($im); imagedestroy($im); ?>

So that's the image generation script. The content type header is required. That is the complete and entire script, there is no other code there. No HTML code, nothing else. That's it.Your other script can be where the form submits to. Your form processing script can get the ID from the submitted form, and add it to the URL to get the image.

<html><body><img src="get_image.php?ArrayID=<?php echo $_POST['ArrayID']; ?>"></body></html>

You can do whatever else you want with that image. Put it in a link tag, add onclick event, or whatever you want it to do. The point you seem to be missing is you need more than one script. The script that outputs the image can do nothing except output the image.

Sorry I replied before I saw your message. I had thought a bit about the best way to approach it. The way you suggest may be better than the approach I took. The reason for the approach I took is that there are different options that the user may select and they determine which executable is called. There was an if-else block that I edited out since it was not germane to the thrust of the problem I was having. I will read your post more carefully but I thought I would get back with my current status.Thanks again,Peter.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...