【问题标题】:IMagick function to cut an image into roughly equally-sized tilesIMagick 功能将图像切割成大致相同大小的图块
【发布时间】:2011-01-18 22:26:53
【问题描述】:
我正在尝试使用 IMagick PHP 包装器来帮助将指定的图像切割成一组图块(其数量是可变的)。
在 ImageMagick 文档中,引用了 -crop 运算符,该运算符接受 @ 的可选标志,指示它将图像切割成“大致相同大小的分区”(see here),解决了当图像大小不是所需图块大小的精确倍数时该怎么办。
有谁知道是否有办法在 IMagick PHP 包装器中利用此功能?除了cropImage(),还有什么我可以用的吗?
【问题讨论】:
标签:
php
imagemagick
imagick
【解决方案1】:
我必须做同样的事情(如果我没看错你的问题)。虽然它确实使用cropImage...
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);
}
}
}