我正在尝试使用IMagick PHP包装器来帮助将指定的图像分割成一组瓦片(其数量是可变的)。
在ImageMagick文档中,提到-crop运算符接受@的可选标志,该标志将指示它将图像剪切为“大致相等大小的部分”(see here),从而解决当图像大小不是所需平铺大小的精确倍数时该如何处理的问题。
有没有人知道有没有办法在IMagick的PHP中利用这个功能?除了cropImage(),还有什么我可以用的吗?
发布于 2011-05-13 20:07:05
我不得不做同样的事情(如果我没看错你的问题)。尽管它确实使用了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);
}
}
}https://stackoverflow.com/questions/4729767
复制相似问题