Jump to content

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!
Link to comment
https://forums.phpfreaks.com/topic/10699-solved-image-generation-problem/
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?
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.
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]
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]
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?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.