From 3440aa4344160d1721889ec6ee9dc53578f8248a Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Tue, 7 Jun 2016 16:06:15 +0100 Subject: [PATCH] Switch image resizing example from 'jimp' to 'sharp' because it's far faster --- .../NodeServicesExamples/Node/resizeImage.js | 18 ++++++------------ .../Views/Home/ImageResizing.cshtml | 9 ++++++++- samples/misc/NodeServicesExamples/package.json | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/samples/misc/NodeServicesExamples/Node/resizeImage.js b/samples/misc/NodeServicesExamples/Node/resizeImage.js index 119f94a..9f3d9bf 100644 --- a/samples/misc/NodeServicesExamples/Node/resizeImage.js +++ b/samples/misc/NodeServicesExamples/Node/resizeImage.js @@ -1,15 +1,9 @@ -var Jimp = require('jimp'); +var sharp = require('sharp'); module.exports = function(cb, physicalPath, mimeType, maxWidth, maxHeight) { - Jimp.read(physicalPath, function (err, loadedImage) { - if (err) { - cb(err); - } - - loadedImage - .contain(maxWidth > 0 ? maxWidth : Jimp.AUTO, maxHeight > 0 ? maxHeight : Jimp.AUTO) - .getBuffer(mimeType, function(err, buffer) { - cb(err, { base64: buffer && buffer.toString('base64') }); - }); - }); + sharp(physicalPath) + .resize(maxWidth > 0 ? maxWidth : null, maxHeight > 0 ? maxHeight : null) + .toBuffer(function (err, buffer) { + cb(err, { base64: buffer && buffer.toString('base64') }); + }); } diff --git a/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml b/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml index 1ff36af..28652d8 100644 --- a/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml +++ b/samples/misc/NodeServicesExamples/Views/Home/ImageResizing.cshtml @@ -1,12 +1,19 @@

Image Resizing

- This sample shows how the NPM module jimp + This sample shows how the NPM module sharp can be used for dynamic image resizing from within an ASP.NET Core application. There is one copy of the following image on disk, but we can set up an MVC action method that returns it resized to fit within an arbitrary width and height.

+

+ Dependencies: On Windows and Linux, there are no native dependencies. Just running + npm install is enough. On OS X, however, you need to have libvips installed, + which you can get through Homebrew by running + brew install homebrew/science/vips. +

+

Parrot by D Coetzee diff --git a/samples/misc/NodeServicesExamples/package.json b/samples/misc/NodeServicesExamples/package.json index 5c987b4..6f6640d 100644 --- a/samples/misc/NodeServicesExamples/package.json +++ b/samples/misc/NodeServicesExamples/package.json @@ -4,6 +4,6 @@ "dependencies": { "babel-core": "^6.7.4", "babel-preset-es2015": "^6.6.0", - "jimp": "^0.2.24" + "sharp": "^0.15.0" } }