上传图片(6M),内存溢出(128M)

luhuan

问题描述

上传的文件只有6M,PHP内存设置的是128M,但是却提示内存溢出

程序代码

use Intervention\Image\ImageManagerStatic as Image;

$file = $request->file('file');
$image = Image::make($file);//这里内存溢出了

报错信息

pid:12746 Worker[12746] process terminated with ERROR: E_ERROR "Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) in /vendor/intervention/image/src/Intervention/Image/Gd/Decoder.php on line 154"

截图报错信息里报错文件相关代码

扩展154行代码如下
截图

操作系统及workerman/webman等框架组件具体版本

linux下的docker容器,php8.0版本,webman版本:1.5.6

1320 2 1
2个回答

six

phar打包或者二进制打包后的文件会很大,启动后会进程会占用较高内存,上传个文件内存就可能超限制了。

walkor 打赏

app/controller/UploadController.php

<?php
namespace app\controller;
use support\Request;
use Intervention\Image\ImageManagerStatic as Image;
class UploadController
{

    public function index()
    {
        return '<body>
<form method="post" action="http://127.0.0.1:8877/upload/files" enctype="multipart/form-data" />
<input name="file" multiple="multiple" type="file">
<input type="submit">
</form>';
    }

    public function files(Request $request)
    {
        $s = 'M<br> 起始内存占用:' . number_format(memory_get_usage()/(1024*1024), 2);
        $file = $request->file('file');
        $image = Image::make($file);
        return  '文件大小:' . number_format($file->getSize()/(1024*1024), 2) . $s
            . 'M<br> 处理后内存占用:' . number_format(memory_get_usage()/(1024*1024), 2)
            . 'M<br> memory_limit:' . ini_get( 'memory_limit');
    }
}

截图

我本地测试结果,没有什么问题

环境

php -v
PHP 7.4.32 (cli) (built: Sep 28 2022 19:14:45) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
php --ri gd
gd

GD Support => enabled
GD Version => bundled (2.1.0 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.9.1
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => 9 compatible
PNG Support => enabled
libPNG Version => 1.6.34
WBMP Support => enabled
XPM Support => enabled
libXpm Version => 30512
XBM Support => enabled
WebP Support => enabled
BMP Support => enabled
TGA Read Support => enabled

Directive => Local Value => Master Value
gd.jpeg_ignore_warning => 1 => 1
composer info | grep image
intervention/image               2.7.2    Image handling and manipulation library with...
  • luhuan 2023-09-20

    老大,我试了还是不行的,上传了一个4M多的,内存占用将近50M,相比你的测试结果,内存占用很高
    结果:
    文件大小:4.64M
    起始内存占用:9.97M
    处理后内存占用:57.15M
    memory_limit:128M

    <?php
    namespace app\api\controller;

    use app\service\CommonService;
    use DI\Attribute\Inject;
    use Intervention\Image\ImageManagerStatic as Image;
    use support\Request;

    class CommonController
    {

    [Inject]

    private CommonService $service;
    
    public function test(Request $request)
    {
        $s = 'M<br> 起始内存占用:' . number_format(memory_get_usage()/(1024*1024), 2);
        $file = $request->file('file');
        $image = Image::make($file);
        return  '文件大小:' . number_format($file->getSize()/(1024*1024), 2) . $s
            . 'M<br> 处理后内存占用:' . number_format(memory_get_usage()/(1024*1024), 2)
            . 'M<br> memory_limit:' . ini_get( 'memory_limit');
    }

    }

  • walkor 2023-09-20

    php版本?
    Intervention\Image 版本?
    用一个空的webman项目试下,排除自动注入问题。

  • luhuan 2023-09-21

    版本信息:php:8.0,Intervention\Image:2.7.2
    有测试结果了,用了一个空的webman测试的。
    在容器中起的服务,上传文件时会占用比较高的内存,在主机上的服务上传则是正常的;
    这个问题有解决的办法吗?

年代过于久远,无法发表回答
×
🔝