分配了足够的内存来加载图像,说它用尽了

时间:2014-02-27 18:59:36

标签: php

尝试将图像加载到内存时出现此错误...

  

允许的内存大小为10485760字节耗尽(尝试分配   18048字节)

//attempt to open image
ini_set('memory_limit', '10M');
if($im = imagecreatefromjpeg($_GET['imgname'])) {
    //See if there is a width or height constraint
    if (isset($_GET['restraint'])) {
        $constr = $_GET['restraint'];
        if ($constr == "width") {
            if (imagesx($im) > imagesy($im)) {
                $im = imagerotate($im, 90, 0);
            }
        } elseif ($constr == "height") {
            if (imagesy($im) > imagesx($im)) {
                $im = imagerotate($im, 90, 0);
            }
        }
    }
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
}

有什么想法吗?

编辑:

memory_get_usage = 125768 memory_get_peak_usage = 127016

1 个答案:

答案 0 :(得分:1)

JPEG是一种压缩类型的图像,也许当图像加载得到解压缩时,我有类似的情况,但不是在php(.net紧凑框架)中:

  

即使压缩图像大小为200kb,当您将其加载为   位图将被解压缩并存储在本机Bitmap的内存中   格式。给定高度和宽度各1500px,并假设位图   格式为32bpp(未指定时的默认值),您正在查看   9MB的已分配内存

     

1500 * 1500 * 4 = 9MB。

看看这个:

http://www.dotsamazing.com/en/labs/phpmemorylimit

您也可以尝试在php.ini中直接设置内存限制并重启apache。

http://www.php.net/manual/en/ini.core.php#ini.memory-limit

尝试使用大型(512M)

这是我的问题,但是,它不是与PHP有关,而是与图像大小/内存相关 OutOfMemoryException loading big image to Bitmap object with the Compact Framework