I would like to change the black border of this image : http://dev.loungeup.net/im/ to transparent with Imagick in PHP.
The result image should have the gray border visible, the image inside the gray border visible, and everything outside the gray border (today in black) should be transparent and let the underlying content (in HTML page for example) visible.
I have been through the documentation several times and tried several solutions but did not figure out how to achieve it.
Any help is appreciated.
Thanks
Here's one method you could use which is similar to Photoshop's magic wand tool:
convert original.jpg -alpha set -channel alpha -fuzz 18% \
-fill none -floodfill +0+0 black transparent-border.png
Here are the commands broken down:
convert original.jpg
: start Imagemagick with the original image
-alpha set
: activate the alpha channel
-channel alpha
: have subsequent operators act on alpha channel
-fuzz 18%
: see -floodfill
...
-fill none
: see -floodfill
...
-floodfill +0+0 black
: start from the upper left corner (+0+0
) and find neighboring pixels within -fuzz
color distance of black
and replace it will -fill
transparent-border.png
: the output image
Here's the result:
Unfortunately, as you can see, this method still leaves some dark pixels with the image you provided because the border is not pure black and blends a bit with the inner gray border, and the image itself is quite small.
You will get much better results if you have a better-quality source image, or a larger one which you could then reduce in size after modifying.
If you're stuck with these small images (or if you just want to use another method), I would recommend going a different route where you create your own mask shape that is just smaller than the shape of the original photo, then add back your own gray border. I put together an example of this process below.
A possible command for this method would be:
convert original.jpg mask.png -compose CopyOpacity -composite \
-compose src-over new-border.png -composite clean-result.png
... broken down ...
convert original.jpg mask.png
: start Imagemagick with original image and bring in mask.png as the second layer (mask.png is a white rounded-rectangle shape of the photo on a black background, but the shape is slightly smaller than that of the original - the result will remove the original's gray and black border).
-compose CopyOpacity -composite
: use mask.png to "knock out" shape from original.png
-compose src-over
: reset composite method to a simple overlay
new-border.png -composite
: overlay a gray border (png is a 3 px wide border, 1px on each side of mask edge on a transparent background)
clean-result.png
: the output image
I created mask.png and new-border.png in Photoshop. You could use Imagemagick's vector tools and do this all in one command using just original.jpg, but it wouldn't be easy.
The result of the above:
On a final note, I'm not sure whether you're using PHP's Imagick or the Imagemagick from the command line. Several years ago I tried using Imagick but quickly became frustrated from the lack of documentation compared to the command line (perhaps that's changed, though). Instead, I execute the commands from PHP (e.g., from exec()
or passthru()
). Some or many others will probably tell you that you should never execute shell commands from PHP, but as long as you carefully escape any arguments I have yet to see a convincing argument against doing so. Then you'll have the entire Imagemagick documentation at your disposal (http://www.imagemagick.org/Usage/).
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With