Page 1 of 1

php.ini file question re: upload_max_filesize

Posted: Mon Nov 29, 2010 1:51 pm
by chopWood
everything was okay until I decided to increase the upload_max_filesize in my php.ini file.
My current php.ini is as follows:

register_globals = Off
File_uploads=on
upload_max_filesize = 10M

However I can't load a jpg image that is 4 mb. though I can load a 1.2 mb file okay.
My php system defaults to 32mb.

This is the error that I am getting when I attempt an upload:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4772 bytes) in /PhpSql/quickStart_Practice/phpPNEdevelopment/bobAskey/add_image.php on line 44

( it seems like the 33554432 bytes is referring to the 32MB limit that the system defaults to. If so, then it is ignoring my 10mb setting in the php.ini file. So, I'm confused about all this.)

This is line 44 which is within a function that resizes the image to given parameters:
$src_image = imagecreatefromjpeg($filename);

The thing is, before I started messing with the php.ini file, I was okay loading 4-6mb file sizes.

A related question is: Does the php.ini file need to be in the root directory or in the same directory as the php file that is doing the uploading.

------ update ----------

checked the phpinfo():

the max size, post max, are both set at 32mb
the executable time is set to 30

Where my php.ini file is set to lower than that, 10mb for max and post, it should work -I would think

I still get: Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4772 bytes) in /PhpSql/quickStart_Practice/phpPNEdevelopment/bobAskey/add_image.php on line 44
Also, where should the php.ini file go? I would think that it could go in the folder that contains the upload script.



thanks for your assistance,
Chop

Apache/2.0.63 (Unix)
PHP/5.2.11 DAV/2
MySQL client version: 5.1.37

Re: php.ini file question re: upload_max_filesize

Posted: Mon Nov 29, 2010 2:44 pm
by Darhazer
Maybe you are performing some manipulation on the image (using GD for example) that needs more memory.
What is the code of add_image.php ?

Re: php.ini file question re: upload_max_filesize

Posted: Mon Nov 29, 2010 3:16 pm
by chopWood
Here are the functions that create a thumb image and a large image for the image that has been "browsed" via a form

Code: Select all

// ================ FUNCTIONS FOR MAKING THUMB AND LARGE IMAGES =============


// -------------- make thumb ---------------------
function mkthumb($filename, $thumbname) {//tempname,real name

    /* generate an image thumbnail */
    $thumb_width = THUMBSIZE; 
    $thumb_height = THUMBSIZE; 

    if (preg_match('/\.gif$/i', $filename)) {
	$src_image = imagecreatefromgif($filename); 
    } else if (preg_match('/\.png$/i', $filename)) {
	$src_image = imagecreatefrompng($filename);
	
    } else {
	/* assume jpeg by default */
	$src_image = imagecreatefromjpeg($filename);// PROBLEM SEEMS TO COME FROM THIS LINE
    }

    $width = imagesx($src_image);
    $height = imagesy($src_image);

    if (($height > $thumb_height) || ($width > $thumb_width)) {
	/* create a thumbnail */
	if ($width > $height) {
	    $ratio = $thumb_width / $width; 
	} else {
	    $ratio = $thumb_height / $height;
	}

	$new_width = round($width * $ratio);
	$new_height = round($height * $ratio); 

	$dest_image = ImageCreateTrueColor($new_width, $new_height);
	imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0,
			   $new_width, $new_height, $width, $height); 
	imagedestroy($src_image); 
    } else {
	/* image is already small enough; just output */
	$dest_image = $src_image;
    }

    imagejpeg($dest_image, $thumbname); 
    imagedestroy($dest_image); 
    //die("end of thumb function");
} 

// --------------------- end make thumb -------------



// ================ END IMAGE UPLOAD FUNCTIONS =================

Re: php.ini file question re: upload_max_filesize

Posted: Mon Nov 29, 2010 4:53 pm
by cpetercarter
The error message is complaining that you are trying to exceed the php memory limit. It has nothing to do with the maximum size of file which you can upload. I think that, with your change to the upload_max_filesize, you are successfully uploading the 4mb image file, but then running out of memory when you try to manipulate it. You can increase the memory limit using ini_set(); or in php.ini. But you might also try to work out why your script requires more than 32MB to manipulate a 4MB image.

Re: php.ini file question re: upload_max_filesize

Posted: Mon Nov 29, 2010 5:13 pm
by chopWood
What you say makes sense. I hadn't known that there was such a thing as "php memory". However the .jpg file does not upload. My guess is that the script re-sizes the image before the image is uploaded and so hits an error.

What is the script for increasing php memory that I can put into my php.ini file? Also, is there a way to calculate how much php memory I would need to, say, re-size and then load an image that is 10mb? It is clear that upload_max_filesize needs to be 10mb or more, but how much memory would I need for php? Perhaps this information can be echoed to the screen in some way?

Someone else suggested post_max_size might need to be set but this defaults in phpinfo() to 32mb the same as upload_max_filesize.

I appreciate your assistance

p.s. I just found "memory_limit" in phpinfo(). The dfault is 32mb.

Re: php.ini file question re: upload_max_filesize

Posted: Tue Nov 30, 2010 2:29 pm
by cpetercarter
Think about it. The script cannot possibly resize an image that has not been uploaded!

I have seen it suggested (eg here) that resizing an image requires four times as many bytes of memory as there are pixels in the image. This very large memory use is the reason why it is important to destroy intermediate image resources when the resizing has been completed (as the code you are using does). So, very roughly, if your uploaded image has 8 million pixels or more, and your php memory limit is 32MB, then you are likely to run out of memory.

Try increasing the memory limit by putting

Code: Select all

ini_set('memory_limit', '64M');
at the top of the file where your 'out of memory' error occurs.

Re: php.ini file question re: upload_max_filesize

Posted: Tue Nov 30, 2010 5:11 pm
by chopWood
I did some experimenting with the "memory_limit" with the following results for loading and resizing a .jpg image that is 4.4mb in size:

memory_limit = 64mb or less - complete failure, no image uploaded

memory_limit = (from 80mb to 88 mb) - the image DID upload and re-size but I continued to get a memory error anyway.

memory_limit = 92mb - this is the lowest value I tried where everything worked and received no error message.


I didn't try all the in between values but it is apparent that I need a memory_limit of at least 20 times the size of the .jpg file

I've been doing this locally, on my own computer. I'm wandering if the mem_size will be an issue from a remote server?

Thank you for your help in steering me to the mem_size parameter. I guess my issue is solved for now.

Re: php.ini file question re: upload_max_filesize

Posted: Wed Dec 01, 2010 1:05 am
by cpetercarter
I am glad that you have found a solution.

Remember that jpeg is a compressed format. A 4.4MB image file will not contain just 4.4 million pixels - it may contain several times that number. Your underlying problem is that you are using php's image manipulation functions to resize some very large images, and php requires a lot of memory to do this.