【发布时间】:2015-03-28 19:28:49
【问题描述】:
我正在尝试将图像上传到我的本地主机。没问题,如果是小图像(不超过 1 MB)。但是当我尝试上传大约 3MB 的图像时。 php说
致命错误:C:\Users\Tharit\Desktop\Work\Pixelbar\Code\ecompro\vendor\intervention\image\src\Intervention\Image\ 中允许的内存大小为 134217728 字节已用尽(试图分配 18048 字节)第 115 行的 Gd\Decoder.php
不应该这么多。这是代码,在图片上传部分。
if (Input::hasFile('profile_picture'))
{
$old_profile_pic= $user->profile_pic;
$profile_picture = Input::file('profile_picture');
$profile_picture_size=getimagesize($profile_picture);
if($profile_picture_size[0]>$profile_picture_size[1])
{
$mainside = $profile_picture_size[1];
$cordx=($profile_picture_size[0]-$profile_picture_size[1])/2;
$cordx=(int) $cordx;
$cordy=0;
}
else
{
$mainside = $profile_picture_size[0];
$cordy=($profile_picture_size[1]-$profile_picture_size[0])/2;
$cordy=(int) $cordy;
$cordx=0;
}
$filename = time() .$user->id . '.' . $profile_picture->getClientOriginalExtension();
$path = public_path('img/user/' . $filename);
Image::make($profile_picture->getRealPath())
->crop($mainside,$mainside,$cordx , $cordy)
->resize(100, 100)
->save($path);
$user->profile_pic = 'img/user/'.$filename;
if($old_profile_pic != 'img/user/default_profile.gif')
{$imagecheck=1;}
}
【问题讨论】:
-
我怀疑问题出在您调整图像的大小上。如果这是使用 gd,那么您需要为调整大小前后的图像提供图像高度 x 宽度 x 4 字节的内存
-
@MarkBaker 你的意思是前置+后置?
-
我的意思是你需要留出足够的内存来同时在 PHP 内存中保存完整大小的图像和缩略图
-
创建新图像的内存使用量将比上传的图像大小大得多。您上传的图片很可能是压缩格式。当您运行此操作时,它正在创建一个真彩色图像,因此内存使用量将类似于 Height * Width * Channels * (magical constant);因此,您上传的图像一旦解压缩,可能会占用比您预期的更多的内存。本例中的“神奇常数”为 1.7(取自php.net/manual/en/function.imagecreatetruecolor.php#99623);
-
@StephenMtl 这对我来说是新事物。我应该学习更多!谢谢
标签: php laravel laravel-4 memory-leaks