Magick / PHP image drops with large images

I have a PHP script that is used to resize images in a custom FTP folder for use on its website.

While slow resizing, the script completed correctly with all images in the past. However, recently a user uploaded an album of 21-megapixel JPEG images, and, as I already found, the script cannot convert the images, but does not produce any PHP errors. When I consulted with various logs, I found that several Apache processes were killed with Out Of Memory errors.

The functional part of the PHP script is essentially a for loop that iterates through my images on disk and calls a method that checks if a thumbnail exists and then does the following:

$image = new Imagick();
$image->readImage($target);
$image->thumbnailImage(1000, 0);
$image->writeImage(realpath($basedir)."/".rescale."/".$filename);
$image->clear();
$image->destroy();  

The server has 512 MB of RAM, usually at least 360 MB + free.

PHP has a memory limit currently set at 96 MB, but I set it higher without any impact on the problem.
According to my estimates, a 21-megapixel image should occupy an 80 MB + area when uncompressed, and therefore I am puzzled by why RAM disappears so quickly if Image Magick objects are not deleted from memory.

- script, ?

RAM, ?

+3
3

$image->setSize() $image->readImage(), libjpeg , .

(), : JPEG PHP

+2

. .

imagick , PHP, PHP .

new Imagick():

// pixel cache max size
IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 32);
// maximum amount of memory map to allocate for the pixel cache
IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 32);

, imagick ( -/tmp), 32 . , (/tmp ramdisk, , imagik ).

+3

MattBianco is almost correct, the only change is that the memory limits are in bytes, so there would be 33554432 for 32 MB:

// pixel cache max size
IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 33554432);
// maximum amount of memory map to allocate for the pixel cache
IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 33554432);
+2
source

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


All Articles