问题描述
尝试拍摄矩形照片,将其裁剪为正方形区域,然后将其蒙版为具有透明背景的圆形.
Trying to take a rectangular photo, crop it into a square region, and then mask it into a circular with a transparent background.
//$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();
结果是矩形图像,未裁剪且未圆形化.我在做什么错了?
The result is the rectangular image, uncropped and without being circularized. What am I doing wrong?
示例图片:
$ dims = {"x":253,"y":0,"x2":438.5,"y2":185.5,"w":185.5,"h":185.5}的示例输入
Example input for $dims = {"x":253,"y":0,"x2":438.5,"y2":185.5,"w":185.5,"h":185.5}
粗略预期输出:
我得到的图像看起来大致类似于输入图像.
Image i'm getting looks roughly like the input image.
推荐答案
这对我有用:
<?php
//$dims is an array with the width, height, x, y of the region in the rectangular image (whose path on disk is $tempfile)
$tempfile = 'VDSlU.jpg';
$outfile = 'blah.png';
$circle = new Imagick();
$circle->newImage(185.5, 185.5, 'none');
$circle->setimageformat('png');
$circle->setimagematte(true);
$draw = new ImagickDraw();
$draw->setfillcolor('#ffffff');
$draw->circle(185.5/2, 185.5/2, 185.5/2, 185.5);
$circle->drawimage($draw);
$imagick = new Imagick();
$imagick->readImage($tempfile);
$imagick->setImageFormat( "png" );
$imagick->setimagematte(true);
$imagick->cropimage(185.5, 185.5, 253, 0);
$imagick->compositeimage($circle, Imagick::COMPOSITE_DSTIN, 0, 0);
$imagick->writeImage($outfile);
$imagick->destroy();
?>
<img src="blah.png">
我总是尝试使代码保持简单,直到我能正常工作,然后添加所有变量等.这可能是问题,或者您的Imagick版本可能有问题.
I always try to keep the code simple until I get it working and then add all the variables etc. That could be the problem or there could be a problem with your version of Imagick.
还是不知道是什么意思! -我对php有所了解,因为这些天我不太使用它.
Still do not know what it means! - I am getting a bit behind with php as I do not use it very much these days.
这篇关于使用imagick使图像圆形化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!