【发布时间】:2012-12-10 05:32:07
【问题描述】:
尝试拍摄一张矩形照片,将其裁剪为正方形区域,然后将其蒙版为具有透明背景的圆形。
//$dims is an array with the width, height, x, y of the region in the rectangular image (whose path on disk is $tempfile)
$circle = new \Imagick();
$circle->newImage($dims['w'], $dims['h'], 'none');
$circle->setimageformat('png');
$circle->setimagematte(true);
$draw = new \ImagickDraw();
$draw->setfillcolor('#ffffff');
$draw->circle($dims['w']/2, $dims['h']/2, $dims['w']/2, $dims['w']);
$circle->drawimage($draw);
$imagick = new \Imagick();
$imagick->readImage($tempfile);
$imagick->setImageFormat( "png" );
$imagick->setimagematte(true);
$imagick->cropimage($dims['w'], $dims['h'], $dims['x'], $dims['y']);
$imagick->compositeimage($circle, \Imagick::COMPOSITE_DSTIN, 0, 0);
$imagick->writeImage($tempfile);
$imagick->destroy();
结果是矩形图像,未裁剪且未圆化。我做错了什么?
示例图片:
$dims = {"x":253,"y":0,"x2":438.5,"y2":185.5,"w":185.5,"h":185.5} 的示例输入
粗略的预期输出:
我得到的图像与输入图像大致相似。
【问题讨论】:
-
你的意思是这样的? stackoverflow.com/q/8699228/367456
-
我宁愿不必创建蒙版图像,然后必须根据上传/给我的图像调整它的大小。
-
请在您的问题中添加示例图像,以便清楚输入是什么以及预期的输出是什么(并描述/显示您当前的不同输出是什么)。
-
为什么所有 Imagick 文本前面都有 \?
-
它是命名空间的。我正在使用 5.3.2+