当需要多种尺寸时,图像应该如何存储?
存储图像的最佳方式是什么?目前,当上传图像时,我将其大小调整为 3 种不同的尺寸(缩略图、正常尺寸和大尺寸)。我将图像的描述、格式保存在数据库中,并使用数据库中的 ID 号作为图像名称。每个尺寸的图像都有自己的目录。
我应该将图像存储在数据库中吗?我应该只存储较大的尺寸并根据需要生成缩略图吗?或者你还有其他的想法吗?
What is the best way to store images? Currently when an image is uploaded I resize it to 3 different sizes (a thumbnail, a normal size, and a large size). I save in a database a description of the image, the format, and use the id number from the database as the image name. Each size image has its own directory.
Should I be storing the images in the database? Should I only be storing the larger size and generate the thumbnail as needed? Or any other ideas you have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所描述的对我来说听起来很合理。您可以将图像存储在数据库中,但您不会注意到任何重大的性能提升,并且如果您的网络主机限制 MySQL 数据库的大小,您可能会发现您为数据库空间支付了过高的费用。
至于调整图像大小,执行一次并存储三个副本比每次请求生成一次较小的图像的计算成本要低得多;但是,您可能需要考虑让浏览器为您生成缩略图(只需指定一个 img 标记,并将 src 设置为全尺寸图像加上宽度/高度属性),这将允许您节省 CPU 功率和磁盘空间拥有不太漂亮的缩略图的成本很高(浏览器通常会以牺牲质量为代价来缩放图像以提高速度)。
What you described sounds reasonable to me. You could store the images in your database but you wouldn't notice any major performance boosts and, if your webhost limits the size of your MySQL database, you might find you're overpaying for database space.
As for resizing the images, doing it once and storing the three copies is way less computationally expensive than generating smaller images once per request; however, you may want to consider having the browser do your thumbnail generation for you (just specify an img tag with the src set to the full size image plus width/height attributes), which would allow you to save CPU power and disk space at the expensive of having not-so-pretty thumbnails (browsers generally scale images for speed at the expensive of quality).
您可以选择两者的混合,将大图像保存在 SQL(或 HDD)上。
您可以根据请求即时生成缩略图,然后将它们缓存在服务器上一段时间。
这意味着您可以随时在磁盘上保存大图像以及最近/常用图像的缩略图。
You can choose a mix of both worlds, saving the big image on the SQL (or HDD).
You generate the thumbnails on the fly when requested, but then cache them on the server for a certain ammount of time.
This means at any time you will have on the disk the big images and the thumbnails for the most recent / frequently used ones.
如果您有一个由 drupal 支持的站点,则此功能是通过 http://drupal.org/project/imagecache< 内置的/a>
If you had a drupal powered site, this functionality is built in with http://drupal.org/project/imagecache