Preventing Memory Leaks in PHP

Image resizing need memory. I use this script to prevent memory leak.

I use Intervention Image library for resizing I often get ‘Allowed memory size of …. bytes exhausted (tried to allocate … bytes)’ message.

////////////////////////////////////////////////////
//get memory_limit info from php.ini
$memoryAvailable = filter_var(ini_get(“memory_limit”), FILTER_SANITIZE_NUMBER_INT) * 1048576;

//getting the image width and height
$imageInfo = getimagesize($destinationPath . ‘/’ . $filename);

//This is quite rough and includes a fudge factor, 2.5, which you may want to experiment with.
$requiredMemory = ( $imageInfo[0] * $imageInfo[1] * ($imageInfo[‘bits’] / 8) * $imageInfo[‘channels’] * 2.5 );

//check memory usage
if (memory_get_usage() + $requiredMemory < $memoryAvailable) {

//we have enough memory to resize the image.
}
////////////////////////////////////////////////////

Blogbook : PHP | Javascript | Laravel | Corcel | CodeIgniter | VueJs | ReactJs | WordPress