mardi 10 juin 2014

Resize an image with node js

Resize an image with gm & node js:
var fs = require('fs')
 , gm = require('gm')
 , express = require('express');

var port = (process.env.VCAP_APP_PORT || 3000);

var app = module.exports = express();

app.get('/image', function (req, res) {

 gm('DSC03282.JPG')
  .resize(width, height)
  .noProfile()
  .toBuffer(function (err, buffer) {
   res.writeHead(200, {'Content-Type': 'image/jpeg' });
   res.end(buffer, 'binary');
  });
});

app.listen(port);
console.log("Go Image API : 3000");

Create your own Cloud Image Api in few minutes



Links

heroku-buildpack-graphicsmagick : https://github.com/mcollina/heroku-buildpack-graphicsmagick
heroku-buildpack-multi : https://github.com/ddollar/heroku-buildpack-multi
image resize library for node Js : https://github.com/aheckmann/gm

Requirement:

this article : http://ibmbluemix.blogspot.com/2014/05/tips-if-you-have-problem-to-publish.html
the custom buildpack : https://github.com/dam1/cloudfoundry-custom
the Image Api demo : https://github.com/dam1/Imageapi-cloudfoundry

Tuto:

git clone git@github.com:dam1/Image-Service-Api-Example---Node-Js.git

cf push cloudimageapi --no-manifest  -b git://github.com/dam1/cloudfoundry-custom.git

"-b git://github.com/da..." let us choose a custom buildpacks

try http://cloudimageapi.ng.bluemix.net/image?width=400&height=300


You can create your own CDNConnect, Cloudinary, resrc.it, blitline !