Page 1 of 1

PHP GD: Get Image Outline?

Posted: Fri Jun 30, 2006 3:21 pm
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 ;) :)

Posted: Fri Jun 30, 2006 9:22 pm
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.

Posted: Fri Jun 30, 2006 9:48 pm
by IceMetalPunk
Really? Crap. I'm using a host that has GD instealled, but no imagemajick..... oh, well, thanks anyway.

-IMP ;) :)

Posted: Fri Jun 30, 2006 9:53 pm
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.

Re: PHP GD: Get Image Outline?

Posted: Sat Jul 01, 2006 12:53 am
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?

Posted: Sat Jul 01, 2006 5:06 am
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">

Posted: Sat Jul 01, 2006 3:35 pm
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 ;) :)

Posted: Sat Jul 01, 2006 5:02 pm
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.

Posted: Sat Jul 01, 2006 5:59 pm
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 ;) :)

Posted: Sat Jul 01, 2006 6:04 pm
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.

Posted: Sat Jul 01, 2006 6:47 pm
by Ollie Saunders

Posted: Sat Jul 01, 2006 8:59 pm
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 ;) :)

Posted: Sun Jul 02, 2006 5:00 am
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.. ;)

Posted: Sun Jul 02, 2006 6:26 pm
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 ;) :)