尝试拍摄一张矩形照片,将其裁剪为方形区域,然后将其蒙版为具有透明背景的圆形。
//$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} 的示例输入
粗略的预期输出:
我得到的图像看起来与输入图像大致相似。