Answer the question
In order to leave comments, you need to log in
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"
...
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;
}
...
...
realpath_cache_size=256M
memory_limit = 256M
...
upload_max_filesize = 5M
post_max_size = 5M
...
Answer the question
In order to leave comments, you need to log in
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.
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());
Как уже написали, самое быстрое и хорошее решение: apt-get install php7.0-imagick. Подставить свою версию php.
В код изменений вносить не требуется, т.к. расширение само выбирает нужную библиотеку.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question