When my PHP 7.4.33 website (running on linux) attempts to shrink/downsize an uploaded jpeg I get this error:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 16384 bytes)
I have carefully studied similar SO posts such as this and this.
Here is the code that crashes:
function ShrinkImage($sourcePath, $destinationPath, $maxWidth) {
// Get the original image dimensions
list($originalWidth, $originalHeight) = getimagesize($sourcePath);
// Calculate the new height while maintaining the aspect ratio
$newWidth = $maxWidth;
$newHeight = ($originalHeight / $originalWidth) * $newWidth;
// Create a new image resource
$newImage = imagecreatetruecolor($newWidth, $newHeight);
// Load the original image
$originalImage = imagecreatefromjpeg($sourcePath); // Change this line based on the image type (e.g., imagecreatefrompng for PNG)
// Resize the image
imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
// Save the resized image
imagejpeg($newImage, $destinationPath, 80); // Change this line based on the desired image format and quality (e.g., imagepng for PNG)
// Free up memory
imagedestroy($newImage);
imagedestroy($originalImage);
}
That code works fine on my local IIS-PHP-Windows11
setup. But on my Bluehost linux VPS it crashes with the error shown above.
I don't believe my PHP memory settings are too low, because all the jpegs I have tried (all of which cause the same error) are under 4 Mb.
Here are my PHP.ini settings on the server:
From studying similar SO posts it seems that this error most often occurs because of coding errors and not ini memory limits.
Any ideas?