简体   繁体   English

我想使用php中的GD库创建多个缩略图,这是即时创建还是创建物理缩略图更好?

[英]I want to create multiple thumbnails using GD library in php, which is better creating on the fly or creating physical one?

I want to create multiple thumbnails using GD library in php, and I already have a script to do this, the question is what is better for me .. is it better to create thumbnail on the fly? 我想使用php中的GD库创建多个缩略图,并且我已经有一个脚本可以执行此操作,问题是什么对我来说更好..即时创建缩略图会更好吗? or create a physical file on my server each time I want a thumb?? 还是每次我想在服务器上创建物理文件时? and Why? 为什么?

Please, consider time consuming and storage capacity and other disadvantages for both 请考虑两者的耗时和存储容量以及其他缺点

When you create the thumbnail depends on a couple of factors (that I'll get into) but you should never discard the output of something like this (unless you'll never use it again) as it's a really expensive operation. 创建缩略图时,取决于两个因素(我会介绍),但是您绝对不应丢弃类似这样的输出(除非您永远不会再使用它),因为这是一项非常昂贵的操作。

Anyway your two main choices for "when to generate the thumbnail" are: 无论如何,“何时生成缩略图”的两个主要选择是:

  1. When it's first requested. 首次请求时。 This is common and it means that you don't generate thumbnails that are never used but it does mean if you have a page full of first-time-thumbnails that the server might become overwhelmed with PHP processes generating the thumbnails. 这很常见,这意味着您不会生成从未使用过的缩略图,但这确实意味着如果您的页面中充满了第一次创建的缩略图,则服务器可能会被生成缩略图的PHP进程所淹没。

    I had a similar issue with Sorl+Django where I was generating 100+ thumbnails per request for the first few requests after uploading and it basically made the entire server hang for 20 minutes. 我在Sorl + Django中遇到了类似的问题,在上传后,我为每个请求的前几个请求生成了100个以上的缩略图,这基本上使整个服务器挂了20分钟。 Not good. 不好。

  2. Generate all required thumbnails when you upload. 上传时生成所有必需的缩略图。 Because it takes a long time to upload, you break down the processing quite a lot. 由于上载需要很长时间,因此您需要大量处理。 You can also pull it out-of-process (ie use another script to process uploads - perhaps not even in PHP). 您也可以将其拉出进程(即,使用另一个脚本来处理上载-甚至在PHP中也不可能)。

    The obvious downside is you're using up disk space that you otherwise might not need to use up... But unless you're talking about hundreds of thousands of thumbnails, a small percentage of unused ones probably won't break the bank. 明显的缺点是您正在用尽磁盘空间,否则您可能不需要用完...但是除非您谈论数十万个缩略图,否则一小部分未使用的缩略图可能不会破坏资金。

    Of course, if disk space is an issue, there might be an argument for pushing the thumbnail up to a CDN at the same time as you process it. 当然,如果磁盘空间有问题,则可能存在一个论点,即在处理缩略图的同时将缩略图推送到CDN。

One note when you save the thumbnails, it's fairly common that you'll want to resize the thumbnails at some point down the line or perhaps want two small variants. 在保存缩略图时要注意的一点是,很常见的一点是,您需要在行的某个位置调整缩略图的大小,或者可能想要两个小的变体。 I find it really useful to make the filenames very specific so if the original image is image.jpg , the 200x200 version is image-200x200.jpg . 我发现使文件名非常具体真的很有用,因此,如果原始图像为image.jpg ,则200x200版本为image-200x200.jpg

GD is really resource heavy, so you should look at if you can use ImageMagick instead (which also has a clearer syntax). GD确实占用大量资源,因此您应该查看是否可以使用ImageMagick(它也具有更清晰的语法)。

You definitely will be better off caching the created thumbnail after the first run (regardless of if you run GD or ImageMagick) and serve them from the cache. 第一次运行后(无论运行GD还是ImageMagick都可以)缓存创建的缩略图并从缓存中提供它们,绝对会更好。 If you are worried about storage, clear out old files from the cache now and then. 如果您担心存储,请不时从缓存中清除旧文件。

Neither/both - don't generate the thumbnails till you need them - but keep the files you generate. 两者都不是-都不生成缩略图,直到需要它们时-保留生成的文件。

That way you'll minimise the amount of work needed and have a self-repairing system 这样一来,您将需要的工作量降至最低,并拥有一个自我修复系统

C. C。

Always cache (= write out to disk) the results of GD operations. 始终缓存(=写出到磁盘)GD操作的结果。 They are too expensive both regarding processor time and memory to be done on the fly every time. 就处理器时间和内存而言,它们太昂贵了,以至于每次都无法即时完成。 This becomes increasingly true the more visitors/hits you have. 您拥有的访问者/点击次数越多,这种情况就越来越真实。

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

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