【问题标题】:circularize an image with imagick用imagick循环图像
【发布时间】: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+

标签: php imagick


【解决方案1】:

对于那些使用旧版本 Imagick(setimagematte 在低于 6.2.9 的版本中不存在)的用户,我想出了一个简单的解决方案。这里的事情是将不透明度从蒙版复制到原始图像

原图:

面具:

结果:

代码:

$base = new Imagick('original.jpg');
$mask = new Imagick('mask.png');

$base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$base->writeImage('result.png');

您可以使用 Imagick 黑色圆圈作为蒙版,但我认为它并不完美,所以我使用了自己的。

当然,您当然必须调整/裁剪您的图像,但那是另一回事了。

希望这会有所帮助。

J.

【讨论】:

  • 确实有帮助。谢谢!
【解决方案2】:

这对我有用:

<?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 方面有点落后,因为这些天我不太使用它。

【讨论】:

  • 嗯,是的,我将再次替换值并重试。谢谢!还有:php.net/manual/en/language.namespaces.php
  • 另外,对于其他来这里的人,我最终只是使用 css 边界半径而不是在服务器端这样做。
  • 我想知道为什么生成的图像似乎在右侧和底部被切割。圈子并不完美。
  • 扁平可能是由于裁剪图像尺寸造成的。我没有把它完美,因为它是一个例子并证明了这个过程。
【解决方案3】:

我在这里还建议了另一种解决方法:

// 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!

非常感谢之前所有的答案和贡献者,他们引导我找到这段代码!

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

【讨论】:

  • 这是一个更好的解决方案,因为它不需要遮罩图像。
  • roundCorners 已从上一个版本中删除
【解决方案4】:

我在为 Ruby on Rails 寻找类似的解决方案时偶然发现了这一点,请注意 this Stackoverflow question 使用了小插图,这似乎是解决问题的一种更简单的方法。

我使用 vignette 解决了 rounded images in Ruby on Rails using Dragonfly 的问题。

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 2022-06-14
    • 1970-01-01
    • 2011-10-21
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多