[Solved] Drawing a box with GD

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

[Solved] Drawing a box with GD

Post 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!
Last edited by Grim... on Fri Aug 12, 2005 10:04 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

imageline() !

:D :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Moved to ...... Graphics! dun-dun-duuuun...... :P
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you remember to createimagefromjpeg() ?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you aren't outputting the results. imagejpeg()

remember to do imagedestroy() on those image resources (memory leaks in GD)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
}
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

potential permissions issue with an existing file, or the folder maybe?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Yup, I've just changed it. Hurrah!

Feyd == genius ;)

Thanks mate
Post Reply