Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the GD library use a lot of memory?

Tags:

php

gd

I want to use GD library in my PHP script to generate a small thumbnail of a random big picture from external server. Every time the page is called, the GD library will regenerate a thumbnail and show it.

Will this slow down the server or use up an unusual amount of memory?

like image 978
CodeOverload Avatar asked Jan 13 '10 21:01

CodeOverload


2 Answers

GD use a lot of memory. It loads the image into memory entirely and uncompresses it, so you will need at least 32 bits per pixel. An image with the size 800 x 600 do then use up:

800 * 600 * 32 bits = 15.4 megabit = 2 MB

This is only to load the image. I have heard that it will use the double of this if you do resizing, and if your images are even bigger it will be even more memory.

You should really consider caching your thumbnails so they only have to be generated once (this will speed up the page for your visitors too!).

I also read now that you are loading the images from an external server, in which case you REALLY must cache the image because otherwise your visitors have to wait for YOU to download the entire image first. This gets even worse if the external server is down or overloaded and your visitors will have to wait for a timeout (this will look like it's your service that is slow). In addition to this you will waste a lot of bandwidth if you download the image every time a user requests a thumbnail of it.


Since GD uses so much memory, it may be worthwhile to instead generate the thumbnails using the IMagick extension. ImageMagick does scale much better regarding memory consumption and is also very fast (but you should still cache the images, for the reasons stated above).

like image 86
Emil Vikström Avatar answered Oct 08 '22 12:10

Emil Vikström


sure it will slow down the server it also depends on the size of the image you are using. why don't you just save the thumbnail-image?

like image 21
antpaw Avatar answered Oct 08 '22 12:10

antpaw