K
K
Ken Jee2016-10-24 13:04:54
Yii
Ken Jee, 2016-10-24 13:04:54

How to resolve the error processing large images Allowed memory size on Yii2 via \yii\imagine\Image?

There is a project on Yii2. When trying to process a large image using the \yii\image\Image module, an out-of-memory error occurs.
Specific example.
I am uploading a 3.94 MB JPG file (the size of the image itself is 3024x5386 pixels). And when trying to process open this image, an error occurs:

code:1
file:"[PROJECT PATH]\vendor\imagine\imagine\lib\Imagine\Gd\Image.php"
line:622
message:"Allowed memory size of 268435456 bytes exhausted (tried to allocate 65028097 bytes)"
name : "PHP Fatal Error"

Here is the \vendor\imagine\imagine\lib\Imagine\Gd\Image.php code:
...
private function createImage(BoxInterface $size, $operation)
    {
        $resource = imagecreatetruecolor($size->getWidth(), $size->getHeight());

        if (false === $resource) {
            throw new RuntimeException('Image '.$operation.' failed');
        }

        if (false === imagealphablending($resource, false) ||
            false === imagesavealpha($resource, true)) {
            throw new RuntimeException('Image '.$operation.' failed');
        }

        if (function_exists('imageantialias')) {
            imageantialias($resource, true);
        }

        $transparent = imagecolorallocatealpha($resource, 255, 255, 255, 127);
        imagefill($resource, 0, 0, $transparent); // <---------- СТРОКА 622 В КОТОРОЙ ПРОИСХОДИТ ОШИБКА
        imagecolortransparent($resource, $transparent);

        return $resource;
    }
...

Here are the relevant options from PHP.INI:
...
realpath_cache_size=256M
memory_limit = 256M
...
upload_max_filesize = 5M
post_max_size = 5M
...

Is it really necessary to process such a graphic file (I described the characteristics above) more than 256 MB of memory ??? Or is there some other way to solve this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Timofeev, 2016-10-24
@webinar

There are several options:
the most correct one is to change the gd library to Imagick, it consumes several times less memory (10 times). but if there is none on the server and there is no way to install it, then either increase the RAM on the server, or set a limit in the rules of the model for the maximum photo size.

M
Maxim, 2016-10-24
@pudovMaxim

message:"Allowed memory size of 268435456 bytes exhausted (tried to allocate 65028097 bytes)"
Says 256 MB bye, I want another 64 MB. I have no idea where to get it, so I went to bed.
On a test environment, try to increase the memory size and see how much you need. But it also looks really big. It may be taking too much somewhere.
I would also check if the image size is determined correctly here

$resource = imagecreatetruecolor($size->getWidth(), $size->getHeight());

S
Sergei Iamskoi, 2016-10-25
@syamskoy

Как уже написали, самое быстрое и хорошее решение: apt-get install php7.0-imagick. Подставить свою версию php.
В код изменений вносить не требуется, т.к. расширение само выбирает нужную библиотеку.

A
alexeytru, 2017-08-13
@alexeytru

gc_collect_cycles(); - принудительный запуск сборщика мусора перед обработкой может помочь.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question