使用 CSS / HTML 调整图像大小更好,还是保存图像的多个版本更好?
到目前为止,当用户上传图像时,我一直保存它的几个不同版本以供在整个网站中使用。随着网站的发展,所需的尺寸数量也在增加。
目前,每个上传的图像大小约为 6 个新图像并保存在服务器上。
缺点是每次我需要创建新尺寸(例如,现在我正在为图像库制作新尺寸),我必须循环浏览所有数千张图像并重新剪切新尺寸对于每个。
然而,当我开始时,这是一种避免动态调整图像大小的快速方法,但现在它开始变成一场噩梦。
是继续保存不同的尺寸,只处理开销更好,还是此时获取 3 个通用尺寸,并根据需要动态调整它们的尺寸更好?
Up until now, when a user has uploaded an image, I have been saving several different versions of it for use throughout my site. As the site has grown, so have the numbers of sizes needed.
At the moment each uploaded image is sized in to about 6 new images and saved on the server.
The downside is that every time I need to create a new size (right now, for instance, I'm making a new size for an image gallery), I have to cycle through all the thousands of images and re-cut a new size for each.
Whereas, when I started, it was a nice quick way to avoid resizing images on the fly, now it's starting to turn into a nightmare.
Is it better to continue saving different sizes, and just deal with the overhead, or is it better at this point to get maybe 3 general sizes, and resize them on the fly as needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 html/css “调整大小”图像(例如,指定高度和宽度)通常不是您想要做的 - 它会导致缩放后的图像带有调整大小的伪影,并且效率低下,因为用户可能会下载更大的图像文件比他们实际需要的多。
相反,拥有某种服务器端解决方案来允许动态调整大小可能正是您想要的。我建议使用 ImageMagick - 结合您最喜欢的语言和一些网络的实现 -服务器 voodoo (例如,使用 .htaccess for Apache),您可以轻松地让 /path/to/yourimage.png?50x50 触发对调整大小脚本的调用,该脚本调整图像大小,将其保存在缓存文件夹中,并输出调整大小的文件到浏览器。这一切都更好 - 您可以正确调整大小,您的用户只下载他们需要的确切文件,并且最终结果被缓存,因此您的调整大小操作仅发生一次。查看Image::Magick: :Thumbnail 为例(在 perl 中)
编辑 - 如果您使用您正在使用的服务器端语言/框架对此做出响应,我很乐意为您指出 ImageMagick 的缩略图/调整大小实现的方向或适合您平台的其他内容。
"Resizing" images using html/css (e.g., specifying height & width) is generally not what you want to do - it results in poorly scaled images with artifacts from the resize, and is inefficient as the user is potentially downloading a much larger file than they actually need.
Rather, having some kind of server-side solution to allow for on-the-fly resizing is probably what you want. I'd recommend using ImageMagick - combined with the implementation for your favorite language and some web-server voodoo (e.g., using .htaccess for Apache), you can easily have /path/to/yourimage.png?50x50 fire a call to a resize script that resizes the image, saves it in a cache folder, and outputs the resized file to the browser. This is better all around - you get proper resizing, your user only downloads the exact file they need, and the end-result is cached so your resize action only occurs once. Check out Image::Magick::Thumbnail for an example (in perl)
Edit - if you respond to this with what server-side language/framework you are using, I would be happy to point you in the direction of a thumbnail/resizing implementation of ImageMagick or something else for your platform.
多个版本。
有些浏览器根本无法很好地缩放这些东西,最终会导致图像不稳定、像素化不良等。
例外情况可能是,如果您知道所有图像都是摄影图像。然后为您提供更大尺寸的版本,但缩小尺寸也可以。但如果这些有插图或文字,效果就会很明显。
Multiple versions.
Some browsers simply don't scale these things well and you end up with choppy nasty in the image, bad pixelation, etc...
The exception could be if you know all the images are photographic. Then have versions for your larger sizes, but shrinking could be ok. But if these have illustration or text, the effect will be noticeable.