About mongoose-thumbnail
mongoose plugin that adds a thumbnail field to a mongoose schema. This is especially suited to handle image file uploads with nodejs/expressjs.
Install
npm install mongoose-thumbnail
Usage
The plugin adds a thumbnail field to the mongoose schema.
As it uses mongoose-file internally, the added field has all the mongoose-file
field sub-properties.
Please refer to mongoose-file documentation to understand basic usage.
In addition to the sub-fields carried by mongoose-file, this plugin creates an additional sub-field, named by default thumb
.
Depending on the inline
plugin option, the thumbnail field may contain additional sub-fields containing the thumbnail file properties, or could be a string containing the Data URI encoded thumbnail.
In addition those pertaining to mongoose-file, the following options are available:
thumb
- the name of the thumbnail sub-field (defaults tothumb
)format
- the image format for the thumbnail (defaults tojpg
)size
- the side size of the thumbnail (by default96
)thumb_prefix
- the prefix for the thumbnail files (defaults tot_
)inline
- iftrue
the thumbnail is not saved to a file but directly in the mongoose document, in the thumbnail sub-field, encoded as a string using the Data URI scheme (defaults tofalse
)save
- iftrue
the model instance is saved after every assignment to the image field (change to thefile
sub-property) (defaults totrue
)
Please note that this library re-exports also filePlugin
and make_upload_to_model
.
JavaScript
var mongoose = ;var thumbnailPluginLib = ;var thumbnailPlugin = thumbnailPluginLibthumbnailPlugin;var make_upload_to_model = thumbnailPluginLibmake_upload_to_model; ... var uploads_base = path;var uploads = path;... var SampleSchema = ...;SampleSchema;var SampleModel = db;
CoffeeScript
mongoose = require 'mongoose'filePluginLib = require 'mongoose-thumbnail'filePlugin = filePluginLibfilePluginmake_upload_to_model = filePluginLibmake_upload_to_model ...uploads_base = pathjoin__dirname"uploads"uploads = pathjoinuploads_base"u"... SampleSchema = ...SampleSchemaplugin thumbnailPlugin name: "photo" format: "png" size: 80 inline: false save: true upload_to: make_upload_to_modeluploads'photos' relative_to: uploads_baseSampleModel = dbmodel"SampleModel"SampleSchema
Using with express
PictureSchema = title: StringPictureSchemaplugin thumbnailPlugin name: "photo" inline: falsePicture = dbmodel"Picture"PictureSchema ... apppost "/upload" picture = title: reqbodytitle pictureset'image.file'reqfilesimage picturesave return nexterr if err resredirect '/'
Now in a Jade template, you could have something like:
<img src="/{{ picture.image.thumb.rel }}" />
Otherwise, using thumbnail inlining:
PictureSchema = title: StringPictureSchemaplugin thumbnailPlugin name: "photo" inline: truePicture = dbmodel"Picture"PictureSchema
the template would use:
<img src="{{ picture.image.thumb }}" />
Bugs and pull requests
Please use the github repository to notify bugs and make pull requests.
License
This software is © 2012 Marco Pantaleoni, released under the MIT licence. Use it, fork it.
See the LICENSE file for details.