⬆️ ⬇️

Lack of memory on image processing hosting

On my site, users upload images that are reduced to the required size (maximum side - 800 points). And faced with the limitation of remembering PHP scripts, 7 megapixels were no longer enough memory to process the photos, and now cameras and more will not force users to crop the photos before sending, many will not upload them at all.



The error was on the line:

$image = imagecreatefromjpeg($filename);
PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 3072 bytes) in ...



To solve the problem, you need to reduce the original photo. ImageMagick was not on the hosting, so I don’t know if it was possible to solve the problem through this library.

But it turned out that you can execute an external command via exec ();

')

Solution to the problem:

$filename = 'image.jpg';

$maxside = 800;

$size = getimagesize($filename);

if (($size[0] > $maxside) or ($size[1] > $maxside)) {

exec ( 'mogrify -resize '.$maxside.'x'.$maxside.' '.$filename);

$size = getimagesize($filename);

}

$image = imagecreatefromjpeg($filename);


PS: A search in Google showed several forums where the problem was suggested to be solved in the forehead - by reducing the memory limit, by agreement with the hosting administrators.

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



All Articles