开发者

Imagick roundCorners makes image half-transparant

开发者 https://www.devze.com 2023-03-13 10:31 出处:网络
I\'m in the process of writing an image script for a website I\'m making, and for one of the image modes I need to both crop the image and make round corners for it. This is the code I\'m using now:

I'm in the process of writing an image script for a website I'm making, and for one of the image modes I need to both crop the image and make round corners for it. This is the code I'm using now:

$img = new Imagick();
$img->readImageBlob($data);
$img->resizeImage($width, $height, $img->FILTER_GAUSSIAN, 1);
$canvas = new Imagick();
$canvas->newImage($size[0], $size[1], new ImagickPixel('white'));
$canvas->compositeImage($img, imagick::COMPOSITE_OVER, 0, 0);
$canvas->roundCorners(10, 10);
$canvas->setImageFormat('png');

Resulting image

However, the resulting image is about half-transparent, as seen above, except for a 10-ish px border around the edge, created by roundCorner I assume. I've tried a lot of different ways of doing this, including cropping $img directly and roundCorner on开发者_JS百科 it, but then the result is without round corners.

I suspect compositeImage to be the thief in this drama, but I can't say for sure. Anyone?

EDIT: Correction: If I do the crop and round solution, I end up with rounded corners, but they have a black background. This is the code I'm using here. Anyone spot the error/solution?

$img = new Imagick();
$img->readImageBlob($data);
$img->resizeImage($width, $height, $img->FILTER_GAUSSIAN, 1);
$img->cropImage($size[0], $size[1], 0, 0);
$img->roundCorners(10, 10);

EDIT 2: The solution to the crop and round resolution was to make sure it's a png, so that is has alpha-support. However, the result is the same as the one produced from my original code.

EDIT 3: Tried this script on my deployment server, and it worked as I wanted it to, so it seems like there's some problem with the version of imagick / php-imagick shipped with macports. Thanks for all the help!


You can tell imagemagick to give you an alpha background with

$img->setImageAlphaChannel ( imagick::ALPHACHANNEL_TRANSPARENT )

or try setting the image format to PNG24 explicitly before doing any transformations.

Order is important here, the image needs an alpha channel before you expose the background.


It was a problem with the version of Imagick / php-imagick shipped with current macports. Haven't solved it for my computer, but the script is working as intended on my deployment server.

0

精彩评论

暂无评论...
验证码 换一张
取 消