Jump to content


Photo

*SOLVED* Image generation problem


  • Please log in to reply
9 replies to this topic

#1 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 29 May 2006 - 11:36 AM

I'm having a real problem getting an image creation script to work...

   <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>

Could anyone point out where I'm going wrong on this? I have absolutely no idea why it won't work!

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 May 2006 - 12:05 PM

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?
Legend has it that reading the manual never killed anyone.
My site

#3 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 29 May 2006 - 02:36 PM

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.

#4 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 31 May 2006 - 08:26 PM

I still haven't been able to get this one to work. Can anyone help?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 31 May 2006 - 11:12 PM

Store this in "myimage.php"
(I added comments where I corrected your code)

::myimage.php::
<?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 
?>

In your main page
   <form method="POST" action="imgoutput.php">
        <img src="myimage.php">
  </form>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 02 June 2006 - 07:48 PM

That fixed it! Thanks Barand.

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

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 02 June 2006 - 08:26 PM

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 ::
<?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
?>

... and place onpage with

<IMG src='myimage.php?bg=200'>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 04 June 2006 - 02:50 PM

I managed to get that all working barand but here's my final problem in the myimage.php file...

  $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);

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?

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 04 June 2006 - 06:28 PM

Change the signs when calculating y coords of square

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

$rcoordx= $unitx + 2;
$rcoordy= $unity + 2;
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 05 June 2006 - 01:27 PM

Thanks for all your help Barand! **SOLVED**




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users