Intervention Image 允许的内存大小已用尽(尝试分配 10240 字节)

10
我正在使用 Laravel 5 和强大的 intervention 库 实现一些图像处理。如果图片很小,比如大小低于800像素宽且小于700KB,则可以正常工作。
但是,当处理大尺寸图片时,例如8 MB大小和2000像素宽度,则无法处理。我已经尝试了传统的增加 memory_limit 的方法,但并没有起作用。
ini_set("memory_limit","20M");

有没有一种解决方案可以在不关闭服务器的情况下工作。

这是我的上传服务代码。它从Request::file('file')中获取图像,并使用 intervention 调整大小为 3 种尺寸。

  • Big size which is max width of 2000 px, then a
  • Medium 800px wide
  • Thumb is 200px wide

    public function photo($file)
    {
        $me = Auth::user();
        //user folder for upload
        $userFolder = $me->media_folder;
    
        //Check the folder exist, if not create one
        if($this->setupUsrFolder($userFolder)) {
            //Unique filename for image
            $filename = $this->getUniqueFilename($userFolder);
    
            //Bump the memory to perform the image manipulation
            ini_set("memory_limit","20M");
    
            //Generate thumb, medium, and max_width image
            $img = Image::make($file);
            //big_img
            $big = $img->resize(config('go.img.max_width'), null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            //Big Image
            $big->save($this->getPhotoPath($filename, $userFolder, 'b_'));
    
            //Medium image
            $med = $big->resize(config('go.img.med_width'), null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            $med->save($this->getPhotoPath($filename, $userFolder, 'm_'));
    
            //Thumbnail
            $thumb = $med->resize(config('go.img.thumb_width'), null, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            $thumb->save($this->getPhotoPath($filename, $userFolder, 't_'));
    
            return $filename;
        }
    
        return null;
    }
    

请帮助我如何使其更高效和快速


1
请尝试以下操作,使用$med作为$img,并像处理$big一样处理$thumb。在每个->save()之后,使用->destroy()。顺便说一下,ini_set("memory_limit","20M")非常低。 - user2094178
1
20971520字节等于20M,那么你如何确定内存限制设置无效呢? - Devon
谢谢@user2094178,让我试试->destroy(),内存将会是48M。@Devon,是的,它将内存设置为20MB,但是它不起作用,仍然报同样的错误。 - Saqueib
谢谢@Devon,我已将内存设置为128m,但现在在保存每个图像后调用->destroy()时出现imagesx() 19不是有效的图像资源 - Saqueib
现在似乎是一个不同的问题,所以不再与这个问题相关。 - Devon
显示剩余6条评论
2个回答

11

顺便说一句,那些有注释的解决方案对我没用。也就是说,以下内容对我没有用:

Image::make($file)->resize(2000, null)->save('big.jpg')->destroy();
Image::make($file)->resize(800, null)->save('med.jpg')->destroy();
Image::make($file)->resize(200, null)->save('thumb.jpg')->destroy();

仍然会抛出以下错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) in  
C:\Users\XXX\app\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 136
我的解决方法是更改内存限制,具体做法请参照此方案
public function resize() {
    ini_set('memory_limit', '256M');
    // Do your Intervention/image operations...
}

// or...

ini_set('memory_limit', '256M');
Image::make($file)->resize(2000, null)->save('big.jpg');
Image::make($file)->resize(800, null)->save('med.jpg');
Image::make($file)->resize(200, null)->save('thumb.jpg');

这成功地解决了问题。
原因是:

调整图像大小是一项非常消耗内存的任务。无法假设1MB图像占用1MB内存。例如,将3000 x 2000像素的图像调整为300 x 200,可能需要高达32MB的内存。即使图像文件小于1MB。
@olivervogel. 2018. Allowed memory size of 134217728 bytes exhausted. [ONLINE] Available at: https://github.com/Intervention/image/issues/567#issuecomment-224230343. [Accessed 14 June 2018].


2

只需将php.ini文件中的memory_limit更新为256M即可。

memory_limit=256M

我尝试过将memory_limit设置为128和20,但这对我没有用。但是当我将内存限制更新为256M时,问题就解决了。

在cpanel中,您会找到PHP选项。您需要更新内存限制为:

memory_limit=256M
or
memory_limit=192M
or
memory_limit=368M
or
memory_limit=512M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接