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开发者_StackOverflow it.
Will this slow down the server or use up an unusual amount of memory?
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).
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?
Depends on what you are doing with it, but why not try for yourself:
- memory_get_usage() — Returns the amount of memory allocated to PHP
- memory_get_peak_usage() — Returns the peak of memory allocated by PHP
Any kind of image processing is likely to be memory intensive.
If you can cache these images so as not to regenerate them with every hit to the page, that'd be a great move.
A PHP library that deals with a lot of this caching for you is phpThumb - it's probably ideal for tasks like these.
http://phpthumb.sourceforge.net/
If you generate a new thumbnail every page load, it will take extra processing time. Depending on the amount of images you are thumbnailing as well as their original sizes, you may or may not notice slowness. If there is anyway you can create the thumbnail on the first page load and save it, and load the pre-created version for other page loads, you will be better off.
Yes, this is a terrible idea and will quickly cause your server to buckle as traffic increases.
Image processing uses huge amounts of server resources, both CPU and memory. For example, to resize a ~600kB JPG file when I tested it recently, it took GD ~9MB of memory at peak usage. You can measure this using memory_get_peak_usage(true)
. If you dynamically generate the thumbnails, these resources will be used every time a page is requested. Furthermore, because resizing is a slow operation, much slower than serving a file, this memory will be in use for a significant amount of time, much longer than it typically takes a PHP script to execute. A sufficient number of requests at once will thus quickly cause your server to run out of memory and go into swap, something you never want to have happen, and that usually results in visible downtime to most people viewing your site.
Thumbnails are small. For instance, I generated a (still quite large, 640x480) thumb from this file and it took up only 64kB. Smaller thumbnails are even smaller. HD space is cheap and you're only storing the small output file, not the peak usage. If the original file is on your server, the space required by storing multiple thumbnails is going to be negligible.
Generating once and storing it is the only sane option here. With that approach, you only incur the high CPU and memory usage once, and you get the full benefits of serving a small thumbnail file. Furthermore, if you're clever you can choose when to incur the heavy image processing costs, such as by putting unprocessed images in a queue and then waiting for a moment when the server is under relatively light load, and generating the thumbnail then. Sometimes a delay of only a few seconds still leads to good user experience, but avoids the "heavy" call if the server is experiencing a traffic spike at the moment.
精彩评论