GD库会占用大量内存吗? [英] Does the GD library use a lot of memory?

查看:131
本文介绍了GD库会占用大量内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的PHP脚本中使用GD库,以从外部服务器生成随机大图片的小缩略图.每次调用该页面时,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?

推荐答案

GD使用大量内存.它将图像完全加载到内存中并解压缩,因此每个像素至少需要32位.然后将尺寸为800 x 600的图像用完:

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位= 15.4兆位= 2 MB

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.

由于GD使用了大量内存,因此可能值得使用IMagick扩展名生成缩略图.关于内存消耗,ImageMagick确实可以更好地扩展,而且速度也非常快(但是由于上述原因,您仍应缓存图像).

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).

这篇关于GD库会占用大量内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆