干预映像已用尽20971520字节的允许内存大小(尝试分配10240字节)


Intervention image Allowed memory size of 20971520 bytes exhausted (tried to allocate 10240 bytes)

我正在Laravel 5上使用强大的干预库实现一些图像操作。如果图像很小,比如700KB以下,宽度小于800px,它就可以正常工作。

但它无法处理大尺寸的图像,我想上传大小为8MB、宽度为2000px的图像。

我尝试过碰撞memory_limit的经典方法,但它不起作用

ini_set("memory_limit","20M");

有没有任何解决方案可以在不杀死服务器的情况下工作。

这是我的上传服务的代码。它从Request::file('file')获取图像,并使用干预将其调整为3个大小。

  • 大尺寸,最大宽度为2000像素,然后
  • 中等800px宽
  • 拇指是200像素宽的

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

请帮助我如何提高的效率和速度

FWIW,注释的解决方案对我不起作用。也就是说,我无法让以下内容为我工作:

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.已耗尽134217728字节的允许内存大小。[在线]网址:https://github.com/Intervention/image/issues/567#issuecomment-224230343。【查阅日期:2018年6月14日】

只需将php.ini文件memory_limit更新为256M。

memory_limit=256M

我试过用128和20来限制其他内存。这对我来说不起作用。但当我将内存限制更新到256M时,问题就解决了。

在cpanel中,您可以找到PHP选项。从那时起,您需要将内存限制更新为:

memory_limit=256M
or
memory_limit=192M
or
memory_limit=368M
or
memory_limit=512M
相关文章: