Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including pictoose with all npm packages installed. Try it out:

var pictoose = require("pictoose")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

pictoose v0.0.6

Pictures in Mongoose!


Pictoose on NPM Pictoose downloads

Pictoose is a Mongoose plugin made for make image storage (and video!) easiest possible

Now with image resizing

How to use it

Lets supose that you actually have this code:

// Requirements
var express     = require('express');
var multer      = require('multer');
var mongoose    = require('mongoose');
var Schema      = mongoose.Schema;
// Create Express Server
var app = express();
app.use(multer({dest: './uploads/'}));
// Connect to database

// Car Schema & Model
var CarSchema = new Schema({
    model: String,
var Car = mongoose.model('Car', CarSchema);


console.log('Listening on port 3000');

To use Pictoose all you need to do is require de module, configure it and include the plugin to a Schema by this way:

var Pictoose    = require('pictoose');

Pictoose.Config('RESOURCE_STORAGE_ROOT', './public/');
Pictoose.Config('RESOURCE_STORAGE_URL', '');
Pictoose.Config('RESOURCE_MAIN_URL', '');

CarSchema.plugin(Pictoose.Plugin, ['thumbnail','brand']);

app.get('/resources/:resid', Pictoose.RouteController);

Now every image will be stored in the 'public' folder (RESOURCE_STORAGE_ROOT), Pictoose will construct the URLs using the RESOURCE_STORAGE_URL to return de images.

Also you need to specify a RESOURCE_MAIN_URL used to handle broken links, resizing, etc. and add it to your Express routing.

And finally, will create the fields 'thumbnail' and 'brand', so to save an image with Pictoose al you need to do is this:'/', function(req,res){
    var myCar = new Car(req.body);
    // Just save the image's local URI after it was uploaded and saved by, in this case, Multer
    myCar.thumbnail = req.files.thumbnail.path;
    myCar.brand = req.files.brand.path;;

You can also use a base64 string or a public URL to store images'/', function(req,res){
    var muCar = new Car(req.body);
    myCar.thumbnail = req.body.thumbnail; // (data:image/png;base64,...)
    myCar.brand = '';;

When Pictoose receives a new image/video in base64/filepath, it will validate the file.

Supported file formats:

  • png
  • jpg
  • jpeg
  • gif
  • png
  • bmp
  • webp
  • mp4
  • avi
  • mov
  • mpeg

If Pictoose receives a public URL will not validate anything and will directly store the URL

That's all!

Image Resizing

Image resizing is simple, if we have the following URL generated and stored by Pictoose:

And we add this query:

Will resize the image, store it in his new size, and return the public URL of the new image, the next time we query the same resize options, the image will be already resized and stored.

Resize options are: widthxheight(fill|afit|afil)



RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free