Understand GD Library

13May08

In the forums I received very often the question :

I got the failure “Fatal error: Out of memory ” or “Error : Exceed Memory limit. Require : xx.xx MByte”, but my image has only the size of 1 MB ?

After I answered this question now 1000 times , I hope this post will clarify this point. Let’s create a white image with the size of 640 x 480 (It’s the VGA resolution) and open this with a image viewer like IrfanView.

White image

Let’s have a look at the bottom line :

640×480 Size

The image requires a disk space (the file size) of 5.30 KB and a memory size of 900.04 KB

Now increase the resolution to 1024 x 768 :

1024 Size

The image requires still the file size of 5.30 KB but a memory size of 2.25 MB

Now look for a 4 Megapixel image (Resolution 2304 x 1728) :

4 MP Size

The required memory size increases to 11.39 MB

And a 7.1 MP camera picture :

7.1 MP Size

We need now 20.25 MB memory , but the file size is still 5.30 KB.

What do we learn from this test ? JPG, PNG (and other compression format’s) reduces the file size, but for image manipulation we need for each pixel a lot of memory space:

File size is not equal to the required memory space.

Inside the most web server the common image manipulation library for PHP is called GD and this library doesn’t work different then programs like Photoshop, Paintshop or others. A very rough calculation for the memory need under GD library is :

Width x Height x 8 (bits) x 3(channels) / 8 x 1.65

NextGEN Gallery shows you the current memory usage of WordPress and all loaded plugins inside the server settings :

alexrabe

Now have a look at your Memory Limit in the server settings and you can calculate the maximum resolution for your images, which you can manipulate (create thumbnails) with the GD library. It’s possible to change the memory limit, but there exist various way’s and not all of them are allowed at your web server. At the best contact your web hoster, if you want to increase this limit. A good and useful value for images is a limit of 64 MB.

35 Responses to “Understand GD Library”


  1. Gravatar Icon 1 David Posted May 18th, 2008 - 13:33

    “After I answered this question now 1000 times , I hope this post will clarify this point.”

    And this is just one small point! You might want to think about making a deal with someone (moi?). You will explain how it all works to this person and he (or she) in return will explain in X number of posts and in incredible detail to the world how it ALL works.

  2. Gravatar Icon 2 alex.rabe Posted May 18th, 2008 - 13:49

    Ehh ? Didn’t get the point.. What deal ? Are you willing to help ?

  3. Gravatar Icon 3 Doc Posted May 20th, 2008 - 08:44

    Thank you for the good explanation. I had the same problem and I contacted my provider an they change the limit from 32 MB to 64 MB.

    Now everything works perfectly.

    Thanks a lot :-)))

  4. Gravatar Icon 4 alex.rabe Posted May 20th, 2008 - 11:23

    Thanks, good to know that is article is not useless….

  5. Gravatar Icon 5 Dave Posted May 21st, 2008 - 19:56

    The comment is not useless, in fact it’s pretty helpful.

    However, it’s the snarky tone of the first sentence (“I’ve told you children a thousand times already…”) that sets my teeth on edge.

    Look, dude, I searched the forums and read several pages of posts before deciding the answer wasn’t here or easily findable. That’s why I posted the question. I’m not an idiot. And I’m not an inconvenience, but that’s how it comes off.

    Last but not least: If a question has been asked “a thousand times”, wouldn’t you think you could save yourself lots of time and aggravation by putting it into an FAQ?

    Again, thank you for the explanation, if not the snark.

  6. Gravatar Icon 6 Steve Posted May 23rd, 2008 - 08:55

    (Error : Create Image failed. Check memory limit)

    My Memory Usage is 11.79MB and my PHP Memory Limit is 65MB. So what is the Problem?
    PHP Memory is not the same as GD Memory?

    Server- Einstellungen
    Betriebssystem : Linux
    Server : Apache
    Speicherverbrauch : 11.79 MByte
    MySQL Version : 4.1.22-max-log
    SQL Modus : Nicht gesetzt
    PHP Version : 4.4.8
    PHP Safe Mode : Aus
    PHP Allow URL fopen : An
    PHP Memory Limit : 65M
    PHP Max Upload Size : 200M
    PHP Max Post Size : 200M
    PHP Max Script Execute Time : 30s
    PHP Exif Modul : Ja ( V1.4 )
    PHP IPTC Modul : Ja
    PHP XML Modul : Ja

    GD Unterstützung
    GD Version : bundled (2.0.28 compatible)
    FreeType Support : Ja
    FreeType Linkage : with freetype
    T1Lib Support : Ja
    GIF Read Support : Ja
    GIF Create Support : Ja
    JPG Support : Ja
    PNG Support : Ja
    WBMP Support : Ja
    XBM Support : Ja
    JIS-mapped Japanese Font Support : Nein

  7. Gravatar Icon 7 alex.rabe Posted May 26th, 2008 - 09:52

    @Dave

    Yes, you are right. I’m a little bit irritably. With Google and a bit of reading in the forums this is already answered many times. At the end I wrote this article to help and not to blame somebody…

  8. Gravatar Icon 8 Henrik Posted May 27th, 2008 - 15:52

    Hey Alex

    Thank you for article, it cleared a lot up for me. I haven’t had the problem on my own site, but I have recently installed WordPress and NEXGen on my girlfriends homepage and she get’s the error message constantly.

    So I’m glad you made this post, even if you had been asked 1000 times. I didn’t see it as an irretable response.

    Have a great day (and thank you so much for your great efforts on NEXGen)

    Henrik

  9. Gravatar Icon 9 GJtje Posted May 28th, 2008 - 22:18

    @Steve

    Did you read this article? If you didn’t, read this article, it explains a lot about the error! :)

  10. Gravatar Icon 10 Printer Posted May 30th, 2008 - 09:53

    Hi Alex.. I have a question about this topic;
    I have two blogs on different domains. Hosted by the same host. My server settings are exactly the same on both sites.
    I have updated nextgen gallery on one of my sites recently, and now I get the “exceed Memory limit” error more frequently. Before it worked fine. i’m not uploading larger/resolution images than usually. Can it be that from the latest nextgen versions it takes more use of the memory than before?
    My other site where I still have nextgen version “V0.90 – 18.03.2008″ still works fine. I tested it with exactly the same images from my other site.
    Any idea how this can be?

  11. Gravatar Icon 11 Deadpan110 Posted June 11th, 2008 - 17:30

    Hi,

    Just a note to add to these comments… nextgen reports my site as having:
    PHP Memory Limit : 128M
    and I am struggling to get anything to work (my camera is 4 megapixel – sometimes resizing and thumbnails will work and other times it wont).

    It appears there must be a problem elsewhere… my error logs show Out of memory (allocated 32505856) (tried to allocate 9152 bytes).

    I have no control over my webhost but it seems like even though I can use ini_set, php5.ini and the newly built in wordpress config setting (yes, it just uses ini_set), the PHP memory limit must be capped at around 32M by my hosts admins and they will not change it.

    I am not pointing a finger of blame to anyone, but my galleries used to work… the camera is the same make/model/settings, the webhost is still the same (they don’t upgrade software – only complete servers)… the only thing that has changed is the WordPress version and nextgen version.

    It is a shame that people are having these problems as nextgen is a superb way to add photos and galleries to a blog, but you can’t please everyone all the time.

    For people who are running nextgen successfully – good on you, continue to support it… but for my blog, and my partners blog (she has 108 pictures in 14 galleries – luckily its not more)… it looks like we have got to convert to the new and boring WordPress default.

  12. Gravatar Icon 12 impulse Posted June 14th, 2008 - 11:26

    Thank you for the great plugin. I have the next problem though: each time I post a gallery with nextgen, my memory limit decreases. N10x1024 pixel images were no problem in the past, but now 3×640 pixels images will not work anymore.cannot generate thumbnails due to the memory error. using wordpress 2.5.1.

    * Operating System : Linux
    * Server : Apache (UNIX)
    * Memory usage : 13.38 MByte
    * MYSQL Version : 5.0.24alyceu
    * SQL Mode : Not set
    * PHP Version : 4.4.7
    * PHP Safe Mode : On
    * PHP Allow URL fopen : On
    * PHP Memory Limit : 16M
    * PHP Max Upload Size : 50M
    * PHP Max Post Size : 50M
    * PHP Max Script Execute Time : 30s
    * PHP Exif support : No
    * PHP IPTC support : Yes
    * PHP XML support : Yes

    GD support

    * GD Version : 2.0 or higher
    * FreeType Support : Yes
    * FreeType Linkage : with freetype
    * T1Lib Support : No
    * GIF Read Support : Yes
    * GIF Create Support : Yes
    * JPG Support : Yes
    * PNG Support : Yes
    * WBMP Support : Yes
    * XBM Support : No
    * JIS-mapped Japanese Font Support : No

  13. Gravatar Icon 13 impulse Posted June 14th, 2008 - 11:33

    Ok, if I make the thumbnails manually it works! So I got confused by the message telling me I had a memory problem.

  14. Gravatar Icon 14 impulse Posted June 14th, 2008 - 11:45

    I also found out that some images were 150dpi instead of 72. This is also causing the warning.

  15. Gravatar Icon 15 Johan Kraner Posted July 12th, 2008 - 02:21

    Would it be possible to make an option where the resize will be done with javascript on the client´s side when you pick an image to upload?

    In that case we wouldn´t have the problem with the memory limit…

  16. Gravatar Icon 16 S4L3M Posted August 1st, 2008 - 00:37

    Why bothering with partial transformation … when we can ask our provider for more MEM … This doesnt have to fit on a sattellite .. eh ? ;)

  17. Gravatar Icon 17 Bradley Posted September 25th, 2008 - 06:14

    Maybe this is of some help: NextGen Gallery: upload_max_filesize.

  18. Gravatar Icon 18 enki Posted December 24th, 2008 - 13:57

    Hey, great post. Very educating. Here is a helpful chart of the memory sizes of various common resolutions. The memory size is calculated as W * H * 3 / 1024^2 MB. I assume when you multiply by 1.65 in your equation above, this is for GD overhead? So your GD memory setting will need to be 1.65 the amount of memory listed below if I understand correctly.

    Megapixels – Resolution – Memory Usage
    5MP – 2592×1944 – 14.42MB
    5.9MP – 2816×2112 – 17.02MB
    7.1MP – 3072×2304 – 20.25MB
    8MP – 3264×2448 – 22.86MB
    9MP – 3456×2592 – 25.63MB
    10MP – 3648×2736 – 28.56MB
    11.1MP – 3840×2880 – 31.64MB
    12MP – 4000×3000 – 34.33MB
    14.6MP – 4416×3312 – 41.84MB
    21MP – 5616×3744 – 60.16MB
    24.4MP – 6048×4032 – 69.77MB

  19. Gravatar Icon 19 Ivangelion Posted July 14th, 2009 - 10:43

    I have the same problem, which I cannot create thumbnails due to memory limit issue.

    I’ve checked my system memory usage, and it is 173MB used and 850MB available. My current memory_limit in php.ini is 256M, and the wordpress 2.8.1 consumes 13.42MB. Neither totally unistalling nggallery then reinstalling it again, or even changing memory_limit to 512M solved the problem.

    Please tell me what I can do. Thank you very much.

  20. Gravatar Icon 20 Salman Posted December 17th, 2009 - 13:19

    Nicely explained. On the gallery I created for someone, I’ve added a “maximum megapixel limit” and set it to 5,000,000 (5 Mega-pixels). Upon image upload, the getimagesize function is used as follows:

    list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );

    If $source_image_width * $source_image_height > 5,000,000; if it is, the script rejects the upload.

  21. Gravatar Icon 21 Dale Reagan Posted January 26th, 2010 - 19:43

    Greetings,

    Very nice plugin!

    I also saw the memory errors and my system has plenty of resources.

    After a bit of review I find that:

    a) ImageMagick works just fine :)
    b) GD does not like ‘bad files’ – if you try to import a file that is NOT an image file (but has a common image name like ‘picture.jpg’) then this memory error will show up. My guess is that the GD libraries ‘assume’ that you would only provide a ‘real image’ file for processing – perhaps adding a ‘magic’ check prior to call GD routines would resolve this seemingly common issue?

    :)
    Dale

  22. Gravatar Icon 22 Sascha Posted July 12th, 2010 - 11:30

    Hi all,

    I just experienced the same Problem (WP 3.0 and latest NextGen). I just realised that my php safe mode was set to “off”.

    PHP-Unterstützung (PHP ‘safe_mode’ aktiviert )

    Just after activating the safe mode, everything worked fine !

    I had enough Memory and DiskSpace resources. However, had the same problem.

    THANX Alex for your great work !!!!

    Sascha

  23. Gravatar Icon 23 Newport Posted July 19th, 2010 - 22:36

    Another solution is to simply allow more memory to just the Nextgen Gallery plugin. From this post: http://www.visser.com.au/blog/thumbnail-generation-php-memory-limit-nextgen-gallery/

    Just go the the Nextgen Gallery plugin folder on your server, then navigate to the lib folder and open the file gd.thumbnail.inc.php. Go to approx. line #168 where it reads //@ini_set(‘memory_limit’, ’128M’); and remove the the // (the double slashes comment the line, ie, make it unused). You can then set an appropriate memory limit for your site.

    Works like a charm for me.

  24. Gravatar Icon 24 Brian Posted July 22nd, 2010 - 02:20

    Hi Alex,

    Just helping a client workout a kink with his Gallery.

    It’s funny my problem is similiar to some issues I’ve been reading about but none of the proposed solutions have helped so far.

    Tried 4 browsers, tried changing the memory limit code in the plugin.

    There are no errors or messages except “0 picture(s) successfully added” after the upload, and I do get the large file but it does not generate a thumbnail. The gallery manager will not show the images because there is no thumbnail, and the pages do not display them either, but the FTP backend shows the large file does upload. (small ‘large’ files btw – 127kb for example)

    I’ve already re-upgraded wordpress and re-upgraded the plugin.

    Well if you read and have any ides I would very much appreciate it.

    Thanks for the great work though – I admire donators to the software world!

  25. Gravatar Icon 25 Paul Clerkin Posted August 5th, 2010 - 19:27

    Thanks Newport – that worked great.

  26. Gravatar Icon 26 Webbdesign i Göteborg Posted February 22nd, 2011 - 23:17

    Thanks. Nice :)

  27. Gravatar Icon 27 Stu Posted October 26th, 2011 - 11:53

    Hi

    I turned of Backup images just above the rezise tick box and it now works fine :) Hope this helps

Who's linking?

  1. 1 Problém s NextGen Gallery a obrázky s velkým rozlišením | [REALLY HARD] - Ji?í Van?k Pingback on Jun 29th, 2008
    "[...] informací pak zjistíte také zde: http://alexrabe.de/2008/05/13/understand-gd-library/ (Zatím nehodnoceno)  Na?ítám [...] "
  2. 2 Ivan Teoh » Blog Archive » NextGEN Gallery Maximum 300 Big Images Pingback on Sep 3rd, 2008
    "[...] Understand GD Library [...] "
  3. 3 …Gurgi.Girl » Blog Archive » NextGEN issues.. Pingback on Nov 7th, 2008
    "[...] For ages it was working fine… but since the Wordpress upgrade.. and NextGEN plugin upgrade.. I’ve started experiencing issues.. …"
  4. 4 Gd-library Fatal error: Out of memory … memory_limit | openrelay Pingback on Feb 1st, 2009
    "[...] Alex Rabe [...] "
  5. 5 benju.blog » Blog Archive » NextGEN Gallery Fehlermeldung: Exceed Memory limit Pingback on Mar 23rd, 2009
    "[...] links: http://alexrabe.de/2008/05/13/understand-gd-library/ http://wordpress.org/support/topic/232390?replies=5 posted under [...] "
  6. 6 NextGen Gallery a problém s Exceed Memory Limit | Seby.cz Pingback on Jul 7th, 2009
    "[...] se podíváte na p?ísp?vek samotného Alexe Raba (tv?rce NGG) – Understand GD Library, zjistíte, že hláška o nedostatku pam?ti …"
  7. 7 Unterschied Dateigroesse und Arbeitsspeichergroesse « Einmal ist Keinmal! Pingback on Jan 13th, 2010
    "[...] zufällig auf einen interessanten Blogeintrag bei alexrabe gestoßen, der kurz aber sehr eingängig erklärt, warum ein Bild mehr Platz …"
  8. 8 WordPress 3.0 Memory Requirements: 256 MB at alex.rabe Pingback on Jul 5th, 2010
    "[...] If you can use the plugin but you are not longer able to create thumbnails, please remind my blog …"
Comments are currently closed.