Jump to content

Archived

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

Tjk

*SOLVED* Image generation problem

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I still haven't been able to get this one to work. Can anyone help?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
That fixed it! Thanks Barand.

One final question.. could I simply incorporate the code from myimage.php into my main page?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Change the signs when calculating y coords of square

$lcoordx= $unitx- 2;
$lcoordy= $unity - 2;

$rcoordx= $unitx + 2;
$rcoordy= $unity + 2;

Share this post


Link to post
Share on other sites
Thanks for all your help Barand! **SOLVED**

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.