gpt4 book ai didi

php - 干预图像允许内存大小为 20971520 字节耗尽(尝试分配 10240 字节)

转载 作者:搜寻专家 更新时间:2023-10-31 21:06:44 24 4
gpt4 key购买 nike

我正在使用很棒的 intervention library 实现一些图像处理在 Laravel 5 上。如果图像很小,比如小于 700KB,宽度小于 800px,它工作正常。

但是它不能处理大尺寸的图片,我想上传 8 MB 的图片,2000 像素宽。

我已经尝试过经典的方法来提高 memory_limit 但它不起作用

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

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

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

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

    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

This solution为我工作的是改变内存限制如下:

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

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

Resizing images is a very memory consuming task. You can't assume that a 1MB image takes 1MB of memory. Resizing a 3000 x 2000 pixel image to 300 x 200 may take up to 32MB memory for example. Even if the image is under 1MB filesize.
@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].

关于php - 干预图像允许内存大小为 20971520 字节耗尽(尝试分配 10240 字节),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31128856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com