Resize multiple images in php without exceeding the memory limit

I am currently trying to get a form that will allow uploading and changing multiple images on a server using PHP. Each uploaded image by the client is about 2.5 MB.

I am currently using the move_uploaded_file() function.

No problem moving files to the server. The problem occurs when I try to crop. Not having ImageMagick on my host, I use this setting (not all the code, as far as relevant, is in a loop with $width , etc., Changing for different cropping sizes)

 $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, $output_filename, 80); 

In its current form, this will only work for two images. If there are 3 or more submitted, I get an "exhausted memory" error. I researched this since my memory limit is 120 MB. Apparently, the imagecreatefromjpeg function uses a lot of memory, especially if the file has a high resolution (which is mine, so I need them, cropped / modified).

Does anyone know a more efficient approach to this task? I researched Google, but everyone uses the same technique as me.

+4
source share
1 answer

Use imagedestroy to clear any memory associated with $ image and $ image_p:

 $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, $output_filename, 80); imagedestroy($image); imagedestroy($image_p); 
+5
source

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


All Articles