上傳的文件只有6M,PHP內(nèi)存設(shè)置的是128M,但是卻提示內(nèi)存溢出
use Intervention\Image\ImageManagerStatic as Image;
$file = $request->file('file');
$image = Image::make($file);//這里內(nèi)存溢出了
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"
擴(kuò)展154行代碼如下
linux下的docker容器,php8.0版本,webman版本:1.5.6
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> 起始內(nèi)存占用:' . 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> 處理后內(nèi)存占用:' . number_format(memory_get_usage()/(1024*1024), 2)
. 'M<br> memory_limit:' . ini_get( 'memory_limit');
}
}
我本地測試結(jié)果,沒有什么問題
環(huán)境
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...
老大,我試了還是不行的,上傳了一個(gè)4M多的,內(nèi)存占用將近50M,相比你的測試結(jié)果,內(nèi)存占用很高
結(jié)果:
文件大小:4.64M
起始內(nèi)存占用:9.97M
處理后內(nèi)存占用: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
{
private CommonService $service;
public function test(Request $request)
{
$s = 'M<br> 起始內(nèi)存占用:' . 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> 處理后內(nèi)存占用:' . number_format(memory_get_usage()/(1024*1024), 2)
. 'M<br> memory_limit:' . ini_get( 'memory_limit');
}
}
版本信息:php:8.0,Intervention\Image:2.7.2
有測試結(jié)果了,用了一個(gè)空的webman測試的。
在容器中起的服務(wù),上傳文件時(shí)會(huì)占用比較高的內(nèi)存,在主機(jī)上的服務(wù)上傳則是正常的;
這個(gè)問題有解決的辦法嗎?