HOW THE CHECK THE MEMORY BEFORE CREATING AN IMAGE?
I worked on a script where I delt with large images. Very often the error "out of memory" occured. So I had to figure out, how to check the memory BEFORE creating an image. I didn't find a solution on the web, so I ran my own test script. It built lots of images to compute the needed memory. I found out, that the formula mem = x * y * channel was not enough. There was an additional multiplier that varied. As a result 1.7 worked best. So the formula is: mem = x * y * channel * 1.7.
I post this script here hoping it is useful to someone who will run into the same problems.
Code (PHP)
<?php
//-------------------------------------------------- DEFINE MAXMEM
define ("MAXMEM", 32*1024*1024); //--- memory limit (32M) ---
//-------------------------------------------------- ENOUGH MEMORY ?
function enoughmem ($x, $y, $rgb=3) {
return ( $x * $y * $rgb * 1.7 < MAXMEM - memory_get_usage() );
}
//-------------------------------------------------- SIMPLE EXAMPLE
list ($x, $y) = @getimagesize ('your_img.jpg'); //--- get size of img ---
if (enoughmem($x,$y)) {
$img = @imagecreatefromjpeg ('your_img.jpg'); //--- open img file ---
$thumb = 200; //--- max. size of thumb ---
if ($x > $y) {
$tx = $thumb; //--- landscape ---
$ty = round($thumb / $x * $y);
} else {
$tx = round($thumb / $y * $x); //--- portrait ---
$ty = $thumb;
}
if (enoughmem($tx,$ty)) {
$thb = imagecreatetruecolor ($tx, $ty); //--- create thumbnail ---
imagecopyresampled ($thb,$img, 0,0, 0,0, $tx,$ty, $x,$y);
imagejpeg ($thb, 'your_thumbnail.jpg', 80);
imagedestroy ($thb);
}
imagedestroy ($img);
}
//--------------------------------------------------
//--- to check the memory working with ---
//--- b/w-image or gif use: ---
//--------------------------------------------------
$check = enoughmem ($x, $y, 1);
?>
Taking the opportunity, I thank everyone here at php.net for the great manual and the useful user scripts.