GD help...

M

Mr. 47

Guest
Code:
<?PHP

Header("Content-type: image/png");

$im = ImageCreateFromPNG("map.png");
$red = ImageColorAllocate($im, 255, 0, 0);

$trX = $map.x + "2";
$tlX = $map.x - "2";
$brX = $map.x + "2";
$blX = $map.x - "2";

$trY = $map.y - "2";
$tlY = $map.y - "2";
$brY = $map.y + "2";
$blY = $map.y + "2";

ImageLine($im, $trX, $trY, $blX, $blY, $red);
ImageLine($im, $tlX, $tlY, $brX, $brY, $red);

ImagePNG($im);

?>

Please help, what is wrong with this script, its useing the newest GD Libary so it could be the CreateFromGIF that did it, please help.

And also is there any way to save the image...

Edit - Just editted to turn the PHP tags into CODE tags, since PHP merges with the BarrysWorld colour scheme - Jonty
 
J

Jonty

Guest
PHP Manual
Versions of GD older than gd-1.6 support GIF format images, and do not support PNG, where versions greater than gd-1.6 support PNG, not GIF.
Due to royalty issues, mainly, GIF support is pretty much out nowadays.

As for the code, try this . . .

Code:
<?php
Header("Content-type: image/png");

$im = @ImageCreateFromPNG("map.png"); // EDIT - '@' Prefix added
$red = ImageColorAllocate($im, 255, 0, 0);

$trX = $map.x + "2";
$tlX = $map.x - "2";
$brX = $map.x + "2";
$blX = $map.x - "2";

$trY = $map.y - "2";
$tlY = $map.y - "2";
$brY = $map.y + "2";
$blY = $map.y + "2";

ImageLine($im, $trX, $trY, $blX, $blY, $red);
ImageLine($im, $tlX, $tlY, $brX, $brY, $red);

ImagePNG($im);
ImageDestroy($im); // EDIT - Resources should be freed on completion
?>
The code you have written seems to be almost perfect, just a couple of things I noticed (marked with the // EDIT ... comments). Please do post again if this does not work, for I haven't had chance to test it myself.

Finally, you can save the image in a variety of ways, the most common of which is to actually export the code into a preexisting image file. But it all depends on the context, so feel free to post the specifics :)

Kind Regards
 
M

Mr. 47

Guest
it still wont work...

and I want to save as a differnt file name ie. map2.png

I also want this saved version to have all the clicks stored on it as a poll results kinda thing...

I want people to see were they clicked and after clicking another link were everyone has clicked...
 
J

Jonty

Guest
Hi Mr. 47

You'll have to forgive me, but I don't quite understand what those last two sentences in your post mean.

Anyway, to output to a specific file, simply add the filename to the ImagePNG function; e.g.
Code:
ImagePNG ($im,"map2.png");
Unfortunately, I can't quite remember whether you have to create a blank file their first. If it fails to work, simply save a blank Notepad document and change the extention to '.png'. Upload the file, CHMOD it to 777 (and any directories between it and the script (if applicable)).

Alternatively, if the file this code is in is just for the image, simply target it as the source of an image in your results page; e.g.
Code:
[img]map2.php[/img]

As for the code, I've had a little play and it appears to work fine. I believe the problem could occur either in the data being sent to the function, or, perhaps more likely, when trying to create the image from the original PNG. To test for the latter, try adding this code as suggested in the PHP manual . . .
Code:
<?php
Header("Content-type: image/png");

$im = @ImageCreateFromPNG("map.png");

// <newCode>
if (!$im)
{
  // If the open failed, create a new image with an error message
  $im  = imagecreate (150, 30);
  $bgc = imagecolorallocate ($im, 255, 255, 255);
  $tc  = imagecolorallocate ($im, 0, 0, 0);
  imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
  imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
exit;
// </newCode>

$red = ImageColorAllocate($im, 255, 0, 0);

$trX = $map.x + "2";
$tlX = $map.x - "2";
$brX = $map.x + "2";
$blX = $map.x - "2";

$trY = $map.y - "2";
$tlY = $map.y - "2";
$brY = $map.y + "2";
$blY = $map.y + "2";

ImageLine($im, $trX, $trY, $blX, $blY, $red);
ImageLine($im, $tlX, $tlY, $brX, $brY, $red);

ImagePNG($im);
ImageDestroy($im);
?>
Hope this helps
 
M

Mr. 47

Guest
Code:
<?php

Header("Content-type: image/png");

$im = ImageCreateFromPNG("map.png");
$red = ImageColorAllocate($im, 255, 0, 0);

$x = $map.x;
$y = $map.y;

@ImageFilledEllipse($im, $x, $y, 10, 10, $red);

ImagePNG($im);
ImageDestroy($im);

?>

This is my new script, it still don't work...
 
J

Jonty

Guest
Hello again

Firstly, get rid of the periods '.' in your variable names (i.e. $mapx, $mapy instead of $map.x, $map.y). This will otherwise cause errors :)

Secondly, try a different function. I managed to get the above code to work, but only by changing the variable names and by using a function other than the ecclipse one (imageline, for example). Quite what's wrong with imagefilledecllipse, I don't know, but it wasn't working for me.

Kind Regards
 
M

Mr. 47

Guest
i got it to work...

i had to remove the name tag fom the HTML form image so it sent the variables as $x and $y as opposed to $map.x and $map.y

it would apperar that filledellipse is not supported in my versoin of GD

i had to use
Code:
imagearc($im, $x, $y, 5, 5, 0, 360, $red);
imagefill($im, $x, $y, $red);
look here!!!

i now have a new problem, however

when i try to edit another image in the same script it won't work...

Code:
<?PHP

header("Content-type: image/png");

$im = imagecreatefrompng ("map.png"); 
$red = imagecolorallocate($im, 255, 0, 0);

imagearc($im, $x, $y, 5, 5, 0, 360, $red);
imagefill($im, $x, $y, $red);

imagepng($im);
imagedestroy($im);

//------------------------------------//

$im2 = imagecreatefrompng ("map2.png"); 
$red = imagecolorallocate($im2, 255, 0, 0);

imagearc($im2, $x, $y, 5, 5, 0, 360, $red);
imagefill($im2, $x, $y, $red);

imagepng($im2, "map2.png");
imagedestroy($im2);

?>

I want this image to be seppaerate from the other.

this image needs to store all the clicks made on the first image, permanantly.

ie. when Fred clicks on the map at a certan location he sees a red dot were he clicked.

but when he clicks "see where everone else clicked" he sees all the same map (differnt pic) with loads of red dots on it...(including his...)
 
M

Mr. 47

Guest
fixed it, thus:
Code:
<?PHP

$im2 = imagecreatefrompng ("map2.png"); 
$red = imagecolorallocate($im2, 255, 0, 0);

imagearc($im2, $x, $y, 5, 5, 0, 360, $red);
imagefill($im2, $x, $y, $red);

imagepng($im2, "map2.png");
imagedestroy($im2);

//-------------------

header("Content-type: image/png");

$im = imagecreatefrompng ("map.png"); 
$red = imagecolorallocate($im, 255, 0, 0);

imagearc($im, $x, $y, 5, 5, 0, 360, $red);
imagefill($im, $x, $y, $red);

imagepng($im);
imagedestroy($im);

?>
 

Users who are viewing this thread

Top Bottom