Page 1 of 1

php script memory leak?

Posted: Wed Nov 17, 2004 3:48 am
by roobottom
I have the same script running on 2 servers from the same provider, with supposedly the exact same setup, however, one works, and the other (on the production server - typical) gives me a memory error...
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 6400 bytes) in /home/fhlinux197/r/roobottom.com/user/htdocs/photos.php on line 24
The touble is, I think 8Mb is enough to be running this script, can anyone see where i'm using all that memory?? Each of the pics i'm re-processing is about 750K, but that wouldn't make any difference to the script would it??

Here's my code...

Code: Select all

//process pic function-----------------------------------------------------------------
$new_width=100;  //Image width Change if needed
$new_height=100;  //Image height Change if needed 
//JPEG function
function thumb_jpeg($image_name, $subFolder)
{
	global $new_width;
    global $new_height;
	global $global_photoRoot;
	
	if(file_exists($global_photoRoot.$subFolder."th_".$image_name)) {
		//the file already exists
	} else {
	
		//resize but constrain proportions...
		list($width_orig, $height_orig) = getimagesize($global_photoRoot.$subFolder.$image_name);
			if ($width && ($width_orig < $height_orig)) {
   				$new_width = ($new_height / $height_orig) * $width_orig;
			} else {
   				$new_height = ($new_width / $width_orig) * $height_orig;
			}

		$destimg=imagecreatetruecolor($new_width,$new_height) or die("Cannont create GD2 image");
		$srcimg=ImageCreateFromJPEG($global_photoRoot.$subFolder.$image_name) or die("Problem In opening Source Image");
		imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,$width_orig,$height_orig) or die("Problem In resizing");
		ImageJPEG($destimg,$global_photoRoot.$subFolder."th_".$image_name) or die("Problem In saving");
	}
}

//--------------------------------------------------------------------------------
The error message relates to line 24 of the above formatting.
Thank You in advance.

Posted: Wed Nov 17, 2004 4:31 am
by Weirdan
The minimum amount of memory required to store an image in memory is calculated as:

Code: Select all

$size = getimagesize("/path/to/image.jpg");
echo $size[0]*$size[1]*$size['bits']*3/4 . " bytes\n"; // for RGB images
echo $size[0]*$size[1]*$size['bits'] . " bytes\n"; // for CMYK images
[php_man]memory_get_usage[/php_man] might be of help too.

Posted: Wed Nov 17, 2004 5:09 am
by roobottom
ahhh... I see, GD doesn't store the Jpeg compressed, it reads every pixel, so this has go more to do with the actual pixel size of the pictures that i'm trying to re-prossess. The output size I get for all the pics is (according to Google) is..
11 520 000 bytes = 10.9863281 megabytes
I'm guessing that my hosting comapny has different settings for out test and production servers...

Does all this sound right??