PHP内存限制 [英] PHP Memory Limit

查看:112
本文介绍了PHP内存限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以给我一个如何使用内存资源的快速入门?我知道我可以通过PHP.ini以及代码行来增加PHP内存的限制:
$ b $ $ $ $ $ $ $ $ $ ini_set( memory_limit的, 24M);

我有一个正在编写的图像上传脚本,它使用了一个非常酷的PHP脚本叫simpleImage可以在这里找到: http:/ / /www.white-hat-web-design.co.uk/articles/php-image-resizing.php



我有一个基本的表单,接受JPG和PNG。我已经把我的PHP内存限制设置为24米,认为这将是足够高的,但是当我试图上传一个3MB的图像,我仍然收到美丽的内存分配用尽错误。把这个值颠倒到240M,脚本运行正常(在本地)。

我的脚本是这样的:
1)接受TMP正在上传图片
2)在图片上运行getimagesize()以检查它是否是有效的图片

3)将临时图像移动到最终的目的地目录。 />
4)使用简单的图片加载图片
5)使用简单的图片调整图片大小

6)使用简单的图片脚本保存调整大小的图片。

因此,我想所有的加载/检查/调整大小需要比24M多一点。但我担心什么是可以接受的内存限制分配。我想让用户能够舒适的上传〜6MB的图片。这是在你的平均专用服务器上是非常有压力吗?

这是我的脚本的主旨..个人,我不认为这是过度浪费的资源??

  if(!empty($ _ FILES)){
// get image
ini_set(memory_limit , 24M);
require_once('simpleImage.php');
require_once('db.php');
$ tempFile = $ _FILES ['file'] ['tmp_name'];
$ originalFile = $ _FILES ['file'] ['name'];
$ extension = strtolower(end(explode(。,$ originalFile)));
$ targetFile =path / to / directory /;

//验证图片
$ validExtensions = array('jpg','jpeg','png');
if(in_array($ extension,$ validExtensions)){
$ validExtension = true;
} else {
$ validExtension = false;


if(getimagesize($ tempFile)== false){
$ validImage = false;
} else {
$ validImage = true;


$ b if($ validExtension == true&& $ validImage == true){
if(move_uploaded_file($ tempFile,$ targetFile)) {

$ image = new SimpleImage();
$ image->载入($ targetFile);
$ image-> resizeToWidth(500);
$ image-> save($ targetFile);




$ div class =h2_lin>解决方案

JPG格式和PNG格式都是高度压缩的,因此一个3MB的文件,也就是600万像素,可以很容易地扩展到24MB的内存。添加输出图像和PHP自己的需求可以很容易地使内存使用跨越30MB。

实际的内存需求更多地取决于图像尺寸而不是文件大小。对于RGB图像(JPEG和不透明PNG)或百万像素* 4 MB,粗略估计将为 megapixels * 3 RGBA图像(透明PNG)。不要忘了,GD库本身可能会为了各种目的而保留临时的内部缓冲区,所以这个估计值应该是一个合理的内存限制。


Would anyone be able to give me a quick primer on how memory resources are used? I know I can up the PHP memory limit in PHP.ini as well as through lines of code such as:

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

I have an image upload script that I'm writing that is making use of a pretty cool PHP script called simpleImage which can be found here: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

I have a basic form that accepts JPGs and PNGs. I've set my PHP memory limit to 24m, thinking this would be high enough, but when I tried to upload a 3mb image I still received the beautiful memory allocation exhausted error. Bumping the value up to a meatier 240M and the script runs fine (locally).

My script does this:
1) Accepts the TMP Image being uploaded
2) Runs getimagesize() on the image to check that it's a valid image.
3) Moves the temporary image to the final destination directory.
4) Loads the image using simple image.
5) Resizes the image using simple image.
6) Saves the resized image using simple image script.

So I guess that all the loading/checking/resizing requires a bit more than the 24M. But I'm worried about what an acceptable memory limit allocation would be. I would like for users to comfortable be able to upload ~6MB images. Is this going to be extremely stressful on your average dedicated servers?

Here's the gist of my script.. personally I don't think it's excessively wasteful on resources??

    if (!empty($_FILES)) {
    // get image
    ini_set("memory_limit","24M");
    require_once('simpleImage.php');
    require_once('db.php');
    $tempFile             =     $_FILES['file']['tmp_name'];
    $originalFile        =    $_FILES['file']['name'];
    $extension             =     strtolower(end(explode(".", $originalFile)));
    $targetFile         =    "path/to/directory/";

    // validate image
    $validExtensions    =    array('jpg', 'jpeg', 'png');
    if(in_array($extension, $validExtensions)) {
        $validExtension        =    true;    
    } else {
        $validExtension        =    false;    
    }

    if(getimagesize($tempFile) == false) {
        $validImage        =    false;    
    } else {
        $validImage        =    true;    
    }


    if($validExtension == true && $validImage == true) {
        if(move_uploaded_file($tempFile,$targetFile)) {

            $image = new SimpleImage();
            $image->load($targetFile);
            $image->resizeToWidth(500);
            $image->save($targetFile);
        }
    } 
}

解决方案

Formats such as JPG and PNG are highly compressed and hence a 3MB file which is say 6 megapixels could easily expand to 24MB in memory. Adding in the output image and PHP's own requirements could easily make the memory usage cross 30MB.

The actual memory requirement depends more on the image dimensions rather than the file size. A rough estimate would be megapixels * 3 MB for RGB images (JPEGs and opaque PNGs) or megapixels * 4 MB for RGBA images (transparent PNGs). Don't forget that the GD library itself may maintain temporary internal buffers for various purposes, so double this estimate should be a reasonable memory limit to use.

这篇关于PHP内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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