Page 1 of 1
[Solved] Drawing a box with GD
Posted: Fri Aug 12, 2005 9:09 am
by Grim...
I've just had a look at the list of image functions, but none of them seems to be the one I want.
Here's the deal: I've got four XY co-ordinates, and I want to draw four lines (a box) onto another image.
If someone could point me at the right function(s) to use, that would be her-wicked!
Posted: Fri Aug 12, 2005 9:10 am
by Grim...
imageline() !

Posted: Fri Aug 12, 2005 9:20 am
by feyd
Moved to ......
Graphics! dun-dun-duuuun......

Posted: Fri Aug 12, 2005 9:31 am
by Grim...
Hmm... I keep getting this error. The image I am using is called 'test2.jpg'.
Code: Select all
Warning: imageline(): supplied argument is not a valid Image resource in /var/www/cptmatt/colleague/test2.php on line 8
Code: Select all
function drawrectangle($image, $tlx, $tly, $brx, $bry)
{
$color = imagecolorallocate($image, 0, 0, 0);
//top line
imageline($image, $tlx, $tly, $brx, $tly, $color);
//bottom line
imageline($image, $tlx, $bry, $brx, $bry, $color);
//left line
imageline($image, $tlx, $tly, $tlx, $bry, $color);
//right line
imageline($image, $brx, $tly, $brx, $bry, $color);
return;
}
Also, is there an better way to set the line color ($color) to black?
Posted: Fri Aug 12, 2005 9:34 am
by feyd
did you remember to createimagefromjpeg() ?
Posted: Fri Aug 12, 2005 9:44 am
by Grim...
Tried that - now I get no error, but no 'boxtest2.jpg' either...
Code: Select all
function drawrectangle($image, $tlx, $tly, $brx, $bry)
{
$im = imagecreatefromjpeg($image);
$color = imagecolorallocate($im, 0, 0, 0);
imageline($im, $tlx, $tly, $brx, $tly, $color);
imageline($im, $tlx, $bry, $brx, $bry, $color);
imageline($im, $tlx, $tly, $tlx, $bry, $color);
imageline($im, $brx, $tly, $brx, $bry, $color);
$dest ="box".$image;
$w = imagesx($im);
$h = imagesy($im);
$dest = imageCreate($w, $h);
imagecopy($dest, $im, 0, 0, 0, 0, $w, $h);
}
Posted: Fri Aug 12, 2005 9:46 am
by feyd
you aren't outputting the results.
imagejpeg()
remember to do
imagedestroy() on those image resources (memory leaks in GD)
Posted: Fri Aug 12, 2005 9:49 am
by Grim...
Code: Select all
Warning: imagejpeg(): Unable to open 'Resource id #4' for writing in colleague/test2.php on line 23
Code: Select all
function drawrectangle($image, $tlx, $tly, $brx, $bry)
{
$im = imagecreatefromjpeg($image);
$color = imagecolorallocate($im, 0, 0, 0);
imageline($im, $tlx, $tly, $brx, $tly, $color);
imageline($im, $tlx, $bry, $brx, $bry, $color);
imageline($im, $tlx, $tly, $tlx, $bry, $color);
imageline($im, $brx, $tly, $brx, $bry, $color);
$dest ="box".$image;
$w = imagesx($im);
$h = imagesy($im);
$dest = imageCreate($w, $h);
imagecopy($dest, $im, 0, 0, 0, 0, $w, $h);
imagejpeg($im, $dest, 100);
imagedestroy($im);
}
Posted: Fri Aug 12, 2005 9:51 am
by Grim...
Wait, I've got $dest twice...
Now I get:
Code: Select all
Warning: imagejpeg(): Unable to open 'boxtest2.jpg' for writing in colleague/test2.php on line 19
Code: Select all
function drawrectangle($image, $tlx, $tly, $brx, $bry)
{
$im = imagecreatefromjpeg($image);
$color = imagecolorallocate($im, 0, 0, 0);
imageline($im, $tlx, $tly, $brx, $tly, $color);
imageline($im, $tlx, $bry, $brx, $bry, $color);
imageline($im, $tlx, $tly, $tlx, $bry, $color);
imageline($im, $brx, $tly, $brx, $bry, $color);
$filename ="box".$image;
$w = imagesx($im);
$h = imagesy($im);
$dest = imageCreate($w, $h);
imagecopy($dest, $im, 0, 0, 0, 0, $w, $h);
imagejpeg($dest, $filename, 100);
imagedestroy($im);
}
Posted: Fri Aug 12, 2005 9:52 am
by feyd
the second argument is a path/filename, not a resource. The first argument is which image resource you want to write.. I think you are dealing with $dest at that point.
you'll need to imagedestroy that one too

Posted: Fri Aug 12, 2005 9:54 am
by feyd
potential permissions issue with an existing file, or the folder maybe?
Posted: Fri Aug 12, 2005 9:57 am
by Grim...
Yup, I've just changed it. Hurrah!
Feyd == genius
Thanks mate