首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于将图像剪切成大小大致相等的平铺图像的IMagick函数

用于将图像剪切成大小大致相等的平铺图像的IMagick函数
EN

Stack Overflow用户
提问于 2011-01-19 06:26:54
回答 1查看 1.1K关注 0票数 1

我正在尝试使用IMagick PHP包装器来帮助将指定的图像分割成一组瓦片(其数量是可变的)。

在ImageMagick文档中,提到-crop运算符接受@的可选标志,该标志将指示它将图像剪切为“大致相等大小的部分”(see here),从而解决当图像大小不是所需平铺大小的精确倍数时该如何处理的问题。

有没有人知道有没有办法在IMagick的PHP中利用这个功能?除了cropImage(),还有什么我可以用的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-13 20:07:05

我不得不做同样的事情(如果我没看错你的问题)。尽管它确实使用了cropImage...

代码语言:javascript
复制
function slice_image($name, $imageFileName, $crop_width, $crop_height)
{

$dir = "dir where original image is stored";
  $slicesDir = "dir where you want to store the sliced images;
  mkdir($slicesDir); //you might want to check to see if it exists first....

$fileName = $dir . $imageFileName;

$img = new Imagick($fileName);
  $imgHeight = $img->getImageHeight();
  $imgWidth = $img->getImageWidth();

$crop_width_num_times = ceil($imgWidth/$crop_width);
  $crop_height_num_times = ceil($imgHeight/$crop_height);
  for($i = 0; $i < $crop_width_num_times; $i++)
  {
    for($j = 0; $j < $crop_height_num_times; $j++)
    {
      $img = new Imagick($fileName);
      $x = ($i * $crop_width);
      $y = ($j * $crop_height);
      $img->cropImage($crop_width, $crop_height, $x, $y);
      $data = $img->getImageBlob();

  $newFileName = $slicesDir . $name . "_" . $x . "_" . $y . ".jpg";
  $result = file_put_contents ($newFileName, $data);
}


}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4729767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档