Connecting Tech Pros Worldwide Help | Site Map

GD library memory problem

Christian Giordano
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi guys I've just tryed to upload a 1 MB image and resize it in a new
hosting (media temple) and it gives me this error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to
allocate 6400 bytes) in /home/httpd/vhosts/....php

where with the GD Library a create a kind of copy:

$source = imagecreatefromjpeg($file);

Does someone know how to solve this issue?


cheers, chr
Erwin Moller
Guest
 
Posts: n/a
#2: Jul 17 '05

re: GD library memory problem


Christian Giordano wrote:

Hi Christian,

This question is posted once every day, it seems.
(Is it in the FAQ alreadY?)
[color=blue]
> Hi guys I've just tryed to upload a 1 MB image and resize it in a new
> hosting (media temple) and it gives me this error:
>
> Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to
> allocate 6400 bytes) in /home/httpd/vhosts/....php
>
> where with the GD Library a create a kind of copy:
>
> $source = imagecreatefromjpeg($file);
>
> Does someone know how to solve this issue?
>[/color]

The problem lies in the fact that you assume that a 1 megabyte image takes
up 1 magebyte of internal memory.
This is not the case.
First the image is compressed, so if PHP needs an internal representation of
all pixels, it needs to unpack it.

A rude estimate:
If your image is 100 x 500 pixels:
that is 50.000 pixels
Every pixel needs 24 bits to store (RGB+alpha)
So you need 1.200.000 bytes (around 1.2 meg)

How to fix it?
Increase the memory that PHP can allocate (in php.ini) or work with smaller
files (if possible).

Regards,
Erwin Moller
[color=blue]
>
> cheers, chr[/color]
Erwin Moller
Guest
 
Posts: n/a
#3: Jul 17 '05

re: GD library memory problem


Erwin Moller wrote:

(correction)
[color=blue]
> A rude estimate:
> If your image is 100 x 500 pixels:
> that is 50.000 pixels
> Every pixel needs 24 bits to store (RGB+alpha)
> So you need 1.200.000 bytes (around 1.2 meg)[/color]

I messed up bits and bytes and math here.
sorry. :P

1 pixel needs 4 bytes (32 bits) if alpha-channel is used.

so you need 200.000 bytes for 50.000 pixel.

In your situation you exceeded the 8.000.000 bytes, so your original image
is over 2.000.000 pixels (eg 2000 x 1000 pixels)

Regards,
Erwin Moller
[color=blue]
>
> How to fix it?
> Increase the memory that PHP can allocate (in php.ini) or work with
> smaller files (if possible).
>
> Regards,
> Erwin Moller
>[color=green]
>>
>> cheers, chr[/color][/color]

Ian Fleeton
Guest
 
Posts: n/a
#4: Jul 17 '05

re: GD library memory problem


Erwin Moller wrote:[color=blue][color=green]
>>How to fix it?
>>Increase the memory that PHP can allocate (in php.ini) or work with
>>smaller files (if possible).
>>
>>Regards,
>>Erwin Moller
>>
>>[color=darkred]
>>>cheers, chr[/color][/color]
>
>[/color]

You can also use getimagesize() to calculate the number of pixels /
memory required before creating the bitmap--that way you can provide a
pleasant warning message to the user rather than run out of memory.

Kind regards,
Ian Fleeton
Christian Giordano
Guest
 
Posts: n/a
#5: Jul 17 '05

re: GD library memory problem


> How to fix it?[color=blue]
> Increase the memory that PHP can allocate (in php.ini) or work with smaller
> files (if possible).[/color]

thx a lot erwin! I'll pay more attention next time to search better
before posting, sorry to all!

cya, chr
Closed Thread