动态调整图像大小,或在磁盘上存储不同大小的图像?
我正在构建一个最终将包含大量图像的 Web 应用程序。这些图像需要在整个网站上以不同的格式显示。这两种解决方案的优缺点是什么:
- 上传时存储图片的各种版本(例如 Thumb、小、中、大、非常大)
- 通过 URL 调整图像大小 - 例如 /Content/Image/1?height =300
你觉得怎么样?
编辑: 我很难接受一个答案而不是另一个答案,所以对于任何阅读此问答的人,请花点时间阅读这两个答案,因为接受的答案是通过掷硬币选择的:)它们都同样好。
I'm building a Web application that will eventually contain a lot of images. These images will need to be displayed in different formats across the site. What would be the pros/cons of the two solutions:
- Storing various versions of the picture when they are uploaded (e.g. Thumb, small, medium, large, verylarge)
- Resizing the image through the URL - e.g. /Content/Image/1?height=300
What do you think?
Edit:
I had a really hard time accepting one answer over the other, so for anyone reading this q/a, take your time to read both answers because the accepted answer was selected by the flip of a coin:) They're both equally good.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我查看维基媒体,他们会采取某种组合。
您可以通过参数定义大小,并在第一步中检查此类文件是否存在,如果不存在,则动态创建该文件并将其保存以供下一个请求使用。
更新
阅读您的评论后
。这个问题几乎每次都会出现:我是在速度上优化还是在大小上优化?这是您必须根据自己和具体问题做出的决定。
也许您可以实施一些额外的检查,例如,如果请求特定大小超过 x 次,则将其存储在磁盘上,或者删除在 y 时间跨度内未收到请求的所有存储图像。
If I take a look on Wikimedia, they take some kind of combination.
You can define the size through a parameter and in a first step they look if such a file exists and if not create it on the fly and save it for the next request.
Update
After reading your comment.
That question comes up almost every time: Do I optimize on speed or on size? That's a decision you have to make for yourself and your concrete problem.
Maybe you can implement some additional checks, like store it on disk if a specific size is requested more than x times or delete all stored images that didn't get a request for a timespan of y.
不可避免地,任何基于参数提供动态大小的东西都将允许最终用户在服务器上引起合理的(可能“比平常更多”)的 CPU 活动量,这对您来说可能是一个问题,具体取决于您正在尝试的内容达到。正如奥利弗所建议的,缓存生成的图像将有助于避免重复执行相同的工作,并且绝对应该成为任何动态解决方案的一部分。
我认为您需要考虑动态调整大小的重要性。在每种情况下,我处理过的预定义尺寸(你的拇指,小,中,大,非常大)都工作得很好。同样的缓存注意事项也适用,但创建大量图像的可能性要小得多。我倾向于在上传图像时创建各种图像尺寸,但是如果尚未创建则按需创建解决方案同样可以很好地工作。
Inevitably anything which provides dynamic sizes based upon a parameter is going to allow an end user to cause a reasonable ('more than usual' perhaps) amount of CPU activity on the server, which might be a problem for you depending on what you are trying to achieve. Caching of generated images, as suggested by Oliver, will help avoid doing the same work twice and should definitely be a part of any dynamic solution.
I think you need to consider how important the dynamic sizing is. In every case I've dealt with predefined sizes (your thumb, small, medium, large, verylarge) have worked fine. The same caching considerations apply, but there far much less potential for a large number of images being created. I've tended to create the various image sizes when the image is uploaded, but a create-on-demand-if-not-already-created solution would work equally well.