用PHP中的许多小映像创建一个大映像,而不会耗尽内存

用PHP中的许多小映像创建一个大映像,而不会耗尽内存,php,image,save,large-files,wideimage,Php,Image,Save,Large Files,Wideimage,我正在尝试使用WideImage库从PHP中的许多小图像中创建一个非常大的图像 如果映像太大,服务器显然会耗尽内存 是否有办法通过成批执行此过程或直接写入磁盘来解决此问题 代码如下所示: $blank = WideImage::load('../paintings/blank.png'); //this is a blank image file $new = $blank -> resize($fullWidth,$fullHeight,'fill');

我正在尝试使用WideImage库从PHP中的许多小图像中创建一个非常大的图像

如果映像太大,服务器显然会耗尽内存

是否有办法通过成批执行此过程或直接写入磁盘来解决此问题

代码如下所示:

        $blank = WideImage::load('../paintings/blank.png'); //this is a blank image file
        $new = $blank -> resize($fullWidth,$fullHeight,'fill'); //resize to full image size

        for ($x = 0; $x < $fullWidth ; $x = $x + $smallPictureWidth)
        {
            for ($y = 0; $y < $fullHeight ; $y = $y + $smallPictureHeight)
            {
                $fileName = //code to get filename based on $x and $y

                $img = WideImage::load($fileName);
                $new = $new -> merge($img,$x,$y,100);
            }
        }
        $fullImageName = //code to determine file name
        $new -> saveToFile($fullImageName);
$blank=WideImage::load('../paints/blank.png')//这是一个空白图像文件
$new=$blank->resize($fullWidth、$fullHeight、'fill')//将大小调整为完整图像大小
对于($x=0;$x<$fullWidth;$x=$x+$smallPictureWidth)
{
对于($y=0;$y<$fullHeight;$y=$y+$smallpicturehight)
{
$fileName=//基于$x和$y获取文件名的代码
$img=WideImage::load($fileName);
$new=$new->merge($img,$x,$y,100);
}
}
$fullImageName=//确定文件名的代码
$new->saveToFile($fullImageName);

谢谢

图像本质上是非常需要记忆的。您可以尝试批处理(例如,在单个进程中生成每一行,然后在不同的进程中将这些行缝合在一起),但是如果您尝试生成的图像确实很大,则可能无法将其放入可用内存中。例如,truecolour 4K图像需要23兆字节(3840 x 2160 x 3=24883200字节,假设没有alpha通道)。您可以尝试类似于
ini_set(“内存限制”,“128M”)的方法
除非
WideImage
提供了将小图像直接写入文件(大图像)的选项,否则您将始终需要将未压缩的大图像存储在内存中。所以你应该从检查他们提供的选项开始。不要为你的中间图像保存到jpg。您将通过重复打开/重新保存目标图像来降低其质量。使用临时无损格式,例如
.bmp
,直到组装完成,然后将其转换为.jpg