将1张大图像切割成相同大小的小图像

时间:2011-07-11 13:54:11

标签: php image gd

鉴于JPG图像,在水平方向切断它,使得spic的大小约为100K

任何提示?

1 个答案:

答案 0 :(得分:0)

过去使用此功能对我来说效果很好......

这将获取一个输入文件并创建指定大小的图块...

function makeTiles($name, $imageFileName, $crop_width, $crop_height)
{
    $dir = "source dir";
    $slicesDir = "target dir";

    // might be good to check if $slicesDir exists etc if not create it.

    $inputFile = $dir . $imageFileName;

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

        $cropWidthTimes = ceil($imgWidth/$crop_width);
    $cropHeightTimes = ceil($imgHeight/$crop_height);
    for($i = 0; $i < $cropWidthTimes; $i++)
    {
        for($j = 0; $j < $cropHeightTimes; $j++)
        {
            $img = new Imagick($inputFile);
            $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);
        }
    }
}