mirror of
https://github.com/lovell/sharp.git
synced 2026-02-04 13:46:19 +01:00
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a0d79a78b | ||
|
|
764b57022b | ||
|
|
5f61331d1a | ||
|
|
d0e6a4c0f3 | ||
|
|
f99e42d447 | ||
|
|
9b4387be97 | ||
|
|
9c3631ecb7 | ||
|
|
31ca68fb14 | ||
|
|
d5d85a8697 | ||
|
|
ae9a8b0f57 | ||
|
|
0899252a72 | ||
|
|
16551bc058 | ||
|
|
e9d196f696 | ||
|
|
2f97d04dfa | ||
|
|
e4ca8f44ec | ||
|
|
6b3dc1e350 | ||
|
|
7ffcdb79e0 | ||
|
|
10ce7c6693 | ||
|
|
ccd6012152 | ||
|
|
377662fffc | ||
|
|
d509458ba1 | ||
|
|
be8f35d830 | ||
|
|
7e8af63129 | ||
|
|
f7b8ce1287 | ||
|
|
dde9e94850 | ||
|
|
21f12e74ba |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -12,6 +12,6 @@ logs
|
||||
results
|
||||
build
|
||||
node_modules
|
||||
tests/output.jpg
|
||||
tests/output.*
|
||||
|
||||
npm-debug.log
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.11"
|
||||
- "0.10"
|
||||
before_install:
|
||||
- sudo apt-get update -qq
|
||||
- sudo apt-get install -qq libvips-dev imagemagick
|
||||
- sudo ln -s /usr/lib/pkgconfig/vips-7.26.pc /usr/lib/pkgconfig/vips.pc
|
||||
262
README.md
262
README.md
@@ -1,51 +1,54 @@
|
||||
# sharp
|
||||
# sharp
|
||||
|
||||
_adj_
|
||||
|
||||
1. clearly defined; distinct: a sharp photographic image.
|
||||
2. quick, brisk, or spirited.
|
||||
3. shrewd or astute: a sharp bargainer.
|
||||
4. (Informal.) very stylish: a sharp dresser; a sharp jacket.
|
||||
1. clearly defined; distinct: a sharp photographic image.
|
||||
2. quick, brisk, or spirited.
|
||||
3. shrewd or astute: a sharp bargainer.
|
||||
4. (Informal.) very stylish: a sharp dresser; a sharp jacket.
|
||||
|
||||
The typical use case for this high speed Node.js module is to convert a large JPEG image to smaller JPEG images of varying dimensions.
|
||||
The typical use case for this high speed Node.js module is to convert large JPEG, PNG, WebP and TIFF images to smaller images of varying dimensions.
|
||||
|
||||
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.
|
||||
The performance of JPEG resizing is typically 15x-25x faster than ImageMagick and GraphicsMagick, based mainly on the number of CPU cores available.
|
||||
|
||||
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.
|
||||
This module supports reading and writing images to and from both the filesystem and Buffer objects (TIFF is limited to filesystem only). Everything remains non-blocking thanks to _libuv_.
|
||||
|
||||
Performance is 4x-8x faster than ImageMagick and 2x-4x faster than GraphicsMagick, based mainly on the number of CPU cores available.
|
||||
Anyone who has used the Node.js bindings for [GraphicsMagick](https://github.com/aheckmann/gm) will find the API similarly expressive.
|
||||
|
||||
This module is powered by the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by John Cupitt.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* Node.js v0.8+
|
||||
* node-gyp
|
||||
* libvips-dev 7.28+
|
||||
* [libvips](https://github.com/jcupitt/libvips) v7.38.5+
|
||||
|
||||
```
|
||||
sudo npm install -g node-gyp
|
||||
sudo apt-get install libvips-dev
|
||||
```
|
||||
### Install libvips on Mac OS
|
||||
|
||||
When installed as a package, please symlink `vips-7.28.pc` (or later, installed with libvips-dev) as `/usr/lib/pkgconfig/vips.pc`. To do this in Ubuntu 13.04 (64-bit), use:
|
||||
brew install homebrew/science/vips
|
||||
|
||||
sudo ln -s /usr/lib/x86_64-linux-gnu/pkgconfig/vips-7.28.pc /usr/lib/pkgconfig/vips.pc
|
||||
### Install libvips on Ubuntu/Debian Linux
|
||||
|
||||
sudo apt-get install automake build-essential git gobject-introspection gtk-doc-tools libfftw3-dev libglib2.0-dev libjpeg-turbo8-dev libpng12-dev libwebp-dev libtiff5-dev liborc-0.4-dev libxml2-dev swig
|
||||
git clone https://github.com/jcupitt/libvips.git
|
||||
cd libvips
|
||||
./bootstrap.sh
|
||||
./configure --enable-debug=no
|
||||
make
|
||||
sudo make install
|
||||
sudo ldconfig
|
||||
|
||||
## Install
|
||||
|
||||
npm install sharp
|
||||
|
||||
## Usage
|
||||
|
||||
var sharp = require("sharp");
|
||||
|
||||
### crop(inputPath, outputPath, width, height, callback)
|
||||
|
||||
Scale and crop JPEG `inputPath` to `width` x `height` and write JPEG to `outputPath` calling `callback` when complete.
|
||||
|
||||
Example:
|
||||
## Usage examples
|
||||
|
||||
```javascript
|
||||
sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
|
||||
var sharp = require('sharp');
|
||||
```
|
||||
|
||||
```javascript
|
||||
sharp('input.jpg').resize(300, 200).write('output.jpg', function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
@@ -54,64 +57,205 @@ sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
|
||||
});
|
||||
```
|
||||
|
||||
### embedWhite(inputPath, outputPath, width, height, callback)
|
||||
|
||||
Scale and embed JPEG `inputPath` to `width` x `height` using a white canvas and write JPEG to `outputPath` calling `callback` when complete.
|
||||
|
||||
```javascript
|
||||
sharp.embedWhite("input.jpg", "output.jpg", 200, 300, function(err) {
|
||||
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
// output.jpg is a 200 pixels wide and 300 pixels high image
|
||||
// containing a scaled version of input.jpg embedded on a white canvas
|
||||
// buffer contains progressive JPEG image data, 200 pixels high
|
||||
});
|
||||
```
|
||||
|
||||
### embedBlack(inputPath, outputPath, width, height, callback)
|
||||
|
||||
Scale and embed JPEG `inputPath` to `width` x `height` using a black canvas and write JPEG to `outputPath` calling `callback` when complete.
|
||||
|
||||
```javascript
|
||||
sharp.embedBlack("input.jpg", "output.jpg", 200, 300, function(err) {
|
||||
sharp('input.png').resize(300).sharpen().webp(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
// output.jpg is a 200 pixels wide and 300 pixels high image
|
||||
// containing a scaled version of input.jpg embedded on a black canvas
|
||||
// buffer contains sharpened WebP image data (converted from PNG), 300 pixels wide
|
||||
});
|
||||
```
|
||||
|
||||
```javascript
|
||||
sharp(buffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
// output.tiff is a 200 pixels wide and 300 pixels high image containing a scaled
|
||||
// version, embedded on a white canvas, of the image data in buffer
|
||||
});
|
||||
```
|
||||
|
||||
```javascript
|
||||
sharp('input.jpg').resize(200, 300).embedBlack().webp(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
// buffer contains WebP image data of a 200 pixels wide and 300 pixels high image
|
||||
// containing a scaled version, embedded on a black canvas, of input.png
|
||||
});
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### sharp(input)
|
||||
|
||||
Constructor to which further methods are chained.
|
||||
|
||||
`input` can either be a filename String or a Buffer.
|
||||
|
||||
### resize(width, [height])
|
||||
|
||||
Scale to `width` x `height`. By default, the resized image is cropped to the exact size specified.
|
||||
|
||||
`width` is the Number of pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
|
||||
|
||||
`height` is the Number of pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
|
||||
|
||||
### crop()
|
||||
|
||||
Crop the resized image to the exact size specified, the default behaviour.
|
||||
|
||||
### embedWhite()
|
||||
|
||||
Embed the resized image on a white background of the exact size specified.
|
||||
|
||||
### embedBlack()
|
||||
|
||||
Embed the resized image on a black background of the exact size specified.
|
||||
|
||||
### sharpen()
|
||||
|
||||
Perform a mild sharpen of the resultant image. This typically reduces performance by 30%.
|
||||
|
||||
### progressive()
|
||||
|
||||
Use progressive (interlace) scan for the output. This typically reduces performance by 30%.
|
||||
|
||||
### sequentialRead()
|
||||
|
||||
An advanced setting that switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`. This will reduce memory usage and can improve performance on some systems.
|
||||
|
||||
### write(filename, callback)
|
||||
|
||||
`filename` is a String containing the filename to write the image data to. The format is inferred from the extension, with JPEG, PNG, WebP and TIFF supported.
|
||||
|
||||
`callback` is called with a single argument `(err)` containing an error message, if any.
|
||||
|
||||
### jpeg(callback)
|
||||
|
||||
Write JPEG image data to a Buffer.
|
||||
|
||||
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant JPEG image data.
|
||||
|
||||
### png(callback)
|
||||
|
||||
Write PNG image data to a Buffer.
|
||||
|
||||
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant PNG image data.
|
||||
|
||||
### webp(callback)
|
||||
|
||||
Write WebP image data to a Buffer.
|
||||
|
||||
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant WebP image data.
|
||||
|
||||
### toBuffer(callback)
|
||||
|
||||
Write image data to a Buffer, the format of which will match the input image.
|
||||
|
||||
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data.
|
||||
|
||||
### sharp.cache([limit])
|
||||
|
||||
If `limit` is provided, set the (soft) limit of _libvips_ working/cache memory to this value in MB. The default value is 100.
|
||||
|
||||
This method always returns cache statistics, useful for determining how much working memory is required for a particular task.
|
||||
|
||||
Warnings such as _Application transferred too many scanlines_ are a good indicator you've set this value too low.
|
||||
|
||||
```javascript
|
||||
var stats = sharp.cache(); // { current: 98, high: 115, limit: 100 }
|
||||
sharp.cache(200); // { current: 98, high: 115, limit: 200 }
|
||||
sharp.cache(50); // { current: 49, high: 115, limit: 50 }
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
npm install --dev sharp
|
||||
npm test
|
||||
|
||||
## Performance
|
||||
|
||||
### AMD Athlon 4x core 3.3GHz 512KB L2
|
||||
Test environment:
|
||||
|
||||
* imagemagick x 5.55 ops/sec ±0.45% (31 runs sampled)
|
||||
* gm x 10.31 ops/sec ±3.57% (53 runs sampled)
|
||||
* epeg x 27.79 ops/sec ±0.12% (69 runs sampled)
|
||||
* sharp x 31.52 ops/sec ±8.74% (80 runs sampled)
|
||||
* AMD Athlon 4 core 3.3GHz 512KB L2 CPU 1333 DDR3
|
||||
* libvips 7.38
|
||||
* libjpeg-turbo8 1.3.0
|
||||
* libpng 1.6.6
|
||||
* zlib1g 1.2.7
|
||||
* libwebp 0.3.0
|
||||
* libtiff 4.0.2
|
||||
|
||||
### AWS t1.micro
|
||||
`-file-buffer` indicates read from file and write to buffer, `-buffer-file` indicates read from buffer and write to file etc.
|
||||
|
||||
* imagemagick x 1.36 ops/sec ±0.96% (11 runs sampled)
|
||||
* sharp x 12.42 ops/sec ±5.84% (64 runs sampled)
|
||||
`-sharpen`, `-progressive` etc. demonstrate the negative effect of options on performance.
|
||||
|
||||
### AWS m1.medium
|
||||
### JPEG
|
||||
|
||||
* imagemagick x 1.38 ops/sec ±0.45% (11 runs sampled)
|
||||
* sharp x 12.66 ops/sec ±5.54% (65 runs sampled)
|
||||
* imagemagick x 5.53 ops/sec ±0.62% (31 runs sampled)
|
||||
* gm-file-file x 4.10 ops/sec ±0.41% (25 runs sampled)
|
||||
* gm-file-buffer x 4.10 ops/sec ±0.36% (25 runs sampled)
|
||||
* epeg-file-file x 23.82 ops/sec ±0.18% (60 runs sampled)
|
||||
* epeg-file-buffer x 23.98 ops/sec ±0.16% (61 runs sampled)
|
||||
|
||||
### AWS c1.medium
|
||||
* sharp-buffer-file x 20.76 ops/sec ±0.55% (54 runs sampled)
|
||||
* sharp-buffer-buffer x 20.90 ops/sec ±0.26% (54 runs sampled)
|
||||
* sharp-file-file x 91.78 ops/sec ±0.38% (88 runs sampled)
|
||||
* sharp-file-buffer x __93.05 ops/sec__ ±0.61% (76 runs sampled)
|
||||
|
||||
* imagemagick x 2.10 ops/sec ±0.67% (15 runs sampled)
|
||||
* sharp x 18.97 ops/sec ±10.54% (52 runs sampled)
|
||||
* sharp-file-buffer-sharpen x 63.09 ops/sec ±5.58% (63 runs sampled)
|
||||
* sharp-file-buffer-progressive x 61.68 ops/sec ±0.53% (76 runs sampled)
|
||||
* sharp-file-buffer-sequentialRead x 60.66 ops/sec ±0.38% (75 runs sampled)
|
||||
|
||||
### AWS m3.xlarge
|
||||
### PNG
|
||||
|
||||
* imagemagick x 4.46 ops/sec ±0.33% (26 runs sampled)
|
||||
* sharp x 28.89 ops/sec ±7.75% (74 runs sampled)
|
||||
* imagemagick x 4.27 ops/sec ±0.21% (25 runs sampled)
|
||||
* gm-file-file x 8.33 ops/sec ±0.19% (44 runs sampled)
|
||||
* gm-file-buffer x 7.45 ops/sec ±0.16% (40 runs sampled)
|
||||
|
||||
* sharp-buffer-file x 4.94 ops/sec ±118.46% (26 runs sampled)
|
||||
* sharp-buffer-buffer x 12.59 ops/sec ±0.55% (64 runs sampled)
|
||||
* sharp-file-file x 44.06 ops/sec ±6.86% (75 runs sampled)
|
||||
* sharp-file-buffer x __46.29 ops/sec__ ±0.38% (76 runs sampled)
|
||||
|
||||
* sharp-file-buffer-sharpen x 38.86 ops/sec ±0.22% (65 runs sampled)
|
||||
* sharp-file-buffer-progressive x 46.35 ops/sec ±0.20% (76 runs sampled)
|
||||
* sharp-file-buffer-sequentialRead x 29.02 ops/sec ±0.62% (72 runs sampled)
|
||||
|
||||
### WebP
|
||||
|
||||
* sharp-buffer-file x 3.30 ops/sec ±117.14% (19 runs sampled)
|
||||
* sharp-buffer-buffer x 7.66 ops/sec ±5.83% (43 runs sampled)
|
||||
* sharp-file-file x 9.88 ops/sec ±0.98% (52 runs sampled)
|
||||
* sharp-file-buffer x 9.95 ops/sec ±0.25% (52 runs sampled)
|
||||
* sharp-file-buffer-sharpen x 9.05 ops/sec ±0.36% (48 runs sampled)
|
||||
* sharp-file-buffer-sequentialRead x 9.87 ops/sec ±0.98% (52 runs sampled)
|
||||
|
||||
### TIFF
|
||||
|
||||
* sharp-file-file x 68.24 ops/sec ±5.93% (85 runs sampled)
|
||||
* sharp-file-file-sharpen x 50.76 ops/sec ±0.52% (82 runs sampled)
|
||||
* sharp-file-file-sequentialRead x 36.37 ops/sec ±0.90% (87 runs sampled)
|
||||
|
||||
## Licence
|
||||
|
||||
Copyright 2013, 2014 Lovell Fuller
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0.html)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -7,11 +7,13 @@
|
||||
'<!@(PKG_CONFIG_PATH="/usr/lib/pkgconfig" pkg-config --libs vips)'
|
||||
],
|
||||
'include_dirs': [
|
||||
'/usr/local/include/glib-2.0',
|
||||
'/usr/local/lib/glib-2.0/include',
|
||||
'/usr/include/glib-2.0',
|
||||
'/usr/lib/glib-2.0/include',
|
||||
'/usr/lib/x86_64-linux-gnu/glib-2.0/include'
|
||||
],
|
||||
'cflags': ['-fexceptions'],
|
||||
'cflags_cc': ['-fexceptions']
|
||||
'cflags': ['-fexceptions', '-pedantic', '-Wall', '-O3'],
|
||||
'cflags_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
||||
}]
|
||||
}
|
||||
|
||||
135
index.js
135
index.js
@@ -1,13 +1,128 @@
|
||||
var sharp = require("./build/Release/sharp");
|
||||
/*jslint node: true */
|
||||
'use strict';
|
||||
|
||||
module.exports.crop = function(input, output, width, height, callback) {
|
||||
sharp.resize(input, output, width, height, "c", callback)
|
||||
}
|
||||
var sharp = require('./build/Release/sharp');
|
||||
|
||||
module.exports.embedWhite = function(input, output, width, height, callback) {
|
||||
sharp.resize(input, output, width, height, "w", callback)
|
||||
}
|
||||
var Sharp = function(input) {
|
||||
if (!(this instanceof Sharp)) {
|
||||
return new Sharp(input);
|
||||
}
|
||||
this.options = {
|
||||
width: -1,
|
||||
height: -1,
|
||||
canvas: 'c',
|
||||
sharpen: false,
|
||||
progressive: false,
|
||||
sequentialRead: false,
|
||||
output: '__jpeg'
|
||||
};
|
||||
if (typeof input === 'string') {
|
||||
this.options.inFile = input;
|
||||
} else if (typeof input ==='object' && input instanceof Buffer) {
|
||||
this.options.inBuffer = input;
|
||||
} else {
|
||||
throw 'Unsupported input ' + typeof input;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
module.exports = Sharp;
|
||||
|
||||
module.exports.embedBlack = function(input, output, width, height, callback) {
|
||||
sharp.resize(input, output, width, height, "b", callback)
|
||||
}
|
||||
Sharp.prototype.crop = function() {
|
||||
this.options.canvas = 'c';
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.embedWhite = function() {
|
||||
this.options.canvas = 'w';
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.embedBlack = function() {
|
||||
this.options.canvas = 'b';
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.sharpen = function(sharpen) {
|
||||
this.options.sharpen = (typeof sharpen === 'boolean') ? sharpen : true;
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.progressive = function(progressive) {
|
||||
this.options.progressive = (typeof progressive === 'boolean') ? progressive : true;
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.sequentialRead = function(sequentialRead) {
|
||||
this.options.sequentialRead = (typeof sequentialRead === 'boolean') ? sequentialRead : true;
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.resize = function(width, height) {
|
||||
if (!width) {
|
||||
this.options.width = -1;
|
||||
} else {
|
||||
if (!Number.isNaN(width)) {
|
||||
this.options.width = width;
|
||||
} else {
|
||||
throw 'Invalid width ' + width;
|
||||
}
|
||||
}
|
||||
if (!height) {
|
||||
this.options.height = -1;
|
||||
} else {
|
||||
if (!Number.isNaN(height)) {
|
||||
this.options.height = height;
|
||||
} else {
|
||||
throw 'Invalid height ' + height;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.write = function(output, callback) {
|
||||
if (!output || output.length === 0) {
|
||||
throw 'Invalid output';
|
||||
} else {
|
||||
this._sharp(output, callback);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
Sharp.prototype.toBuffer = function(callback) {
|
||||
return this._sharp('__input', callback);
|
||||
};
|
||||
|
||||
Sharp.prototype.jpeg = function(callback) {
|
||||
return this._sharp('__jpeg', callback);
|
||||
};
|
||||
|
||||
Sharp.prototype.png = function(callback) {
|
||||
return this._sharp('__png', callback);
|
||||
};
|
||||
|
||||
Sharp.prototype.webp = function(callback) {
|
||||
return this._sharp('__webp', callback);
|
||||
};
|
||||
|
||||
Sharp.prototype._sharp = function(output, callback) {
|
||||
sharp.resize(
|
||||
this.options.inFile,
|
||||
this.options.inBuffer,
|
||||
output,
|
||||
this.options.width,
|
||||
this.options.height,
|
||||
this.options.canvas,
|
||||
this.options.sharpen,
|
||||
this.options.progressive,
|
||||
this.options.sequentialRead,
|
||||
callback
|
||||
);
|
||||
return this;
|
||||
};
|
||||
|
||||
module.exports.cache = function(limit) {
|
||||
if (Number.isNaN(limit)) {
|
||||
limit = null;
|
||||
}
|
||||
return sharp.cache(limit);
|
||||
};
|
||||
|
||||
40
package.json
40
package.json
@@ -1,34 +1,40 @@
|
||||
{
|
||||
"name": "sharp",
|
||||
"version": "0.0.4",
|
||||
"version": "0.2.0",
|
||||
"author": "Lovell Fuller",
|
||||
"description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library",
|
||||
"scripts": {
|
||||
"test": "node tests/unit && node tests/perf"
|
||||
},
|
||||
"main": "index.js",
|
||||
"description": "High performance Node.js module to resize JPEG images using the libvips image processing library",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/lovell/sharp"
|
||||
},
|
||||
"devDependencies": {
|
||||
"imagemagick": "*",
|
||||
"gm": "*",
|
||||
"epeg": "*",
|
||||
"benchmark": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node tests/perf.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
},
|
||||
"keywords": [
|
||||
"jpeg",
|
||||
"png",
|
||||
"webp",
|
||||
"tiff",
|
||||
"resize",
|
||||
"thumbnail",
|
||||
"sharpen",
|
||||
"crop",
|
||||
"embed",
|
||||
"libvips",
|
||||
"fast"
|
||||
"vips",
|
||||
"fast",
|
||||
"buffer"
|
||||
],
|
||||
"author": "Lovell Fuller",
|
||||
"license": "Apache 2.0"
|
||||
"devDependencies": {
|
||||
"imagemagick": "*",
|
||||
"gm": "*",
|
||||
"epeg": "*",
|
||||
"async": "*",
|
||||
"benchmark": "*"
|
||||
},
|
||||
"license": "Apache 2.0",
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
}
|
||||
}
|
||||
465
src/sharp.cc
465
src/sharp.cc
@@ -1,173 +1,378 @@
|
||||
#include <node.h>
|
||||
#include <node_buffer.h>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <string.h>
|
||||
#include <vips/vips.h>
|
||||
|
||||
using namespace v8;
|
||||
using namespace node;
|
||||
|
||||
// Free VipsImage children when object goes out of scope
|
||||
// Thanks due to https://github.com/dosx/node-vips
|
||||
class ImageFreer {
|
||||
public:
|
||||
ImageFreer() {}
|
||||
~ImageFreer() {
|
||||
for (uint16_t i = 0; i < v_.size(); i++) {
|
||||
if (v_[i] != NULL) {
|
||||
g_object_unref(v_[i]);
|
||||
}
|
||||
}
|
||||
v_.clear();
|
||||
}
|
||||
void add(VipsImage* i) { v_.push_back(i); }
|
||||
private:
|
||||
std::vector<VipsImage*> v_;
|
||||
};
|
||||
|
||||
struct ResizeBaton {
|
||||
std::string src;
|
||||
std::string dst;
|
||||
int cols;
|
||||
int rows;
|
||||
struct resize_baton {
|
||||
std::string file_in;
|
||||
void* buffer_in;
|
||||
size_t buffer_in_len;
|
||||
std::string file_out;
|
||||
void* buffer_out;
|
||||
size_t buffer_out_len;
|
||||
int width;
|
||||
int height;
|
||||
bool crop;
|
||||
int embed;
|
||||
VipsExtend extend;
|
||||
bool sharpen;
|
||||
bool progessive;
|
||||
VipsAccess access_method;
|
||||
std::string err;
|
||||
Persistent<Function> callback;
|
||||
|
||||
resize_baton(): buffer_in_len(0), buffer_out_len(0) {}
|
||||
};
|
||||
|
||||
void ResizeAsync(uv_work_t *work) {
|
||||
ResizeBaton* baton = static_cast<ResizeBaton*>(work->data);
|
||||
typedef enum {
|
||||
JPEG,
|
||||
PNG,
|
||||
WEBP,
|
||||
TIFF
|
||||
} ImageType;
|
||||
|
||||
VipsImage *in = vips_image_new_mode((baton->src).c_str(), "p");
|
||||
vips_jpegload((baton->src).c_str(), &in, NULL);
|
||||
if (in == NULL) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
ImageFreer freer;
|
||||
freer.add(in);
|
||||
unsigned char MARKER_JPEG[] = {0xff, 0xd8};
|
||||
unsigned char MARKER_PNG[] = {0x89, 0x50};
|
||||
unsigned char MARKER_WEBP[] = {0x52, 0x49};
|
||||
|
||||
VipsImage* img = in;
|
||||
VipsImage* t[4];
|
||||
|
||||
if (im_open_local_array(img, t, 4, "temp", "p")) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
|
||||
double xfactor = static_cast<double>(img->Xsize) / std::max(baton->cols, 1);
|
||||
double yfactor = static_cast<double>(img->Ysize) / std::max(baton->rows, 1);
|
||||
double factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
|
||||
factor = std::max(factor, 1.0);
|
||||
int shrink = floor(factor);
|
||||
double residual = shrink / factor;
|
||||
|
||||
// Use im_shrink with the integral reduction
|
||||
if (im_shrink(img, t[0], shrink, shrink)) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// Use im_affinei with the remaining float part using bilinear interpolation
|
||||
if (im_affinei_all(t[0], t[1], vips_interpolate_bilinear_static(), residual, 0, 0, residual, 0, 0)) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
img = t[1];
|
||||
|
||||
if (baton->crop) {
|
||||
int width = std::min(img->Xsize, baton->cols);
|
||||
int height = std::min(img->Ysize, baton->rows);
|
||||
int left = (img->Xsize - width + 1) / 2;
|
||||
int top = (img->Ysize - height + 1) / 2;
|
||||
if (im_extract_area(img, t[2], left, top, width, height)) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
img = t[2];
|
||||
} else {
|
||||
int left = (baton->cols - img->Xsize) / 2;
|
||||
int top = (baton->rows - img->Ysize) / 2;
|
||||
if (im_embed(img, t[2], baton->embed, left, top, baton->cols, baton->rows)) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
img = t[2];
|
||||
}
|
||||
|
||||
// Mild sharpen
|
||||
INTMASK* sharpen = im_create_imaskv("sharpen", 3, 3,
|
||||
-1, -1, -1,
|
||||
-1, 32, -1,
|
||||
-1, -1, -1);
|
||||
sharpen->scale = 24;
|
||||
if (im_conv(img, t[3], sharpen)) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
img = t[3];
|
||||
|
||||
if (vips_jpegsave(img, baton->dst.c_str(), "Q", 80, "profile", "none", "optimize_coding", TRUE, NULL)) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
}
|
||||
bool ends_with(std::string const &str, std::string const &end) {
|
||||
return str.length() >= end.length() && 0 == str.compare(str.length() - end.length(), end.length(), end);
|
||||
}
|
||||
|
||||
void ResizeAsyncAfter(uv_work_t *work, int status) {
|
||||
HandleScope scope;
|
||||
bool is_jpeg(std::string const &str) {
|
||||
return ends_with(str, ".jpg") || ends_with(str, ".jpeg") || ends_with(str, ".JPG") || ends_with(str, ".JPEG");
|
||||
}
|
||||
|
||||
ResizeBaton *baton = static_cast<ResizeBaton*>(work->data);
|
||||
bool is_png(std::string const &str) {
|
||||
return ends_with(str, ".png") || ends_with(str, ".PNG");
|
||||
}
|
||||
|
||||
Local<Value> argv[1];
|
||||
if (!baton->err.empty()) {
|
||||
argv[0] = String::New(baton->err.data(), baton->err.size());
|
||||
bool is_webp(std::string const &str) {
|
||||
return ends_with(str, ".webp") || ends_with(str, ".WEBP");
|
||||
}
|
||||
|
||||
bool is_tiff(std::string const &str) {
|
||||
return ends_with(str, ".tif") || ends_with(str, ".tiff") || ends_with(str, ".TIF") || ends_with(str, ".TIFF");
|
||||
}
|
||||
|
||||
void resize_error(resize_baton *baton, VipsImage *unref) {
|
||||
(baton->err).append(vips_error_buffer());
|
||||
vips_error_clear();
|
||||
g_object_unref(unref);
|
||||
vips_thread_shutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
void resize_async(uv_work_t *work) {
|
||||
resize_baton* baton = static_cast<resize_baton*>(work->data);
|
||||
|
||||
// Input
|
||||
ImageType inputImageType = JPEG;
|
||||
VipsImage *in = vips_image_new();
|
||||
if (baton->buffer_in_len > 1) {
|
||||
if (memcmp(MARKER_JPEG, baton->buffer_in, 2) == 0) {
|
||||
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else if(memcmp(MARKER_PNG, baton->buffer_in, 2) == 0) {
|
||||
inputImageType = PNG;
|
||||
if (vips_pngload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else if(memcmp(MARKER_WEBP, baton->buffer_in, 2) == 0) {
|
||||
inputImageType = WEBP;
|
||||
if (vips_webpload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else {
|
||||
resize_error(baton, in);
|
||||
(baton->err).append("Unsupported input buffer");
|
||||
return;
|
||||
}
|
||||
} else if (is_jpeg(baton->file_in)) {
|
||||
if (vips_jpegload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else if (is_png(baton->file_in)) {
|
||||
inputImageType = PNG;
|
||||
if (vips_pngload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else if (is_webp(baton->file_in)) {
|
||||
inputImageType = WEBP;
|
||||
if (vips_webpload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else if (is_tiff(baton->file_in)) {
|
||||
inputImageType = TIFF;
|
||||
if (vips_tiffload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else {
|
||||
argv[0] = Local<Value>::New(Null());
|
||||
resize_error(baton, in);
|
||||
(baton->err).append("Unsupported input file " + baton->file_in);
|
||||
return;
|
||||
}
|
||||
|
||||
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
|
||||
// Scaling calculations
|
||||
double factor;
|
||||
if (baton->width > 0 && baton->height > 0) {
|
||||
// Fixed width and height
|
||||
double xfactor = (double)(in->Xsize) / (double)(baton->width);
|
||||
double yfactor = (double)(in->Ysize) / (double)(baton->height);
|
||||
factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
|
||||
} else if (baton->width > 0) {
|
||||
// Fixed width, auto height
|
||||
factor = (double)(in->Xsize) / (double)(baton->width);
|
||||
baton->height = floor((double)(in->Ysize) / factor);
|
||||
} else if (baton->height > 0) {
|
||||
// Fixed height, auto width
|
||||
factor = (double)(in->Ysize) / (double)(baton->height);
|
||||
baton->width = floor((double)(in->Xsize) / factor);
|
||||
} else {
|
||||
// Identity transform
|
||||
factor = 1;
|
||||
baton->width = in->Xsize;
|
||||
baton->height = in->Ysize;
|
||||
}
|
||||
int shrink = floor(factor);
|
||||
if (shrink < 1) {
|
||||
shrink = 1;
|
||||
}
|
||||
double residual = shrink / (double)factor;
|
||||
|
||||
// Try to use libjpeg shrink-on-load
|
||||
int shrink_on_load = 1;
|
||||
if (inputImageType == JPEG) {
|
||||
if (shrink >= 8) {
|
||||
factor = factor / 8;
|
||||
shrink_on_load = 8;
|
||||
} else if (shrink >= 4) {
|
||||
factor = factor / 4;
|
||||
shrink_on_load = 4;
|
||||
} else if (shrink >= 2) {
|
||||
factor = factor / 2;
|
||||
shrink_on_load = 2;
|
||||
}
|
||||
}
|
||||
VipsImage *shrunk_on_load = vips_image_new();
|
||||
if (shrink_on_load > 1) {
|
||||
// Recalculate integral shrink and double residual
|
||||
factor = std::max(factor, 1.0);
|
||||
shrink = floor(factor);
|
||||
residual = shrink / factor;
|
||||
// Reload input using shrink-on-load
|
||||
if (baton->buffer_in_len > 1) {
|
||||
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
} else {
|
||||
if (vips_jpegload((baton->file_in).c_str(), &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
||||
return resize_error(baton, in);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vips_copy(in, &shrunk_on_load, NULL);
|
||||
}
|
||||
g_object_unref(in);
|
||||
|
||||
VipsImage *shrunk = vips_image_new();
|
||||
if (shrink > 1) {
|
||||
// Use vips_shrink with the integral reduction
|
||||
if (vips_shrink(shrunk_on_load, &shrunk, shrink, shrink, NULL)) {
|
||||
return resize_error(baton, shrunk_on_load);
|
||||
}
|
||||
} else {
|
||||
vips_copy(shrunk_on_load, &shrunk, NULL);
|
||||
}
|
||||
g_object_unref(shrunk_on_load);
|
||||
|
||||
// Use vips_affine with the remaining float part using bilinear interpolation
|
||||
VipsImage *affined = vips_image_new();
|
||||
if (residual != 0) {
|
||||
if (vips_affine(shrunk, &affined, residual, 0, 0, residual, "interpolate", vips_interpolate_bilinear_static(), NULL)) {
|
||||
return resize_error(baton, shrunk);
|
||||
}
|
||||
} else {
|
||||
vips_copy(shrunk, &affined, NULL);
|
||||
}
|
||||
g_object_unref(shrunk);
|
||||
|
||||
// Crop/embed
|
||||
VipsImage *canvased = vips_image_new();
|
||||
if (affined->Xsize != baton->width || affined->Ysize != baton->height) {
|
||||
if (baton->crop) {
|
||||
// Crop
|
||||
int width = std::min(affined->Xsize, baton->width);
|
||||
int height = std::min(affined->Ysize, baton->height);
|
||||
int left = (affined->Xsize - width + 1) / 2;
|
||||
int top = (affined->Ysize - height + 1) / 2;
|
||||
if (vips_extract_area(affined, &canvased, left, top, width, height, NULL)) {
|
||||
return resize_error(baton, affined);
|
||||
}
|
||||
} else {
|
||||
// Embed
|
||||
int left = (baton->width - affined->Xsize) / 2;
|
||||
int top = (baton->height - affined->Ysize) / 2;
|
||||
if (vips_embed(affined, &canvased, left, top, baton->width, baton->height, "extend", baton->extend, NULL)) {
|
||||
return resize_error(baton, affined);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vips_copy(affined, &canvased, NULL);
|
||||
}
|
||||
g_object_unref(affined);
|
||||
|
||||
// Mild sharpen
|
||||
VipsImage *sharpened = vips_image_new();
|
||||
if (baton->sharpen) {
|
||||
INTMASK* sharpen = im_create_imaskv("sharpen", 3, 3,
|
||||
-1, -1, -1,
|
||||
-1, 32, -1,
|
||||
-1, -1, -1);
|
||||
sharpen->scale = 24;
|
||||
if (im_conv(canvased, sharpened, sharpen)) {
|
||||
return resize_error(baton, canvased);
|
||||
}
|
||||
} else {
|
||||
vips_copy(canvased, &sharpened, NULL);
|
||||
}
|
||||
g_object_unref(canvased);
|
||||
|
||||
// Output
|
||||
if (baton->file_out == "__jpeg" || (baton->file_out == "__input" && inputImageType == JPEG)) {
|
||||
// Write JPEG to buffer
|
||||
if (vips_jpegsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else if (baton->file_out == "__png" || (baton->file_out == "__input" && inputImageType == PNG)) {
|
||||
// Write PNG to buffer
|
||||
if (vips_pngsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else if (baton->file_out == "__webp" || (baton->file_out == "__input" && inputImageType == WEBP)) {
|
||||
// Write WEBP to buffer
|
||||
if (vips_webpsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else if (is_jpeg(baton->file_out)) {
|
||||
// Write JPEG to file
|
||||
if (vips_jpegsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else if (is_png(baton->file_out)) {
|
||||
// Write PNG to file
|
||||
if (vips_pngsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else if (is_webp(baton->file_out)) {
|
||||
// Write WEBP to file
|
||||
if (vips_webpsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "Q", 80, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else if (is_tiff(baton->file_out)) {
|
||||
// Write TIFF to file
|
||||
if (vips_tiffsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", 80, NULL)) {
|
||||
return resize_error(baton, sharpened);
|
||||
}
|
||||
} else {
|
||||
(baton->err).append("Unsupported output " + baton->file_out);
|
||||
}
|
||||
g_object_unref(sharpened);
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
void resize_async_after(uv_work_t *work, int status) {
|
||||
HandleScope scope;
|
||||
|
||||
resize_baton *baton = static_cast<resize_baton*>(work->data);
|
||||
|
||||
Handle<Value> argv[2] = { Null(), Null() };
|
||||
if (!baton->err.empty()) {
|
||||
// Error
|
||||
argv[0] = scope.Close(String::New(baton->err.data(), baton->err.size()));
|
||||
} else if (baton->buffer_out_len > 0) {
|
||||
// Buffer
|
||||
Buffer *slowBuffer = Buffer::New(baton->buffer_out_len);
|
||||
memcpy(Buffer::Data(slowBuffer), baton->buffer_out, baton->buffer_out_len);
|
||||
Local<Object> globalObj = Context::GetCurrent()->Global();
|
||||
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
|
||||
Handle<Value> constructorArgs[3] = { slowBuffer->handle_, v8::Integer::New(baton->buffer_out_len), v8::Integer::New(0) };
|
||||
argv[1] = scope.Close(bufferConstructor->NewInstance(3, constructorArgs));
|
||||
g_free(baton->buffer_out);
|
||||
}
|
||||
|
||||
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
|
||||
baton->callback.Dispose();
|
||||
delete baton;
|
||||
delete work;
|
||||
}
|
||||
|
||||
Handle<Value> Resize(const Arguments& args) {
|
||||
Handle<Value> resize(const Arguments& args) {
|
||||
HandleScope scope;
|
||||
|
||||
ResizeBaton *baton = new ResizeBaton;
|
||||
baton->src = *String::Utf8Value(args[0]->ToString());
|
||||
baton->dst = *String::Utf8Value(args[1]->ToString());
|
||||
baton->cols = args[2]->Int32Value();
|
||||
baton->rows = args[3]->Int32Value();
|
||||
Local<String> canvas = args[4]->ToString();
|
||||
resize_baton *baton = new resize_baton;
|
||||
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
||||
if (args[1]->IsObject()) {
|
||||
Local<Object> buffer = args[1]->ToObject();
|
||||
baton->buffer_in_len = Buffer::Length(buffer);
|
||||
baton->buffer_in = Buffer::Data(buffer);
|
||||
}
|
||||
baton->file_out = *String::Utf8Value(args[2]->ToString());
|
||||
baton->width = args[3]->Int32Value();
|
||||
baton->height = args[4]->Int32Value();
|
||||
Local<String> canvas = args[5]->ToString();
|
||||
if (canvas->Equals(String::NewSymbol("c"))) {
|
||||
baton->crop = true;
|
||||
baton->crop = true;
|
||||
} else if (canvas->Equals(String::NewSymbol("w"))) {
|
||||
baton->crop = false;
|
||||
baton->embed = 4;
|
||||
baton->extend = VIPS_EXTEND_WHITE;
|
||||
} else if (canvas->Equals(String::NewSymbol("b"))) {
|
||||
baton->crop = false;
|
||||
baton->embed = 0;
|
||||
}
|
||||
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[5]));
|
||||
baton->extend = VIPS_EXTEND_BLACK;
|
||||
}
|
||||
baton->sharpen = args[6]->BooleanValue();
|
||||
baton->progessive = args[7]->BooleanValue();
|
||||
baton->access_method = args[8]->BooleanValue() ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
|
||||
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[9]));
|
||||
|
||||
uv_work_t *work = new uv_work_t;
|
||||
work->data = baton;
|
||||
uv_queue_work(uv_default_loop(), work, ResizeAsync, (uv_after_work_cb)ResizeAsyncAfter);
|
||||
return Undefined();
|
||||
uv_queue_work(uv_default_loop(), work, resize_async, (uv_after_work_cb)resize_async_after);
|
||||
return scope.Close(Undefined());
|
||||
}
|
||||
|
||||
Handle<Value> cache(const Arguments& args) {
|
||||
HandleScope scope;
|
||||
|
||||
// Set cache limit
|
||||
if (args[0]->IsInt32()) {
|
||||
vips_cache_set_max_mem(args[0]->Int32Value() * 1048576);
|
||||
}
|
||||
|
||||
// Get cache statistics
|
||||
Local<Object> cache = Object::New();
|
||||
cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576));
|
||||
cache->Set(String::NewSymbol("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576));
|
||||
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
|
||||
return scope.Close(cache);
|
||||
}
|
||||
|
||||
static void at_exit(void* arg) {
|
||||
HandleScope scope;
|
||||
vips_shutdown();
|
||||
}
|
||||
|
||||
extern "C" void init(Handle<Object> target) {
|
||||
HandleScope scope;
|
||||
vips_init("");
|
||||
NODE_SET_METHOD(target, "resize", Resize);
|
||||
};
|
||||
AtExit(at_exit);
|
||||
NODE_SET_METHOD(target, "resize", resize);
|
||||
NODE_SET_METHOD(target, "cache", cache);
|
||||
}
|
||||
|
||||
NODE_MODULE(sharp, init)
|
||||
|
||||
BIN
tests/4.webp
Normal file
BIN
tests/4.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 173 KiB |
BIN
tests/50020484-00001.png
Normal file
BIN
tests/50020484-00001.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 84 KiB |
BIN
tests/G31D.TIF
Normal file
BIN
tests/G31D.TIF
Normal file
Binary file not shown.
31
tests/parallel.js
Executable file
31
tests/parallel.js
Executable file
@@ -0,0 +1,31 @@
|
||||
var sharp = require("../index");
|
||||
var fs = require("fs");
|
||||
var assert = require("assert");
|
||||
var async = require("async");
|
||||
|
||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
var width = 720;
|
||||
var height = 480;
|
||||
|
||||
async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
|
||||
var start = new Date().getTime();
|
||||
async.times(parallelism,
|
||||
function(id, callback) {
|
||||
sharp(inputJpg).resize(width, height).toBuffer(function(err, buffer) {
|
||||
buffer = null;
|
||||
callback(err, new Date().getTime() - start);
|
||||
});
|
||||
},
|
||||
function(err, ids) {
|
||||
assert(!err);
|
||||
assert(ids.length === parallelism);
|
||||
var mean = ids.reduce(function(a, b) {
|
||||
return a + b;
|
||||
}) / ids.length;
|
||||
console.log(parallelism + " parallel calls: fastest=" + ids[0] + "ms slowest=" + ids[ids.length - 1] + "ms mean=" + mean + "ms");
|
||||
next();
|
||||
}
|
||||
);
|
||||
}, function() {
|
||||
console.dir(sharp.cache());
|
||||
});
|
||||
460
tests/perf.js
460
tests/perf.js
@@ -1,64 +1,424 @@
|
||||
var sharp = require("../index");
|
||||
var fs = require("fs");
|
||||
var imagemagick = require("imagemagick");
|
||||
var gm = require("gm");
|
||||
var epeg = require("epeg");
|
||||
var async = require("async");
|
||||
var assert = require("assert");
|
||||
var Benchmark = require("benchmark");
|
||||
|
||||
var input = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
var output = __dirname + "/output.jpg";
|
||||
var width = 640;
|
||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
var outputJpg = __dirname + "/output.jpg";
|
||||
|
||||
var inputPng = __dirname + "/50020484-00001.png"; // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
||||
var outputPng = __dirname + "/output.png";
|
||||
|
||||
var inputWebp = __dirname + "/4.webp"; // http://www.gstatic.com/webp/gallery/4.webp
|
||||
var outputWebp = __dirname + "/output.webp";
|
||||
|
||||
var inputTiff = __dirname + "/G31D.TIF"; // http://www.fileformat.info/format/tiff/sample/e6c9a6e5253348f4aef6d17b534360ab/index.htm
|
||||
var outputTiff = __dirname + "/output.tiff";
|
||||
|
||||
var width = 720;
|
||||
var height = 480;
|
||||
|
||||
var suite = new Benchmark.Suite;
|
||||
suite.add("imagemagick", {
|
||||
"defer": true,
|
||||
"fn": function(deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: input,
|
||||
dstPath: output,
|
||||
quality: 0.75,
|
||||
width: width,
|
||||
height: height
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
async.series({
|
||||
jpeg: function(callback) {
|
||||
var inputJpgBuffer = fs.readFileSync(inputJpg);
|
||||
(new Benchmark.Suite("jpeg")).add("imagemagick", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: inputJpg,
|
||||
dstPath: outputJpg,
|
||||
quality: 0.8,
|
||||
width: width,
|
||||
height: height
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("gm-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
gm(inputJpg).resize(width, height).quality(80).write(outputJpg, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("gm-file-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
gm(inputJpg).resize(width, height).quality(80).toBuffer(function (err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("epeg-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
var image = new epeg.Image({path: inputJpg});
|
||||
image.downsize(width, height, 80).saveTo(outputJpg);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("gm", {
|
||||
"defer": true,
|
||||
"fn": function(deferred) {
|
||||
gm(input).crop(width, height).write(output, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
}).add("epeg-file-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
var image = new epeg.Image({path: inputJpg});
|
||||
var buffer = image.downsize(width, height, 80).process();
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("epeg", {
|
||||
"defer": true,
|
||||
"fn": function(deferred) {
|
||||
var image = new epeg.Image({path: input});
|
||||
image.downsize(width, height).saveTo(output);
|
||||
deferred.resolve();
|
||||
}
|
||||
}).add("sharp", {
|
||||
"defer": true,
|
||||
"fn": function(deferred) {
|
||||
sharp.crop(input, output, width, height, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}).add("sharp-buffer-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}).on("cycle", function(event) {
|
||||
console.log(String(event.target));
|
||||
}).on("complete", function() {
|
||||
assert(this.filter("fastest").pluck("name") == "sharp");
|
||||
}).run();
|
||||
}).add("sharp-buffer-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpgBuffer).resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(width, height).write(outputJpg, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-sharpen", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(width, height).sharpen().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-progressive", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(width, height).progressive().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-sequentialRead", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(width, height).sequentialRead().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).on("cycle", function(event) {
|
||||
console.log("jpeg " + String(event.target));
|
||||
}).on("complete", function() {
|
||||
callback(null, this.filter("fastest").pluck("name"));
|
||||
}).run();
|
||||
},
|
||||
png: function(callback) {
|
||||
var inputPngBuffer = fs.readFileSync(inputPng);
|
||||
(new Benchmark.Suite("png")).add("imagemagick", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: inputPng,
|
||||
dstPath: outputPng,
|
||||
width: width,
|
||||
height: height
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("gm-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
gm(inputPng).resize(width, height).write(outputPng, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("gm-file-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
gm(inputPng).resize(width, height).quality(80).toBuffer(function (err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-buffer-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPngBuffer).resize(width, height).write(outputPng, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-buffer-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPngBuffer).resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPng).resize(width, height).write(outputPng, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPng).resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-sharpen", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPng).resize(width, height).sharpen().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-progressive", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPng).resize(width, height).progressive().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-sequentialRead", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputPng).sequentialRead().resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).on("cycle", function(event) {
|
||||
console.log(" png " + String(event.target));
|
||||
}).on("complete", function() {
|
||||
callback(null, this.filter("fastest").pluck("name"));
|
||||
}).run();
|
||||
},
|
||||
webp: function(callback) {
|
||||
var inputWebpBuffer = fs.readFileSync(inputWebp);
|
||||
(new Benchmark.Suite("webp")).add("sharp-buffer-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputWebpBuffer).resize(width, height).write(outputWebp, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-buffer-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputWebpBuffer).resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputWebp).resize(width, height).write(outputWebp, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputWebp).resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-sharpen", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputWebp).resize(width, height).sharpen().toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-buffer-sequentialRead", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputWebp).sequentialRead().resize(width, height).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).on("cycle", function(event) {
|
||||
console.log("webp " + String(event.target));
|
||||
}).on("complete", function() {
|
||||
callback(null, this.filter("fastest").pluck("name"));
|
||||
}).run();
|
||||
},
|
||||
tiff: function(callback) {
|
||||
(new Benchmark.Suite("tiff")).add("sharp-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputTiff).resize(width, height).write(outputTiff, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-file-sharpen", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputTiff).resize(width, height).sharpen().write(outputTiff, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("sharp-file-file-sequentialRead", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputTiff).sequentialRead().resize(width, height).write(outputTiff, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).on("cycle", function(event) {
|
||||
console.log("tiff " + String(event.target));
|
||||
}).on("complete", function() {
|
||||
callback(null, this.filter("fastest").pluck("name"));
|
||||
}).run();
|
||||
}
|
||||
}, function(err, results) {
|
||||
assert(!err, err);
|
||||
Object.keys(results).forEach(function(format) {
|
||||
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
|
||||
});
|
||||
console.dir(sharp.cache());
|
||||
});
|
||||
|
||||
75
tests/random.js
Executable file
75
tests/random.js
Executable file
@@ -0,0 +1,75 @@
|
||||
var sharp = require("../index");
|
||||
var fs = require("fs");
|
||||
var imagemagick = require("imagemagick");
|
||||
var gm = require("gm");
|
||||
var epeg = require("epeg");
|
||||
var async = require("async");
|
||||
var assert = require("assert");
|
||||
var Benchmark = require("benchmark");
|
||||
|
||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
var outputJpg = __dirname + "/output.jpg";
|
||||
|
||||
var min = 320;
|
||||
var max = 960;
|
||||
|
||||
var randomDimension = function() {
|
||||
return Math.random() * (max - min) + min;
|
||||
};
|
||||
|
||||
new Benchmark.Suite("random").add("imagemagick", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: inputJpg,
|
||||
dstPath: outputJpg,
|
||||
quality: 0.8,
|
||||
width: randomDimension(),
|
||||
height: randomDimension()
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("gm", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
gm(inputJpg).resize(randomDimension(), randomDimension()).quality(80).toBuffer(function (err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("epeg", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
var image = new epeg.Image({path: inputJpg});
|
||||
var buffer = image.downsize(randomDimension(), randomDimension(), 80).process();
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
}).add("sharp", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(randomDimension(), randomDimension()).toBuffer(function(err, buffer) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).on("cycle", function(event) {
|
||||
console.log(String(event.target));
|
||||
}).on("complete", function() {
|
||||
var winner = this.filter("fastest").pluck("name");
|
||||
assert.strictEqual("sharp", String(winner), "sharp was slower than " + winner);
|
||||
console.dir(sharp.cache());
|
||||
}).run();
|
||||
70
tests/unit.js
Executable file
70
tests/unit.js
Executable file
@@ -0,0 +1,70 @@
|
||||
var sharp = require("../index");
|
||||
var imagemagick = require("imagemagick");
|
||||
var assert = require("assert");
|
||||
var async = require("async");
|
||||
|
||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
var outputJpg = __dirname + "/output.jpg";
|
||||
|
||||
async.series([
|
||||
// Resize with exact crop
|
||||
function(done) {
|
||||
sharp(inputJpg).resize(320, 240).write(outputJpg, function(err) {
|
||||
if (err) throw err;
|
||||
imagemagick.identify(outputJpg, function(err, features) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, features.width);
|
||||
assert.strictEqual(240, features.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
},
|
||||
// Resize to fixed width
|
||||
function(done) {
|
||||
sharp(inputJpg).resize(320).write(outputJpg, function(err) {
|
||||
if (err) throw err;
|
||||
imagemagick.identify(outputJpg, function(err, features) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, features.width);
|
||||
assert.strictEqual(261, features.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
},
|
||||
// Resize to fixed height
|
||||
function(done) {
|
||||
sharp(inputJpg).resize(null, 320).write(outputJpg, function(err) {
|
||||
if (err) throw err;
|
||||
imagemagick.identify(outputJpg, function(err, features) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(391, features.width);
|
||||
assert.strictEqual(320, features.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
},
|
||||
// Identity transform
|
||||
function(done) {
|
||||
sharp(inputJpg).write(outputJpg, function(err) {
|
||||
if (err) throw err;
|
||||
imagemagick.identify(outputJpg, function(err, features) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(2725, features.width);
|
||||
assert.strictEqual(2225, features.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
},
|
||||
// Upscale
|
||||
function(done) {
|
||||
sharp(inputJpg).resize(3000).write(outputJpg, function(err) {
|
||||
if (err) throw err;
|
||||
imagemagick.identify(outputJpg, function(err, features) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(3000, features.width);
|
||||
assert.strictEqual(2449, features.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
]);
|
||||
Reference in New Issue
Block a user