Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将一个大图像剪切成大小相同的小图像_Php_Image_Gd - Fatal编程技术网

Php 将一个大图像剪切成大小相同的小图像

Php 将一个大图像剪切成大小相同的小图像,php,image,gd,Php,Image,Gd,给定一幅JPG图像,在水平方向将其切掉,使spic的大小约为100K 有什么建议吗 过去使用过此功能,对我来说效果很好 这将获取一个输入文件并创建指定大小的平铺 function makeTiles($name, $imageFileName, $crop_width, $crop_height) { $dir = "source dir"; $slicesDir = "target dir"; // might be good to check if $slicesD

给定一幅JPG图像,在水平方向将其切掉,使spic的大小约为100K


有什么建议吗

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

这将获取一个输入文件并创建指定大小的平铺

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);
        }
    }
}

你试过什么吗?你发现什么问题了吗?恐怕不可能按大小来划分,因为压缩比取决于数据的类型。换句话说,一个100KB的图像将具有比另一个100KB图像更大的尺寸、宽度和高度。您可以使用一些未压缩的格式,例如bmp来避免这种情况,但这通常不是一个好主意。所谓“剪切”,您的意思是,偶然地调整大小?