PHP内存限制已用完25MB-文件上载/裁剪/调整大小 [英] PHP Memory Limit 25MB Exhausted - File Upload/Crop/Resize

查看:136
本文介绍了PHP内存限制已用完25MB-文件上载/裁剪/调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用单个图像上传/裁剪/调整大小脚本来存储最大10MB的文件.

I'm using a single image upload/crop/resize script for files up to 10MB.

在测试时,我将php_ini的内存限制设置为25M,而仅上传约1.4MB的文件时就用光了.

When testing, I set php_ini memory limit to 25M and that is exhausted when uploading a file only about 1.4MB.

"Allowed memory size of 26214400 bytes exhausted (tried to allocate 10368 bytes)"

这对我来说似乎很奇怪,不是10368< 26214400? (反问)

This seems strange to me, isn't 10368 < 26214400? (Rhetorical Question)

或者这意味着我在25MB的内存上占用了10368个字节?我的脚本应该使用这么多的内存吗?

Or does that mean I went 10368 bytes over 25MB? Should my script be using this much memory?

代码:

function make_thumbnails($updir, $img)
{
    $thumbnail_width    = 200;
    $thumbnail_height   = 150;
    $thumb_preword  = "thumb_";

    $arr_image_details  = GetImageSize($updir.$img);
    $original_width = $arr_image_details[0];
    $original_height    = $arr_image_details[1];

    if( $original_width > $original_height ){
        $new_width  = $thumbnail_width;
        $new_height = intval($original_height*$new_width/$original_width);
    } else {
        $new_height = $thumbnail_height;
        $new_width  = intval($original_width*$new_height/$original_height);
    }

    $dest_x = intval(($thumbnail_width - $new_width) / 2);
    $dest_y = intval(($thumbnail_height - $new_height) / 2);

    if($arr_image_details[2]==1) { $imgt = "ImageGIF";  $imgcreatefrom = "ImageCreateFromGIF";  }
    if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG";  }
    if($arr_image_details[2]==3) { $imgt = "ImagePNG";  $imgcreatefrom = "ImageCreateFromPNG";  }

    if( $imgt )
    {
        $old_image = $imgcreatefrom($updir.$img);
        $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imageCopyResized($new_image,$old_image,$dest_x,
        $dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
        $imgt($new_image,$updir.$thumb_preword.$img);

        // Added by your suggestions:
         imagedestroy($old_image);
         imagedestroy($new_image);
    }
}

推荐答案

该错误消息表明它尝试分配另外10368个字节,但它不可用.换句话说,您的25MB池已用尽,脚本无法分配所需的10368个字节.

The error message says that it tried to allocate another 10368 bytes, but it wasn't available. In other words, your 25MB pool was exhausted and the script couldn't allocate the 10368 bytes it needed.

您可以通过添加或更新类似于以下内容的行来增加php.ini文件中的此限制:

You can increase this limit in your php.ini file, by adding or updating a line similar to the following:

memory_limit = 64M

至于如果25MB足够",这是我们很难回答的问题...当然,确实有一些脚本合法地需要64MB甚至更多.如果您发现必须不断增加内存池的大小,那么我将开始研究占用大量空间的内容.

As far as "if 25MB is enough," that is a difficult question for us to answer... There are certainly some scripts out there that legitimately need 64MB or even more. If you find that you have to constantly increase the size of your memory pool, I'd start looking into what's taking so much space.

此处是一个相关问题,该问题在大小上有很好的讨论您的内存池中.

Here is a related question that has some good discussion on the size of your memory pool.

编辑:看到您的代码已发布,使用图像时肯定有可能消耗大量内存.完成操作后,可以通过调用 imagedestroy 来帮助您的脚本.图片资源.

EDIT Seeing your code posted, it is definitely possible to consume a large amount of memory when you are working with images. You can help your script by calling imagedestroy after you are done with an image resource.

这篇关于PHP内存限制已用完25MB-文件上载/裁剪/调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆