Tjk Posted May 29, 2006 Share Posted May 29, 2006 I'm having a real problem getting an image creation script to work...[code] <form method="POST" action="imgoutput.php"><? //define png header("Content-type: image/png"); $imgWidth= 300; $imgHeight= 300; //create image $im= imagecreate($imgWidth, $imgHeight); $colorBlack= imagecolorallocate($im, 0, 0, 0); //create grid lines for($i=1; $i > 11; $i++){ imageline($im, $i*30, 0, $i*30, 300, $colorBlack); imageline($im, 0, $i*30, 300, $i*30, $colorBlack); } //output png imgpng($im); imagedestroy($image); ?> </form>[/code]Could anyone point out where I'm going wrong on this? I have absolutely no idea why it won't work! Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/ Share on other sites More sharing options...
AndyB Posted May 29, 2006 Share Posted May 29, 2006 Please define "won't work".Perhaps you're getting a 'cannot send header message' because there output to the browser (your HTML) before the header? Perhaps 'nothing' happens because that's what I'd expect with a form without any 'submit'.What are you trying to do? Make your own captcha? Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-39918 Share on other sites More sharing options...
Tjk Posted May 29, 2006 Author Share Posted May 29, 2006 Sorry, should've been more specific. I've tried the submit button idea to reload the page and it still doesn't work. By that i mean that when the page reloads anything below where the script is placed doesn't load (doesn't show on view page source either) and the image that should be created doesn't load. There is no error message at all.What I'm trying to do is create an image with a grid on it. The idea that eventually when someone clicks on it it will reload the page and display the x and y coordinates that were clicked on in the image. Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-39946 Share on other sites More sharing options...
Tjk Posted May 31, 2006 Author Share Posted May 31, 2006 I still haven't been able to get this one to work. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-40689 Share on other sites More sharing options...
Barand Posted May 31, 2006 Share Posted May 31, 2006 Store this in "myimage.php"(I added comments where I corrected your code)::myimage.php::[code]<?php//define png header("Content-type: image/png"); $imgWidth= 300; $imgHeight= 300; //create image $im = imagecreate($imgWidth, $imgHeight); ## define bg color first. Black lines on black bg not very clear $colorWhite = imagecolorallocate($im, 0xff, 0xff, 0xff); $colorBlack = imagecolorallocate($im, 0, 0, 0); //create grid lines for($i=1; $i < 11; $i++){ ## $i < 11, not $i > 11 imageline($im, $i*30, 0, $i*30, 300, $colorBlack); imageline($im, 0, $i*30, 300, $i*30, $colorBlack); } //output png imagepng($im); ## imagepng(), not imgpng() imagedestroy($im); ## your image is $im, not $image ?>[/code]In your main page[code] <form method="POST" action="imgoutput.php"> <img src="myimage.php"> </form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-40727 Share on other sites More sharing options...
Tjk Posted June 2, 2006 Author Share Posted June 2, 2006 That fixed it! Thanks Barand.One final question.. could I simply incorporate the code from myimage.php into my main page? Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-41289 Share on other sites More sharing options...
Barand Posted June 2, 2006 Share Posted June 2, 2006 You are creating a png file on-the-fly so place it on the page with an IMG tag as you would a normal png file.If you want control over the image you are creating, pass parameters in the query string. So if you want to vary the shade of grey of the grid background:: myimage.php ::[code]<?php // get bg color from the query string if (isset($_GET['bg'])) { $bg = $_GET['bg']; } else { $bg = 0xFF; } //define png header("Content-type: image/png"); $imgWidth= 300; $imgHeight= 300; //create image $im = imagecreate($imgWidth, $imgHeight); ## define bg color first. Black lines on black bg not very clear $colorWhite = imagecolorallocate($im, $bg, $bg, $bg); $colorBlack = imagecolorallocate($im, 0, 0, 0); //create grid lines for($i=1; $i < 11; $i++){ ## $i < 11, not $i > 11 imageline($im, $i*30, 0, $i*30, 300, $colorBlack); imageline($im, 0, $i*30, 300, $i*30, $colorBlack); } //output png imagepng($im); ## imagepng(), not imgpng() imagedestroy($im); ## your image is $im, not $image?>[/code]... and place onpage with[code]<IMG src='myimage.php?bg=200'>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-41299 Share on other sites More sharing options...
Tjk Posted June 4, 2006 Author Share Posted June 4, 2006 I managed to get that all working barand but here's my final problem in the myimage.php file...[code] $im= imagecreatefrompng('maptemp.png'); mysql_connect("localhost", "user", "passwprd")or die(mysql_error()); mysql_select_db("simdoma_hermes"); $query= mysql_query("SELECT * from battles WHERE unit='1'")or die(mysql_error()); while($row= mysql_fetch_array($query)){ $unit= $row['unit']; $unitx= $row['xcoord']; $unity= $row['ycoord']; } $lcoordx= $unitx- 2; $lcoordy= $unity + 2; $rcoordx= $unitx + 2; $rcoordy= $unity - 2; $color= imagecolorallocate($im, 0xFF, 0xFF, 0xFF); imagefilledrectangle($im, $lcoordx, $lcoordy, $rcoordx, $rcoordy, $color); imagepng($im);[/code]I'm looking to take the coordinates of a unit from the DB then create a square 2 pixels each side of the unit's recorded coordinates in the database and draw a red rectangle on the map and then output it.When I try this the map outputs but there is no rectangle on it. Is it something wrong with my maths? Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-41723 Share on other sites More sharing options...
Barand Posted June 4, 2006 Share Posted June 4, 2006 Change the signs when calculating y coords of square $lcoordx= $unitx- 2; $lcoordy= $unity - 2; $rcoordx= $unitx + 2; $rcoordy= $unity + 2; Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-41808 Share on other sites More sharing options...
Tjk Posted June 5, 2006 Author Share Posted June 5, 2006 Thanks for all your help Barand! **SOLVED** Quote Link to comment https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/#findComment-42009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.