diff --git a/.gitignore b/.gitignore index 2bc8b9f7..bfc4e960 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,7 @@ pids logs results build +node_modules +tests/output.jpg npm-debug.log diff --git a/.travis.yml b/.travis.yml index ef8c916a..26dc54cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,7 @@ language: node_js node_js: - "0.11" - "0.10" - - "0.8" before_install: - sudo apt-get update -qq - - sudo apt-get install -qq libvips-dev + - sudo apt-get install -qq libvips-dev imagemagick - sudo ln -s /usr/lib/pkgconfig/vips-7.26.pc /usr/lib/pkgconfig/vips.pc \ No newline at end of file diff --git a/README.md b/README.md index 221a5a0b..42ac11e4 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# sharp (_adj_) +# sharp + +_adj_ 1. clearly defined; distinct: a sharp photographic image. 2. quick, brisk, or spirited. @@ -9,7 +11,7 @@ The typical use case for this high performance Node.js module is to convert a la It is somewhat opinionated in that it only deals with JPEG images, always obeys the requested dimensions by either cropping or embedding and insists on a mild sharpen of the resulting image. -Under the hood you'll find the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by the University of Southampton. +Under the hood you'll find the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by the University of Southampton. Speed is typically 25-30% faster than the imagemagick equivalent. ## Prerequisites @@ -36,9 +38,24 @@ Ubuntu 13.04: ```javascript var sharp = require("sharp"); -var cropLandscape = sharp.resize("input.jpg", "output.jpg", 300, 200, "c"); -var embedPortraitWhiteBorder = sharp.resize("input.jpg", "output.jpg", 200, 300, "w"); -var embedPortraitBlackBorder = sharp.resize("input.jpg", "output.jpg", 200, 300, "b"); +sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) { + if (err) { + throw err; + } + // output.jpg is cropped input.jpg +}); +sharp.embedWhite("input.jpg", "output.jpg", 200, 300, function(err) { + if (err) { + throw err; + } + // output.jpg contains input.jpg embedded with a white border +}); +sharp.embedBlack("input.jpg", "output.jpg", 200, 300, function(err) { + if (err) { + throw err; + } + // output.jpg contains input.jpg embedded with a black border +}); ``` ## Testing [![Build Status](https://travis-ci.org/lovell/sharp.png?branch=master)](https://travis-ci.org/lovell/sharp) diff --git a/index.js b/index.js new file mode 100755 index 00000000..148d9772 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +var sharp = require("./build/Release/sharp"); + +module.exports.crop = function(input, output, width, height, callback) { + sharp.resize(input, output, width, height, "c", callback) +} + +module.exports.embedWhite = function(input, output, width, height, callback) { + sharp.resize(input, output, width, height, "w", callback) +} + +module.exports.embedBlack = function(input, output, width, height, callback) { + sharp.resize(input, output, width, height, "b", callback) +} diff --git a/package.json b/package.json index b0850251..3b64e186 100755 --- a/package.json +++ b/package.json @@ -7,6 +7,12 @@ "type": "git", "url": "git://github.com/lovell/sharp" }, + "devDependencies": { + "imagemagick": "*" + }, + "scripts": { + "test": "node tests/perf.js" + }, "engines": { "node": "*" }, diff --git a/src/sharp.cc b/src/sharp.cc index be17c2d5..fbad45b7 100755 --- a/src/sharp.cc +++ b/src/sharp.cc @@ -170,4 +170,4 @@ extern "C" void init(Handle target) { NODE_SET_METHOD(target, "resize", Resize); }; -NODE_MODULE(vips, init) +NODE_MODULE(sharp, init) diff --git a/tests/2569067123_aca715a2ee_o.jpg b/tests/2569067123_aca715a2ee_o.jpg new file mode 100644 index 00000000..ad1b5b69 Binary files /dev/null and b/tests/2569067123_aca715a2ee_o.jpg differ diff --git a/tests/perf.js b/tests/perf.js new file mode 100755 index 00000000..6fe98389 --- /dev/null +++ b/tests/perf.js @@ -0,0 +1,42 @@ +var sharp = require("../index"); +var imagemagick = require("imagemagick"); +var assert = require("assert"); + +// http://www.flickr.com/photos/grizdave/2569067123/ +var input = __dirname + "/2569067123_aca715a2ee_o.jpg"; +var output = __dirname + "/output.jpg"; + +var width = 640; +var height = 480; + +// imagemagick +var time = process.hrtime(); +imagemagick.resize({ + srcPath: input, + dstPath: output, + quality: 0.75, + width: width, + height: height +}, function(err) { + if (err) { + throw err; + } + var diff = process.hrtime(time); + imagemagickTime = diff[0] * 1e9 + diff[1]; + console.log("imagemagick took %d nanoseconds", imagemagickTime); + + // sharp + time = process.hrtime(); + sharp.crop(input, output, width, height, function(err) { + if (err) { + throw err; + } + diff = process.hrtime(time); + var sharpTime = diff[0] * 1e9 + diff[1]; + console.log("sharp took %d nanoseconds", sharpTime); + + // diff + assert(sharpTime < imagemagickTime, "sharp was blunt"); + console.log("sharp was %d%% faster", (imagemagickTime - sharpTime) / imagemagickTime * 100); + }); +});