mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Improved API, added first performance test
This commit is contained in:
parent
a4149ed2a5
commit
9ba9b95648
2
.gitignore
vendored
2
.gitignore
vendored
@ -11,5 +11,7 @@ pids
|
|||||||
logs
|
logs
|
||||||
results
|
results
|
||||||
build
|
build
|
||||||
|
node_modules
|
||||||
|
tests/output.jpg
|
||||||
|
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
|
@ -2,8 +2,7 @@ language: node_js
|
|||||||
node_js:
|
node_js:
|
||||||
- "0.11"
|
- "0.11"
|
||||||
- "0.10"
|
- "0.10"
|
||||||
- "0.8"
|
|
||||||
before_install:
|
before_install:
|
||||||
- sudo apt-get update -qq
|
- 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
|
- sudo ln -s /usr/lib/pkgconfig/vips-7.26.pc /usr/lib/pkgconfig/vips.pc
|
27
README.md
27
README.md
@ -1,4 +1,6 @@
|
|||||||
# sharp (_adj_)
|
# sharp
|
||||||
|
|
||||||
|
_adj_
|
||||||
|
|
||||||
1. clearly defined; distinct: a sharp photographic image.
|
1. clearly defined; distinct: a sharp photographic image.
|
||||||
2. quick, brisk, or spirited.
|
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.
|
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
|
## Prerequisites
|
||||||
|
|
||||||
@ -36,9 +38,24 @@ Ubuntu 13.04:
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var sharp = require("sharp");
|
var sharp = require("sharp");
|
||||||
var cropLandscape = sharp.resize("input.jpg", "output.jpg", 300, 200, "c");
|
sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
|
||||||
var embedPortraitWhiteBorder = sharp.resize("input.jpg", "output.jpg", 200, 300, "w");
|
if (err) {
|
||||||
var embedPortraitBlackBorder = sharp.resize("input.jpg", "output.jpg", 200, 300, "b");
|
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 [](https://travis-ci.org/lovell/sharp)
|
## Testing [](https://travis-ci.org/lovell/sharp)
|
||||||
|
13
index.js
Executable file
13
index.js
Executable file
@ -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)
|
||||||
|
}
|
@ -7,6 +7,12 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/lovell/sharp"
|
"url": "git://github.com/lovell/sharp"
|
||||||
},
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"imagemagick": "*"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "node tests/perf.js"
|
||||||
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "*"
|
"node": "*"
|
||||||
},
|
},
|
||||||
|
@ -170,4 +170,4 @@ extern "C" void init(Handle<Object> target) {
|
|||||||
NODE_SET_METHOD(target, "resize", Resize);
|
NODE_SET_METHOD(target, "resize", Resize);
|
||||||
};
|
};
|
||||||
|
|
||||||
NODE_MODULE(vips, init)
|
NODE_MODULE(sharp, init)
|
||||||
|
BIN
tests/2569067123_aca715a2ee_o.jpg
Normal file
BIN
tests/2569067123_aca715a2ee_o.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 813 KiB |
42
tests/perf.js
Executable file
42
tests/perf.js
Executable file
@ -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);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user