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

发布于 2024-07-05 21:20:56 字数 118 浏览 5 评论 0原文

对于图像托管 Web 应用程序:

对于我存储的图像,是否可以使用 PHP(或其他方式)动态创建缩略图,或者我应该将 1 个或多个不同大小的缩略图保存到磁盘并加载它们?

任何帮助表示赞赏。

For an image hosting web application:

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?

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自控 2024-07-12 21:20:56

查看 gd 库imagemagick

check out the gd library and imagemagick

野味少女 2024-07-12 21:20:56

将缩略图保存到磁盘。 图像处理需要大量资源,并且根据图像的大小,可能会超出 php 默认允许的内存限制。 如果您拥有自己的服务器,仅运行您的应用程序,但调整图像大小仍需要大量 CPU 功率和内存,则不必担心。 如果您正在考虑即时创建缩略图,则不必进行太多更改 - 在第一个请求时,从源文件创建缩略图,将其保存到磁盘,并在后续请求时从磁盘中读取它。

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

讽刺将军 2024-07-12 21:20:56

我使用 phpThumb,因为它是两全其美的。 您可以即时创建缩略图,但它会自动缓存图像以加快将来的请求速度。 它围绕 GD 和 ImageMagick 库创建了一个很好的包装器。 值得一看!

I use phpThumb, as it's the best of both worlds. 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. Worth a look!

尛丟丟 2024-07-12 21:20:56

缓存缩略图会好得多。 动态生成它们会对系统造成很大的负担。

It would be much better to cache the thumbnails. Generating them on the fly would be very taxing on the system.

不知在何时 2024-07-12 21:20:56

这取决于网站的使用模式,但基本上,您预计每个图像被查看多少次?

就缩略图而言,它们很可能会存在相当长一段时间(图像上传一次并且从未更改,因此缩略图也不会更改),因此通常值得在上传完整图像时生成并将它们存储起来供以后使用。 除非该网站完全失效,否则它们在其生命周期内将被浏览多次(数百或数千次),而且如今磁盘比延迟便宜得多。 当然,随着服务器负载的增加,这一点也变得更加重要。

相反,对于像股票图表这样每小时更新一次(如果不是更频繁的话)的情况,您最好动态创建它们,以避免浪费 CPU 时间来不断生成无法生成的图像。用户将永远看到。

或者,如果您想变得更奇特,您可以优化处理任一访问模式,方法是在第一次需要时动态生成图像,然后显示预先生成的图像,直到根据更改生成数据为止,此时您将其删除,以便下次需要时重新生成。 但在我看来,对于像缩略图这样静态的东西来说,这太过分了。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文