PHP has allowed memory usage until I see any signs of this

I have this simple php script that contains only the following lines

$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); 

At startup, I get this output

 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 

How is it possible that downloading a file from 952Kb will force PHP (imagecreatefrompeg) to go to the allowed 64Mb of memory? Any ideas?

+4
source share
2 answers

Just because the JPG file is only 952 kilobytes, this does not mean that it cannot take up a lot of memory VERY , for example, a simple test with a clean white image of 2048x2048 creates a file of size 59kbyte.jpg.

This file is unpacked into a 2048x2048x3 = 12.6 megabyte raw bitmap in GD.

You can get a rough estimate of how much memory GD will need to download / decompress the image with:

 $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"; 
+8
source

It is possible that the raw image or internal image format will exceed the memory limit.

Try to upload a large picture and save it as a bitmap, this is huge ..

Each pixel occupies 3 bytes (24-bit color mode), 0-255 red 0-255 green , 0-255 blue .

When opening a file with an additional alpha channel imagealphablending(); you have 4 bytes per pixel (32-bit color mode), which makes an extra 0-255 for alpha blending .

 2,5 MB JPG (100% quality) ~ 36,0 MB bitmap - 12 megapixel 4,1 MB JPG (100% quality) ~ 96,0 MB bitmap - 32 megapixel 

compare with this tool:

http://web.forret.com/tools/megapixel.asp

see wikipedia:

http://en.wikipedia.org/wiki/True_Color#True_color_.2824-bit.29

+1
source

Source: https://habr.com/ru/post/1488116/


All Articles