Image uploader

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Image uploader

Post by Da_Elf »

Ive created an image uploader which ive used on a few sites. its rather simple. however i was testing with a semi large image and got an error.

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 9088 bytes)

the file size is 870kb and the file uploads to the server. i can see it there. however my uploader resizes the jpgs as well and the fatal error is in the area here on the 2nd line listed

Code: Select all

$tn = imagecreatetruecolor($modwidth, $modheight) ; 
$image = imagecreatefromjpeg($file) ; 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;  
how do i allocate more memory if thats whats needed
Last edited by Da_Elf on Fri Oct 16, 2009 5:14 pm, edited 1 time in total.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Image uploader

Post by Mark Baker »

Resizing an image using gd is notoriously memory intensive, your only real solution is to increase the memory limit for your scripts in php.ini
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: Image uploader

Post by Da_Elf »

it seems my php.ini file says 32MB. why would it max out at 32mb when the original file isnt even 1mb
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Image uploader

Post by Mark Baker »

Da_Elf wrote:it seems my php.ini file says 32MB. why would it max out at 32mb when the original file isnt even 1mb
You'd have to look at the innards of the GD library to get an answer to that.

Your file might seem small, but most image files use compression.... it needs to be decompressed before it can be manipulated (e.g. for thumbnailing)
Somebody might actually know the answer, I can only surmise: but I'd suspect that it's taking your original image and converting it to a bitmap internally. Then, if your original image was 320x200 pixels, it would require 256k or memory (4 bytes/pixel) just to hold it in memory. Resizing would require two copies, so 512k. That's just for a small image. 640x480 would take 2.4M.

As I said previously: for whatever reason, any GD image manipulation is know to take large volumes of memory, so you need to make allowance for this when configuring your server to work with large images.
Your alternative, if you don't want to increase PHP memory, is to use external image libraries and call them using exec.
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: Image uploader

Post by Da_Elf »

im getting sick of the GD library with resizing images. i had to increase a the file limit to 50M and a 2.3mb jpg still couldnt be processed by it. what are the other options for resizing images for thumbnails
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Image uploader

Post by McInfo »

Edit: This post was recovered from search engine cache.
Post Reply