[SOLVED] Not enough memory for imagecreatefromjpeg()

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

[SOLVED] Not enough memory for imagecreatefromjpeg()

Post by Skittlewidth »

I'm trying to create a feature for a client's CMS that will allow them to upload 3 images straight off a digital camera, have them resized 3 different ways (thumbnail, large, and one specifically to go in a PDF) and I wrote a script that did that fine, plus created a pdf. The script ran sucessfully both locally and on the server.

The trouble was I was just uploading various jpegs I had scattered about my machine. The photos I was experimenting with came off my really old 2megapix Canon Ixus V and were all <300k and 1024x768.

It then occured to me that newer digicams produce much larger images and uploading 3 at a time would possibly exceed the max upload limit on our hosts PHP configuration, so I rewrote the script to handle one image at a time, and got a few images to test with from my boss's slightly newer camera which were 1600 x 1200 and 655kb.

Everytime I uploaded the image the script went to a file not found error after a long struggle. Since the page was submitting to itself this seems ridiulous. I would have expected a timeout error perhaps.

So I made a test script to upload the image, and make a smaller copy from it, then display it in the browser.
Each time I get the following error:

Code: Select all

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 4800 bytes) in /home/fhlinux202/i/ib3.co.uk/user/htdocs/alltask/admin/test.php on line 10
Line ten was where imagecreatefromjpeg() was called.

The entire script is as follows:

Code: Select all

$imagetmp = $_FILES['pic1']['tmp_name'];
		
		$new_width = 150;
		
		list($width, $height, $type, $attr) = getimagesize($imagetmp);

		$tempImage = imagecreatefromjpeg($imagetmp);
		$new_height = round($height/($width/$new_width));

        // Resample
        $newImage  = imagecreateTrueColor($new_width, $new_height)
            or die("<B>Error: New image could not be created.</B> ");

        // Do a bicubic resample
        imagecopyresampled($newImage, $tempImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
		  
		// Output 
        
        header("Content-type: image/jpeg"); 
		imagejpeg($newImage);
I don't honestly think this has anything to do with my problem but in the manual for imagecreatefromjpeg someone claims that images from a Canon PowerShot S70 causes PHP to crash with a page canot be displayed error. The image I am using came from a Powershot A40.

Someone please tell me that I've just written a poor script and show me how to fix it!
Last edited by Skittlewidth on Fri Dec 16, 2005 3:22 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Not enough memory for imagecreatefromjpeg()

Post by onion2k »

Skittlewidth wrote:The trouble was I was just uploading various jpegs I had scattered about my machine. The photos I was experimenting with came off my really old 2megapix Canon Ixus V and were all <300k and 1024x768.
... ... ...
Someone please tell me that I've just written a poor script and show me how to fix it!
The bad news is that it's probably not your code that the problem.

First thing to be aware of is that the filesize of an image is basically meaningless when it comes to GD. The filesize is for the compressed image data, when you use imagecreatefromjpeg() it all has to be decompressed. A 1024*768 jpeg file will take up approximately 3.1 megabytes of RAM. When the image is actually opened it can need quite a lot more space than that to do that actual decompression. An image of 1024 x 768 should be fine on a standard PHP install (default is up to 8 megabytes per script). However, I've seen lots of installations that only allow up to 2 megabytes per script. Take a look through your PHP.ini file for "memory_limit" .. it's in the Resource Limits section. If it's set to something low .. increase it.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Doh!
Our hosts have set the memory limit to 8mb, and as far as I understand you can't override this with ini_set() - I think I've asked that question before.

And on top of that FastHosts won't let you do it with an .htaccess file either.

A few more experiments whilst I was waiting confirmed what you just said, the image file size does not matter so much as the contents of the image, range of colours etc once you get above 1024 x 768.

So I guess I'm going to have to tell my boss that under the current hosting conditions this can't be done. We were really hoping to be able to let the client bypass the image editing stage and just download the images straight from the camera and leave the rest to the script.

We'll just have to get them to run a batch file to resize the photos before uploading or get them to take them at a lower quality setting on the camera perhaps.

Grr I hate compromise!
:wink:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

There is a possible way around it.. use ImageMagick instead of GD. ImageMagick is external to PHP so it doesn't count toward the memory limit. I think. Not 100% sure though.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

I was wrong... you can over-ride the the Memory Limit with ini_set, on our host at least, so problem solved for the time being!

:D
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Skittlewidth wrote:I was wrong... you can over-ride the the Memory Limit with ini_set, on our host at least, so problem solved for the time being!
Hmm .. I wonder if that's possible on my host too .. Something to investigate .. I set it to 512M locally .. I'm sure they'd love that on my host's server :)
Post Reply