Understand GD Library

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.

Tagged ,

35 thoughts on “Understand GD Library

  1. David says:

    “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. alex.rabe says:

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

  3. Doc says:

    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. alex.rabe says:

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

  5. Dave says:

    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. Steve says:

    (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. alex.rabe says:

    @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. Henrik says:

    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. GJtje says:

    @Steve

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

  10. Printer says:

    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. Deadpan110 says:

    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. impulse says:

    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. impulse says:

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

  14. impulse says:

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

  15. Johan Kraner says:

    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. S4L3M says:

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

  17. [...] For ages it was working fine… but since the WordPress upgrade.. and NextGEN plugin upgrade.. I’ve started experiencing issues.. it seems that thumbnails cannot be created as WordPress and NextGEN use up too much of my server’s memory.. [...]

  18. enki says:

    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. [...] 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 by se m?la zobrazovat až ve chvíli, kdy se do [...]

  20. Ivangelion says:

    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.

  21. Salman says:

    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.

  22. [...] zufällig auf einen interessanten Blogeintrag bei alexrabe gestoßen, der kurz aber sehr eingängig erklärt, warum ein Bild mehr Platz im Arbeitsspeicher bei [...]

  23. Dale Reagan says:

    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

  24. [...] If you can use the plugin but you are not longer able to create thumbnails, please remind my blog post two years ago, it’s the same issue… Filed under Common.  |0 Comments . var [...]

  25. Sascha says:

    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

  26. Newport says:

    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.

  27. Brian says:

    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!

  28. Paul Clerkin says:

    Thanks Newport – that worked great.

  29. Stu says:

    Hi

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

Comments are closed.