MongoDB GridFS存储多种大小的图像或即时调整大小 [英] MongoDB GridFS store multiple sizes of image or use on the fly resizing

查看:109
本文介绍了MongoDB GridFS存储多种大小的图像或即时调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我正在使用MongoDB GridFS来存储用户上传的图像.

In my web app i'm using MongoDB GridFS to store user uploaded images.

该网站的其余部分由MySQL管理.

The rest of the site is managed by MySQL.

在一张照片表(在mysql中)中,我有三个字段用于存储文件对象的MongoId.

In a photo table (in mysql) I have three fields for storing the MongoId of the file object.

因此,我存储了图像的三个版本.大中小.

So I store three versions of the image. Small, medium and large.

我的问题是,有时我需要使用比图片的小"版本小的缩略图(例如,在小部件框中,消息头像等),或者我需要使用略小的版本的缩略图.中等图片.

My question is, sometimes i'll need to use a thumbnail smaller than the 'small' version of the image (i.e. in a widget box, message avatars etc.) or i'll need to use a slighly smaller version of the medium image.

所以我的问题是,最好只将一张图像存储在GridFS系统中,然后在照片表中保留一个字段来存储MongoID,然后创建一个脚本来动态调整图像大小(即

So my question, would it better to just store one image in the GridFS system and then have one field in the photo table storing the MongoID and then create a script that resizes the image on the fly (i.e. http://localhost/image/fetch/{mongoid}?resize=50

否则,在我的数据库中,我将需要thumbnail_50,thumbnail_100,medium_300,medium_400等.

Otherwise, in my db i'll need thumbnail_50, thumbnail_100, medium_300, medium_400 etc. etc.

采用即时调整大小路线将意味着什么?这是一个坏主意还是一个好主意?你会怎么做?

What would be the implications of going down the on-the-fly resize route? is it a bad or a good idea? What would you do?

谢谢.

推荐答案

动态调整大小将导致服务器上的CPU负载比一次调整为各种大小并存储结果要多得多.另一方面,提前调整大小可能会导致将更大的数据集存储在GridFS中.换句话说,这是一个权衡.

Resizing on the fly will result in far more CPU load on your server than resizing to a variety of sizes once, and storing the results. On the other hand, resizing ahead of time may result in a larger set of data to be stored in GridFS. In other words, it's a tradeoff.

您可以考虑使用一种混合方法,您可以即时调整大小,然后将结果保存回GridFS,这样就无需将任何一个图像调整为给定大小的大小超过一次.

You could consider a hybrid approach, where you resize on the fly but save the results back to GridFS so that you don't need to resize any one image to a given size more than once.

您还应该知道HTML和CSS允许使用各种选项来控制图像的显示大小.例如:

You should also know that HTML and CSS allow various options for controlling the displayed size of an image. For example:

<img src="/path/to/image.jpg" width="50"/>

将导致图像按比例缩放到50像素宽(在大多数现代浏览器中,无论如何).您还可以使用widthmax-width CSS属性来控制图像大小.

Will result in an image scaled proportionally to 50 pixels wide (in most modern browsers, at any rate). You can also use the width and max-width CSS properties to control image size.

对我自己来说,不知道要存储的图像的大小或文件大小,添加图像时我可能会调整大小-换句话说,只需一次点击页面速度和CPU负载-然后在特定情况下需要稍微不同的大小时,使用max-width控制屏幕大小,然后使用GridFS提供各种大小.

For myself, and knowing nothing about the volume or file size of the images you'll be storing, I'd probably resize when images are added -- in other words, take the page speed and CPU load hit once -- and then serve the various sizes out of GridFS, using max-width to control the on-screen size if a slightly different size is required in one particular case.

这篇关于MongoDB GridFS存储多种大小的图像或即时调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆