[Solved] Drawing a box with GD
Moderator: General Moderators
[Solved] Drawing a box with GD
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!
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!
Last edited by Grim... on Fri Aug 12, 2005 10:04 am, edited 1 time in total.
Hmm... I keep getting this error. The image I am using is called 'test2.jpg'.
Also, is there an better way to set the line color ($color) to black?
Code: Select all
Warning: imageline(): supplied argument is not a valid Image resource in /var/www/cptmatt/colleague/test2.php on line 8Code: 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;
}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);
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
you aren't outputting the results. imagejpeg()
remember to do imagedestroy() on those image resources (memory leaks in GD)
remember to do imagedestroy() on those image resources (memory leaks in GD)
Code: Select all
Warning: imagejpeg(): Unable to open 'Resource id #4' for writing in colleague/test2.php on line 23Code: 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);
}Wait, I've got $dest twice...
Now I get:
Now I get:
Code: Select all
Warning: imagejpeg(): Unable to open 'boxtest2.jpg' for writing in colleague/test2.php on line 19Code: 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);
}