Customize transparent image with imagick php

I have an image with a transparent background that I would like to highlight using a border of 5 pixels. In Photoshop, I can stroke it to achieve this.

I tried using borderImage but did not outline the penguin.

 $image = new Imagick(); $image->readImage('tux.png'); $image->borderImage(new ImagickPixel('red'), 5, 5); // no outline 

This is an image.

tux original

This is what I want to achieve.

tux outlined

+7
php image outline imagick
source share
1 answer

I'll start from the command line and maybe make PHP later or let you work out this bit ...

Step 1 - Extract Transparency

As you saw, -border describes the overall image, but in fact you want to outline the opaque areas so that you need to work with transparency or an alpha layer. First, extract this:

 convert tux.png -alpha extract alpha.png 

enter image description here

Step 2 - Get the edges of the opaque area

Now you want the edges of this to be highlighted, so I use -morphology :

 convert alpha.png -morphology edge octagon -threshold 50% edge.png 

enter image description here

I heard that people have difficulties with morphological operations in PHP, so there is an alternative method for this step here that does not use morphology. Basically, it duplicates the alpha layer and then uses statistics to find the brightest pixel in each 3x3 window (which will only have an effect at the edges where there are some black and some white pixels in the 3x3 window), and then the differences result with original to show the affected pixels. Easier to do than to describe!

 convert alpha.png \( +clone -statistic maximum 3x3 -threshold 50% \) -compose difference -composite edge.png 

enter image description here

Use a 5x5 margin for a thicker line.

I see that there is the -edge 5 option, which is much simpler - we live and learn!

Step 3 - Make the edges red and the rest transparent

Now you want white to be red and black to be transparent:

 convert edge.png -fill red -opaque white -transparent black rededge.png 

enter image description here

Step 4 - Composite Red Contour Above the Original

And finally, you want to create a composite over the original:

 convert tux.png rededge.png -composite result.png 

enter image description here

Whole pig

Or you can do it all in one way:

 convert tux.png \( +clone -alpha extract -morphology edge octagon -threshold 50% -fill red -opaque white -transparent black \) -composite result.png 

You may prefer the finer effect of the -morphology edgeout over the -morphology edge .

PHP version

My PHP skills are "low", but I started and am making some progress - it will be updated later, but for now it looks like this:

  $image = new Imagick("tux.png"); $alpha = clone $image; $alpha->separateImageChannel(Imagick::CHANNEL_ALPHA); $alpha->negateImage(true); $alpha->edgeImage(5); $alpha->opaquePaintImage("white","red",65000,FALSE); $alpha->transparentPaintImage("black",0.0,0,FALSE); $image->compositeImage($alpha,Imagick::COMPOSITE_DEFAULT,0,0); $image->writeImage("result.png"); 

This seems like a lot of work, but some aspects can probably be matched - in particular, the magic number 65000 and maybe some kind of unnecessary cloning and stuff - I will leave it to you!

+5
source share

All Articles