Rounding corners using gd

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Existance0
Forum Newbie
Posts: 3
Joined: Mon Sep 07, 2009 10:50 pm

Rounding corners using gd

Post by Existance0 »

So, it seemed to work at first when I was only rounding the top right corner but once I rotated the image for the other corners it just puts white boxes there.

Code: Select all

//Create the image
$img=imagecreatetruecolor(250,350);
 
//The following rounds the corners
$corner=imagecreatefrompng('rounded_corner.png');
 
//Top-left corner
imagecopymerge($img,$corner,0,0,0,0,5,5,100);
 
//Bottom-left corner
$rotated=imagerotate($corner,90,0);
imagecopymerge($img,$rotated,0,345,0,0,5,5,100);
 
//Bottom-right corner
$rotated=imagerotate($corner,180,0);
imagecopymerge($img,$rotated,245,345,0,0,5,5,100);
 
//Top-right corner
$rotated=imagerotate($corner,270,0);
imagecopymerge($img,$rotated,245,0,0,0,5,5,100);
 
//Output the image to the browser
header("Content-type:image/png");
imagepng($img);
Anyone know whats wrong?
Attachments
rounded_corner.png
rounded_corner.png (972 Bytes) Viewed 167 times
Last edited by Existance0 on Mon Sep 07, 2009 11:19 pm, edited 2 times in total.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Rounding corners using gd

Post by Ollie Saunders »

I don't know the answer for sure but it wouldn't surprise me if it turns out that this is just a shortcoming of GD. I've found many things like this that are supposed to work but you get just white or black or whatever. There are a load of compilation options for GD that affect it's capabilities so that might have something to do with it, I don't think I ever managed to get them all switched on and working I can't remember why not now, it was quite a while ago.

If you're looking for a quick(ish) solution you might have to resort to putting pixels in rounded corner shapes, which could be fun, it's not every day you get to call trigonometry functions.
Existance0
Forum Newbie
Posts: 3
Joined: Mon Sep 07, 2009 10:50 pm

Re: Rounding corners using gd

Post by Existance0 »

I actually found that it is most likely related to setting the transparency.

I had originally had lime green set as a transparent color but later removed it so that was an issue but I was using...

Code: Select all

imagecolortransparent();
wrong. I took out these lines and ended up getting the top left corner to work again (because changing lime to black made it the opposite of what I wanted). But I think the problem lies in that the other corners after rotated lose their transparency which makes them become that white square instead of the original transparent one that I'm merging with it.

EDIT:
Wow, it appears to be a problem with both rotating the image and setting the transparency. Leaving the top left corner with no transparency it overlays the corner correctly with white outside of the rounded edge. Setting white to be transparent like so...

Code: Select all

imagecolortransparent($img, $white);
Doesn't make the outside white transparent but rather just switches the colors making the white now on the inside of the rounded corner.

The rotating on the other hand and part that is rotated with or without being set transparent returns a white square.
Existance0
Forum Newbie
Posts: 3
Joined: Mon Sep 07, 2009 10:50 pm

Re: Rounding corners using gd

Post by Existance0 »

Example:
http://arch-games.com/FFCG/test.html

OK, if I set transparency then it just switches the colors. As seen in the top left corner. Rotating the image at all makes the image become a white square as seen in the other three corners.
Post Reply