简体   繁体   English

PHP / JS - 动态创建缩略图或存储为文件

[英]PHP/JS - Create thumbnails on the fly or store as files

For an image hosting web application: 对于托管Web应用程序的图像:

For my stored images, is it feasible to create thumbnails on the fly using PHP (or whatever), or should I save 1 or more different sized thumbnails to disk and just load those? 对于我存储的图像,使用PHP(或其他)动态创建缩略图是否可行,或者我应该将1个或更多不同大小的缩略图保存到磁盘并加载那些?

Any help is appreciated. 任何帮助表示赞赏。

Save thumbnails to disk. 将缩略图保存到磁盘。 Image processing takes a lot of resources and, depending on the size of the image, might exceed the default allowed memory limit for php. 图像处理需要大量资源,并且根据图像的大小,可能会超出php的默认允许内存限制。 It is less of a concern if you have your own server with only your application running but it still takes a lot of cpu power and memory to resize images. 如果你有自己的服务器只运行你的应用程序,但它仍然需要大量的CPU功率和内存来调整图像大小,这不是一个问题。 If you're considering creating thumbnails on the fly anyway, you don't have to change much - upon the first request, create the thumbnail from the source file, save it to disk and upon subsequent requests just read it off the disk. 如果您正在考虑动态创建缩略图,则无需进行太多更改 - 在第一次请求时,从源文件创建缩略图,将其保存到磁盘,并在后续请求时将其从磁盘读取。

I use phpThumb , as it's the best of both worlds. 我使用phpThumb ,因为它是两全其美的。 You can create thumbnails on the fly, but it automatically caches the images to speed up future requests. 您可以动态创建缩略图,但它会自动缓存图像以加快将来的请求。 It creates a nice wrapper around the GD and ImageMagick libraries. 它为GD和ImageMagick库创建了一个很好的包装器。 Worth a look! 值得一看!

It would be much better to cache the thumbnails. 缓存缩略图会好得多。 Generating them on the fly would be very taxing on the system. 即时生成它们对系统非常重要。

It depends on the usage pattern of the site, but, basically, how many times do you expect each image to be viewed? 这取决于网站的使用模式,但基本上,您希望每个图像被查看多少次?

In the case of thumbnails, they're most likely to be around for quite a while (the image is uploaded once and never changed, so the thumbnail doesn't change either), so it's generally worthwhile to generate when the full image is uploaded and store them for later. 在缩略图的情况下,它们最有可能存在很长一段时间(图像上传一次但从未改变,因此缩略图也不会改变),因此通常值得在上传完整图像时生成并存储它们以供日后使用。 Unless the site is completely dead, they'll be viewed many (hundreds or thousands of) times over their lifetime and disk is a lot cheaper than latency these days. 除非网站完全死亡,否则它们将在其生命周期内被查看很多次(数百或数千次),并且磁盘比延迟更便宜。 This also becomes more significant as load on the server increases, of course. 当然,当服务器上的负载增加时,这也变得更加重要。

Conversely, for something like stock charts that get updated every hour (if not more frequently), that would be a situation where you'd do better to create them on the fly, so as to avoid wasting CPU time on constantly generating images which no user will ever see. 相反,对于像股票图表那样每小时更新一次(如果不是更频繁)的情况,这将是你在飞行中更好地创建它们的情况,以避免浪费CPU时间不断生成图像,用户会看到。

Or, if you want to get fancy, you can optimize to handle either access pattern by generating the images on the fly the first time they're needed and then showing the pre-generated one afterwards, up until the data it's generated from changes, at which point you delete it so that it will be regenerated the next time it's needed. 或者,如果您想获得想象力,您可以优化处理访问模式,方法是在第一次需要时动态生成图像,然后在之后显示预先生成的图像,直到从更改生成的数据为止,此时您将其删除,以便下次需要时重新生成它。 But that would be overkill for something as static as thumbnails, IMO. 但对于像缩略图一样静态的东西,IMO来说,这将是一种过度杀伤力。

看看gd库imagemagick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM