Php 使用imagick循环图像

Php 使用imagick循环图像,php,imagick,Php,Imagick,尝试拍摄一张矩形照片,将其裁剪成方形区域,然后将其遮罩成具有透明背景的圆形 //$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-&

尝试拍摄一张矩形照片,将其裁剪成方形区域,然后将其遮罩成具有透明背景的圆形

//$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}

大致预期产出:

我得到的图像与输入图像大致相似

这对我很有用:

<?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版本可能有问题

它有名称空间


还是不知道这是什么意思我现在对php的使用有点落后,因为我现在不太使用它。

对于那些使用旧版本Imagick setimagematte的人来说,在低于6.2.9的版本中不存在,我提出了一个简单的解决方案。这里要做的是将不透明度从遮罩复制到原始图像

原始图像: 面具: 结果: 守则: 你可以用一个黑色的圆圈作为面具,但我觉得它并不完美,所以我用了我自己的

当然,你肯定要调整/裁剪你的图片,但那是另一回事

希望这有帮助


J.

我在为RubyonRails寻找类似的解决方案时偶然发现了这一点,请注意,使用vignette似乎是一种更简单的解决问题的方法


我使用vignette解决了我的问题。

这里还有另一个我建议的解决方法:

// create an imagick object of your image
$image = new \Imagick('/absolute/path/to/your/image');
// crop square your image from its center (100px witdh/height in my example)
$image->cropThumbnailImage(100, 100);
// then round the corners (0.5x the width and height)
$image->roundCorners(50, 50);
// force the png format for transparency
$image->setImageFormat("png");
// write the new image
$image->writeImage('/absolute/path/to/your/new/image');
// done!
非常感谢所有以前的答案和贡献者,他们引导我找到了这段代码


请随时测试/评论我的解决方案

你是说像这样?我希望不必创建遮罩图像,然后根据上传/提供给我的图像调整其大小。请在您的问题中添加示例图像,以便清楚输入内容和预期输出内容,并描述/显示您当前的内容,不同的输出是。为什么所有Imagick文本前面都有\呢?它有名称空间。我正在使用5.3.2+嗯,是的,我将再次替换值并重试。谢谢另外:另外,对于其他来到这里的人,我最终只是使用css边框半径,而不是在服务器端这样做。我想知道为什么生成的图像看起来是在右侧和底部切割的。圆不是完美的。平面可能是因为裁剪图像的尺寸。我没有得到完美的,因为它是一个例子,并证明了这个过程。这是一个更好的解决方案,因为它不需要遮罩图像。圆角已从上一个版本中删除
// create an imagick object of your image
$image = new \Imagick('/absolute/path/to/your/image');
// crop square your image from its center (100px witdh/height in my example)
$image->cropThumbnailImage(100, 100);
// then round the corners (0.5x the width and height)
$image->roundCorners(50, 50);
// force the png format for transparency
$image->setImageFormat("png");
// write the new image
$image->writeImage('/absolute/path/to/your/new/image');
// done!