Switch image resizing example from 'jimp' to 'sharp' because it's far faster

This commit is contained in:
SteveSandersonMS
2016-06-07 16:06:15 +01:00
parent 3e82d94f1c
commit 3440aa4344
3 changed files with 15 additions and 14 deletions

View File

@@ -1,15 +1,9 @@
var Jimp = require('jimp'); var sharp = require('sharp');
module.exports = function(cb, physicalPath, mimeType, maxWidth, maxHeight) { module.exports = function(cb, physicalPath, mimeType, maxWidth, maxHeight) {
Jimp.read(physicalPath, function (err, loadedImage) { sharp(physicalPath)
if (err) { .resize(maxWidth > 0 ? maxWidth : null, maxHeight > 0 ? maxHeight : null)
cb(err); .toBuffer(function (err, buffer) {
}
loadedImage
.contain(maxWidth > 0 ? maxWidth : Jimp.AUTO, maxHeight > 0 ? maxHeight : Jimp.AUTO)
.getBuffer(mimeType, function(err, buffer) {
cb(err, { base64: buffer && buffer.toString('base64') }); cb(err, { base64: buffer && buffer.toString('base64') });
}); });
});
} }

View File

@@ -1,12 +1,19 @@
<h1>Image Resizing</h1> <h1>Image Resizing</h1>
<p> <p>
This sample shows how the NPM module <a href="https://www.npmjs.com/package/jimp"><code>jimp</code></a> This sample shows how the NPM module <a href="https://www.npmjs.com/package/sharp"><code>sharp</code></a>
can be used for dynamic image resizing from within an ASP.NET Core application. There is one copy of the 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 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. arbitrary width and height.
</p> </p>
<p>
<strong>Dependencies:</strong> On Windows and Linux, there are no native dependencies. Just running
<code>npm install</code> is enough. On OS X, however, you need to have <code>libvips</code> installed,
which you can get through <a href="http://brew.sh/">Homebrew</a> by running
<code>brew install homebrew/science/vips</code>.
</p>
<p> <p>
<em><a href="https://www.flickr.com/photos/dcoetzee/3572948635">Parrot</a> <em><a href="https://www.flickr.com/photos/dcoetzee/3572948635">Parrot</a>
by <a href="https://www.flickr.com/photos/dcoetzee/">D Coetzee</a> by <a href="https://www.flickr.com/photos/dcoetzee/">D Coetzee</a>

View File

@@ -4,6 +4,6 @@
"dependencies": { "dependencies": {
"babel-core": "^6.7.4", "babel-core": "^6.7.4",
"babel-preset-es2015": "^6.6.0", "babel-preset-es2015": "^6.6.0",
"jimp": "^0.2.24" "sharp": "^0.15.0"
} }
} }