作者:Libra | 来源:互联网 | 2023-05-18 22:41
InmywebappimusingMongoDBGridFStostoreuseruploadedimages.在我的网络应用程序中,我使用MongoDBGridFS来存
In my web app i'm using MongoDB GridFS to store user uploaded images.
在我的网络应用程序中,我使用MongoDB GridFS来存储用户上传的图像。
The rest of the site is managed by MySQL.
该网站的其余部分由MySQL管理。
In a photo table (in mysql) I have three fields for storing the MongoId of the file object.
在照片表(在mysql中)我有三个字段用于存储文件对象的MongoId。
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.
我的问题是,有时我需要使用小于图像的“小”版本的缩略图(即在小部件框中,消息头像等)或者我需要使用稍微小一点的中等图像版本。
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
所以我的问题是,最好只在GridFS系统中存储一个图像,然后在照片表中有一个存储MongoID的字段,然后创建一个动态调整图像大小的脚本(即http:// localhost / image /取/ {mongoid}?调整= 50
Otherwise, in my db i'll need thumbnail_50, thumbnail_100, medium_300, medium_400 etc. etc.
否则,在我的数据库中我将需要thumbnail_50,thumbnail_100,medium_300,medium_400等。
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?
沿着动态调整大小路线会有什么影响?这是一个坏或好主意?你会怎么做?
Thanks.
1 个解决方案
4
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.
动态调整大小会导致服务器上的CPU负载大大超过调整大小一次,并存储结果。另一方面,提前调整大小可能会导致更大的数据集存储在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.
您可以考虑使用混合方法,在其中您可以动态调整大小但将结果保存回GridFS,这样您就不需要将任何一个图像大小调整为给定大小多次。
You should also know that HTML and CSS allow various options for controlling the displayed size of an image. For example:
您还应该知道HTML和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.
将导致图像按比例缩放到50像素宽(在大多数现代浏览器中,无论如何)。您还可以使用宽度和最大宽度CSS属性来控制图像大小。
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.
对于我自己,并且对要存储的图像的音量或文件大小一无所知,我可能会在添加图像时调整大小 - 换句话说,将页面速度和CPU负载调整一次 - 然后服务GridFS的各种尺寸,如果在一个特定情况下需要稍微不同的尺寸,则使用max-width来控制屏幕尺寸。