【发布时间】:2016-12-26 19:30:53
【问题描述】:
我需要知道如何使用 Imagick 屏蔽任何方形图像。这是我到目前为止的代码,但图像没有被正确屏蔽:
获取图片
$srcFile = 'filename.png';
$image = new Imagick($srcFile);
将图像裁剪为正方形
$d = $image->getImageGeometry();
$src_width = $d['width'];
$src_height = $d['height'];
$thumbSize = min(max($src_width, $src_height), abs($thumbSize));
if ($src_width < $src_height) {
$image->cropImage($src_width, $src_width, 0, (($src_height - $src_width)/2));
} else {
$image->cropImage($src_height, $src_height, (($src_width - $src_height)/2), 0);
}
调整图片大小
$image->thumbnailImage($thumbSize, $thumbSize, 1);
使用贝塞尔曲线裁剪/遮罩图像
$image->compositeImage(bezier($thumbSize, $thumbSize), Imagick::COMPOSITE_COPYOPACITY, 0, 0);
bezier 函数创建的形状如下所示:
function bezier($width, $height) {
$fillColor = "#000";
$draw = new ImagickDraw();
用黑色填充未遮罩的部分
$fillColor = new ImagickPixel($fillColor);
$draw->setFillColor($fillColor);
$smoothPointsSet = [
[
['x' => 0.0 * $width, 'y' => 0.5 * $width],
['x' => 0.0 * $width, 'y' => 0.905 * $width],
['x' => 0.095 * $width, 'y' => 1.0 * $width],
['x' => 0.5 * $width, 'y' => 1.0 * $width]
], [
['x' => 0.5 * $width, 'y' => 1.0 * $width],
['x' => 0.905 * $width, 'y' => 1.0 * $width],
['x' => 1.0 * $width, 'y' => 0.905 * $width],
['x' => 1.0 * $width, 'y' => 0.5 * $width]
], [
['x' => 1.0 * $width, 'y' => 0.5 * $width],
['x' => 1.0 * $width, 'y' => 0.095 * $width],
['x' => 0.905 * $width, 'y' => 0.0 * $width],
['x' => 0.5 * $width, 'y' => 0.0 * $width]
], [
['x' => 0.5 * $width, 'y' => 0.0 * $width],
['x' => 0.095 * $width, 'y' => 0.0 * $width],
['x' => 0.0 * $width, 'y' => 0.095 * $width],
['x' => 0.0 * $width, 'y' => 0.5 * $width]
]
];
foreach ($smoothPointsSet as $points) {
$draw->bezier($points);
}
贝塞尔点不填充中间的正方形,所以手动填充它
$points = [
['x' => $width * 0.5, 'y' => 0.0],
['x' => 0.0, 'y' => $height * 0.5],
['x' => $width * 0.5, 'y' => $height],
['x' => $width, 'y' => $height * 0.5]
];
$draw->polygon($points);
将抽屉图像复制到新的透明 Imagick 图像
$imagick = new Imagick();
$imagick->newImage($width, $width, "none");
从这里开始,我尝试了各种属性。我没有得到任何令人满意的结果 - 图像几乎总是没有被蒙版。
#$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
#$imagick->setImageFormat("png");
$imagick->drawImage($draw);
#$imagick->setImageMatte(false);
return $imagick;
}
如果我能知道问题出在哪里以及如何解决它,我会非常高兴。我在 SO 上找到了对我不起作用的各种答案:
使用$dude->setImageMatte(1);
Using a transparent PNG as a clip mask
使用$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
How to use Imagick to merge and mask images?
很遗憾,我无法解决问题。
【问题讨论】: