PHP允许内存使用耗尽,而我看不到它的迹象

use*_*932 4 php

我有这个简单的PHP脚本,只包含以下几行

$mem = memory_get_usage()/1024;
$mem = $mem/1024;
echo "mem: ".$mem ."Mb<br>";
$max =  ini_get('memory_limit');
echo "max is $max<br>";

$filename = 'upload/orig/CID_553.jpg';              
$filesize = (filesize($filename) / 1024);
echo "filesize is $filesize Kb<br>";        
$img_pointer = imagecreatefromjpeg($filename);
Run Code Online (Sandbox Code Playgroud)

运行时,我得到了这个输出

mem: 0.30711364746094Mb
max is 64M
filesize is 952.2666015625 Kb
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 13056 bytes) in C:\temp_checkmem.php on line 13 
Run Code Online (Sandbox Code Playgroud)

如何加载952Kb的文件会使PHP(imagecreatefrompeg)陷入允许的64Mb内存?有任何想法吗?

Mar*_*c B 8

仅仅因为JPG文件只有952k字节并不意味着它不能占用非常大量的内存,例如使用2048x2048纯白图像进行简单测试就会生成59kbyte的.jpg文件.

该文件将解压缩为2048x2048x3 = 12.6GD中的兆字节原始位图.

您可以粗略估计GD需要多少内存来加载/解压缩图像:

$stats = getimagesize($filename);
$memory_estimate = $stats[0] * $stats[1] * 3; // height * width * 3 bytes per pixel
echo "{$stats[0]}x{$stats[1]} -> {$memory_estimate} bytes";
Run Code Online (Sandbox Code Playgroud)