Laravel滑出内存

Laravel glide out of memory

我在 Laravel 上使用 Glide,但遇到 "small" 问题。当我通过 outputImage() 加载小图像时,一切正常。但是当我尝试加载 - 简单 - 1.6MB 的图像 Laravel 放入控制台:

 Allowed memory size of 67108864 bytes exhausted (tried to allocate 14152 bytes) in C:\Users\displate\Documents\displate\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 34

我怎样才能简单地修复它?

我的代码有问题的部分:

protected function get($path,$storage,$sizes,$prefix=''){
    ini_set('memory_limit', '64M'); // it's not working :<
    if($this->check_variables($sizes)){
        $server=$this->prepare_server($storage,$prefix);

        try{
            $server->getImageResponse($path);

            $server->outputImage($path, $_GET);
        }
        catch(\Exception $e){
            $this->download_image($path,$storage);

            $server->getImageResponse($path);

            $server->outputImage($path, $_GET);
        }
    } 
    else{
        abort(404);
    }
}

protected function prepare_server($storage_name,$prefix=''){
    $server = \League\Glide\ServerFactory::create([
                    'source' => \Storage::disk($storage_name)->getDriver(),
                    'cache'  =>  \Storage::disk($storage_name)->getDriver(),
                    'source_path_prefix'    =>  $prefix,
                    'cache_path_prefix'     =>  $storage_name.'_cached',

                ]);

    $_GET['fit']='crop';
    $_GET['crop']='center';

    return $server;
}

如果您确定您实际上 运行 内存不足是出于您认为的原因,而不是由于某些无限循环或其他错误,您可以增加 PHP 能够使用的内存量通过执行以下操作来使用。

  • 找到您的网络服务器使用的 php.ini 文件
  • 编辑 php.ini 文件中的 memory_limit 参数(通常在名为 Resource Limits 的部分中)
  • 重新启动 Apache。

之后应该是这样的:

memory_limit 128M

如果你遇到困难,this 看起来是个不错的资源

64Mb,也就是你有的,不算多。如果你有 ram,我会坚持 512M 甚至 1024M。