PHP GD: Get Image Outline?

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
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

PHP GD: Get Image Outline?

Post by IceMetalPunk »

I was wondering if there is a way, in PHP (using GD functions) to get the outer outline of an image resource.

For example, I create an image, set white as the transparent color, and end up making something like this:

Image

Obviously, the inner small square would be transparent, as would the outside of the black square. What I'm looking for is a way to get everything from the outer edge of the black square in--including the transparent square--as an outline, so I can fill it.

I can't just use coordinates because the images will be mixtures of other pictures from imagecreatefromgif() calls. I just need a way to fill it all in, without filling in the very-outer border.

Is this possible? If so, how?

-IMP ;) :)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i dont believe that you can do it with GD, i have a few friends that use Imagemajick and i think you may be able to do it with that, ive never used it before so im nto sure what to tell you.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Really? Crap. I'm using a host that has GD instealled, but no imagemajick..... oh, well, thanks anyway.

-IMP ;) :)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

dont loose hope until someone gives you a for sure answer, you may be able to get done what you need to with GD2 only, im just not positive if its posiable, hopefully someone else here can give you a more difinitive answer.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP GD: Get Image Outline?

Post by aerodromoi »

IceMetalPunk wrote:I was wondering if there is a way, in PHP (using GD functions) to get the outer outline of an image resource.

For example, I create an image, set white as the transparent color, and end up making something like this:

Image

Obviously, the inner small square would be transparent, as would the outside of the black square. What I'm looking for is a way to get everything from the outer edge of the black square in--including the transparent square--as an outline, so I can fill it.

I can't just use coordinates because the images will be mixtures of other pictures from imagecreatefromgif() calls. I just need a way to fill it all in, without filling in the very-outer border.

Is this possible? If so, how?

-IMP ;) :)
Wouldn't it be possible to do it the other way round - filling a rectangle with images and then creating a border using imagecopy?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Here's the problem: photos are Jpegs. This allows a high level of compression but does not allow transparency. GIF allows transparency but only 8 bit palette images (256 colours) and still the images are a lot bigger than Jpeg. PNG allows transparency and true colour images but the file size would be enormous compared to a Jpeg. So whatever you produce with either GD (which can easily do this) or any other software is going to be completely impracical.

If the image is for use in a web page a much more practical way would be to apply CSS to the image...

Code: Select all

<img style="border:5px #000 solid;padding:0;float:left;margin:5px;" src="image.jpg" alt="sexy image">
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

I'm using PNG, and the file size does not matter.

And I don't want to just draw a border. I want the outer border only (as in, no holes outlined), so I can fill it in in the resulting image... but it seems like it can't be done, so I'll find another way to get this working.

Thanks to everyone for your help, anyay.

-IMP ;) :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Edge detection isn't all that tricky .. I wrote an implementation of the Sobel detection algorithm a while back .. http://www.phpgd.com/scripts.php?script=32

You could then use imagefill() to flood the middle area with a specific color .. and then cycle through the image to change that color to transparent. That would get you an image you could then copy over another image to get the middle filled with the second image. Maybe. It wouldn't be all that easy .. and it'd be slow as hell .. It certainly isn't impossible anyway.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Thanks, but that's not exactly what I'm looking for. That script gets the inside edges, too. I only want it to get the OUTER edges...

-IMP ;) :)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I am just wondering why do you need to have a white border around the black border of your image? Cause you could first just make the image with no white border around the outside, then have it use the previous created image to make another image that has the transparent, outer border.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

@tecktalkcm0391: I'm using imagecreatefromgif, so the shapes are very irregular. So that method is out of the question.

@ole: The border will be many different colors, so that will not work either.

-IMP ;) :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

IceMetalPunk wrote:Thanks, but that's not exactly what I'm looking for. That script gets the inside edges, too. I only want it to get the OUTER edges...

-IMP ;) :)
Like I said, you'd need to modify the script a bit .. write something to scan through the edge data and work out which the outermost edge is, and get rid of the rest. That's the easy bit, edge detection is the hard bit. And I've done that for you.. ;)
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Well, it may be easy for you, but even looking over your script, I have NO IDEA how it works even now :lol:

-IMP ;) :)
Post Reply