Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sharp

Package Overview
Dependencies
Maintainers
1
Versions
156
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sharp - npm Package Compare versions

Comparing version 0.0.9 to 0.1.0

56

index.js

@@ -8,12 +8,58 @@ 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.canvas = {
crop: "c",
embedWhite: "w",
embedBlack: "b"
};
module.exports.resize = function(input, output, width, height, options, callback) {
"use strict";
if (typeof options === 'function') {
callback = options;
options = {};
} else {
options = options || {};
}
if (typeof input === 'string') {
options.inFile = input;
} else if (typeof input ==='object' && input instanceof Buffer) {
options.inBuffer = input;
} else {
callback("Unsupported input " + typeof input);
return;
}
if (!output || output.length === 0) {
callback("Invalid output");
return;
}
var outWidth = Number(width);
if (Number.isNaN(outWidth)) {
callback("Invalid width " + width);
return;
}
var outHeight = Number(height);
if (Number.isNaN(outHeight)) {
callback("Invalid height " + height);
return;
}
var canvas = options.canvas || "c";
if (canvas.length !== 1 || "cwb".indexOf(canvas) === -1) {
callback("Invalid canvas " + canvas);
return;
}
var sharpen = !!options.sharpen;
var progessive = !!options.progessive;
var sequentialRead = !!options.sequentialRead;
sharp.resize(options.inFile, options.inBuffer, output, width, height, canvas, sharpen, progessive, sequentialRead, callback);
};
/* Deprecated v0.0.x methods */
module.exports.crop = function(input, output, width, height, sharpen, callback) {
sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback);
};
module.exports.embedWhite = function(input, output, width, height, callback) {
sharp.resize(input, output, width, height, "w", callback);
sharp.resize(input, output, width, height, {canvas: "w", sharpen: true}, callback);
};
module.exports.embedBlack = function(input, output, width, height, callback) {
sharp.resize(input, output, width, height, "b", callback);
sharp.resize(input, output, width, height, {canvas: "b", sharpen: true}, callback);
};

2

package.json
{
"name": "sharp",
"version": "0.0.9",
"version": "0.1.0",
"author": "Lovell Fuller",

@@ -5,0 +5,0 @@ "description": "High performance module to resize JPEG and PNG images using the libvips image processing library",

@@ -1,2 +0,2 @@

# sharp
# sharp

@@ -12,18 +12,13 @@ _adj_

It is somewhat opinionated in that it only deals with JPEG and PNG 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 John Cupitt.
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.
Performance is up to 18x faster than ImageMagick and up to 8x faster than GraphicsMagick, based mainly on the number of CPU cores available.
Performance is 12x-15x faster than ImageMagick and 4x-6x faster than GraphicsMagick, based mainly on the number of CPU cores available.
## Prerequisites
* Node.js v0.8+
* node-gyp
* [libvips](https://github.com/jcupitt/libvips) v7.37+
* [libvips](https://github.com/jcupitt/libvips) v7.38+
For the sharpest results, please compile libvips from source.
If you prefer to run a stable, package-managed environment such as Ubuntu 12.04 LTS, [v0.0.3](https://github.com/lovell/sharp/tree/v0.0.3) will work with the libvips-dev package.
## Install

@@ -37,8 +32,27 @@

### crop(input, output, width, height, callback)
### resize(input, output, width, height, [options], callback)
Scale and crop to `width` x `height` calling `callback` when complete.
`input` can either be a filename String or a Buffer. When using a filename libvips will `mmap` the file for improved performance.
`output` can either be a filename String or one of `sharp.buffer.jpeg` or `sharp.buffer.png` to pass a Buffer containing image data to `callback`.
`width` is the Number of pixels wide the resultant image should be.
`height` is the Number of pixels high the resultant image should be.
`options` is optional, and can contain one or more of:
* `canvas` can be one of `sharp.canvas.crop`, `sharp.canvas.embedWhite` or `sharp.canvas.embedBlack`. Defaults to `sharp.canvas.crop`.
* `sharpen` when set to true will perform a mild sharpen of the resultant image. This typically reduces performance by 30%.
* `progressive` when set will use progressive (interlace) scan for the output. This typically reduces performance by 30%.
* `sequentialRead` is an advanced setting that, when set, switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`. This will reduce memory usage and can improve performance on some systems.
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data when a Buffer is requested.
### Examples
```javascript
sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) {
if (err) {

@@ -53,7 +67,7 @@ throw err;

```javascript
sharp.crop("input.jpg", sharp.buffer.jpeg, 300, 200, function(err, buffer) {
sharp.resize("input.jpg", sharp.buffer.jpeg, 300, 200, {progressive: true}, function(err, buffer) {
if (err) {
throw err;
}
// buffer contains JPEG image data
// buffer contains progressive JPEG image data
});

@@ -63,21 +77,17 @@ ```

```javascript
sharp.crop("input.jpg", sharp.buffer.png, 300, 200, function(err, buffer) {
sharp.resize("input.jpg", sharp.buffer.png, 300, 200, {sharpen: true}, function(err, buffer) {
if (err) {
throw err;
}
// buffer contains PNG image data (converted from JPEG)
// buffer contains sharpened PNG image data (converted from JPEG)
});
```
### embedWhite(input, output, width, height, callback)
Scale and embed to `width` x `height` using a white canvas calling `callback` when complete.
```javascript
sharp.embedWhite("input.jpg", "output.jpg", 200, 300, function(err) {
sharp.resize(buffer, "output.jpg", 200, 300, {canvas: sharp.canvas.embedWhite}, function(err) {
if (err) {
throw err;
}
// output.jpg is a 200 pixels wide and 300 pixels high image
// containing a scaled version of input.png embedded on a white canvas
// output.jpg is a 200 pixels wide and 300 pixels high image containing a scaled version
// of the image data contained in buffer embedded on a white canvas
});

@@ -87,20 +97,7 @@ ```

```javascript
sharp.embedWhite("input.jpg", sharp.buffer.jpeg, 200, 300, function(err, buffer) {
sharp.resize("input.jpg", sharp.buffer.jpeg, 200, 300, {canvas: sharp.canvas.embedBlack}, function(err, buffer) {
if (err) {
throw err;
}
// buffer contains JPEG image data
});
```
### embedBlack(input, output, width, height, callback)
Scale and embed to `width` x `height` using a black canvas calling `callback` when complete.
```javascript
sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
if (err) {
throw err;
}
// output.png is a 200 pixels wide and 300 pixels high image
// buffer contains JPEG image data of a 200 pixels wide and 300 pixels high image
// containing a scaled version of input.png embedded on a black canvas

@@ -110,15 +107,2 @@ });

### Parameters common to all methods
#### input
String containing the filename to read from.
#### output
One of:
* String containing the filename to write to.
* `sharp.buffer.jpeg` to pass a Buffer containing JPEG image data to `callback`.
* `sharp.buffer.png` to pass a Buffer containing PNG image data to `callback`.
## Testing

@@ -138,17 +122,38 @@

#### JPEG
`-file-buffer` indicates read from file and write to buffer, `-buffer-file` indicates read from buffer and write to file etc.
* imagemagick x 5.53 ops/sec ±0.55% (31 runs sampled)
* gm x 10.86 ops/sec ±0.43% (56 runs sampled)
* epeg x 28.07 ops/sec ±0.07% (70 runs sampled)
* sharp-file x 72.01 ops/sec ±7.19% (74 runs sampled)
* sharp-buffer x 75.73 ops/sec ±0.44% (75 runs sampled)
`-sharpen`, `-progressive` etc. demonstrate the negative effect of options on performance.
#### PNG
### JPEG
* imagemagick x 4.65 ops/sec ±0.37% (27 runs sampled)
* gm x 21.65 ops/sec ±0.18% (56 runs sampled)
* sharp-file x 43.80 ops/sec ±6.81% (75 runs sampled)
* sharp-buffer x 45.67 ops/sec ±0.41% (75 runs sampled)
* imagemagick x 5.50 ops/sec ±0.48% (31 runs sampled)
* gm-file-file x 11.19 ops/sec ±0.51% (57 runs sampled)
* gm-file-buffer x 11.11 ops/sec ±0.42% (57 runs sampled)
* epeg-file-file x 28.59 ops/sec ±0.09% (71 runs sampled)
* epeg-file-buffer x 28.67 ops/sec ±0.14% (71 runs sampled)
* sharp-buffer-file x 24.72 ops/sec ±0.42% (62 runs sampled)
* sharp-buffer-buffer x 24.24 ops/sec ±0.36% (61 runs sampled)
* sharp-file-file x 97.15 ops/sec ±0.44% (80 runs sampled)
* sharp-file-buffer x __98.51 ops/sec__ ±0.42% (80 runs sampled)
* sharp-file-buffer-sharpen x 56.99 ops/sec ±5.43% (57 runs sampled)
* sharp-file-buffer-progressive x 64.89 ops/sec ±0.42% (79 runs sampled)
* sharp-file-buffer-sequentialRead x 64.13 ops/sec ±0.40% (78 runs sampled)
### PNG
* imagemagick x 4.31 ops/sec ±0.27% (26 runs sampled)
* gm-file-file x 17.89 ops/sec ±0.21% (86 runs sampled)
* gm-file-buffer x 14.74 ops/sec ±0.15% (73 runs sampled)
* sharp-buffer-file x 4.97 ops/sec ±120.47% (26 runs sampled)
* sharp-buffer-buffer x 13.00 ops/sec ±0.53% (65 runs sampled)
* sharp-file-file x 53.00 ops/sec ±7.15% (88 runs sampled)
* sharp-file-buffer x __55.43 ops/sec__ ±0.65% (89 runs sampled)
* sharp-file-buffer-sharpen x 45.37 ops/sec ±0.38% (74 runs sampled)
* sharp-file-buffer-progressive x 55.49 ops/sec ±0.45% (89 runs sampled)
* sharp-file-buffer-sequentialRead x 32.27 ops/sec ±0.29% (79 runs sampled)
## Licence

@@ -155,0 +160,0 @@

var sharp = require("../index");
var fs = require("fs");
var imagemagick = require("imagemagick");

@@ -20,2 +21,3 @@ var gm = require("gm");

jpeg: function(callback) {
var inputJpgBuffer = fs.readFileSync(inputJpg);
(new Benchmark.Suite("jpeg")).add("imagemagick", {

@@ -38,3 +40,3 @@ defer: true,

}
}).add("gm", {
}).add("gm-file-file", {
defer: true,

@@ -50,5 +52,17 @@ fn: function(deferred) {

}
}).add("epeg", {
}).add("gm-file-buffer", {
defer: true,
fn: function(deferred) {
gm(inputJpg).crop(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});

@@ -58,6 +72,14 @@ image.downsize(width, height, 80).saveTo(outputJpg);

}
}).add("sharp-file", {
}).add("epeg-file-buffer", {
defer: true,
fn: function(deferred) {
sharp.crop(inputJpg, outputJpg, width, height, function(err) {
var image = new epeg.Image({path: inputJpg});
var buffer = image.downsize(width, height, 80).process();
assert.notStrictEqual(null, buffer);
deferred.resolve();
}
}).add("sharp-buffer-file", {
defer: true,
fn: function(deferred) {
sharp.resize(inputJpgBuffer, outputJpg, width, height, function(err) {
if (err) {

@@ -70,6 +92,6 @@ throw err;

}
}).add("sharp-buffer", {
}).add("sharp-buffer-buffer", {
defer: true,
fn: function(deferred) {
sharp.crop(inputJpg, sharp.buffer.jpeg, width, height, function(err, buffer) {
sharp.resize(inputJpgBuffer, sharp.buffer.jpeg, width, height, function(err, buffer) {
if (err) {

@@ -83,2 +105,61 @@ throw err;

}
}).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp.resize(inputJpg, outputJpg, width, height, function(err) {
if (err) {
throw err;
} else {
deferred.resolve();
}
});
}
}).add("sharp-file-buffer", {
defer: true,
fn: function(deferred) {
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, 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.resize(inputJpg, sharp.buffer.jpeg, width, height, {sharpen: true}, 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.resize(inputJpg, sharp.buffer.jpeg, width, height, {progressive: true}, 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.resize(inputJpg, sharp.buffer.jpeg, width, height, {sequentialRead: true}, function(err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);
deferred.resolve();
}
});
}
}).on("cycle", function(event) {

@@ -91,2 +172,3 @@ console.log("jpeg " + String(event.target));

png: function(callback) {
var inputPngBuffer = fs.readFileSync(inputPng);
(new Benchmark.Suite("png")).add("imagemagick", {

@@ -108,3 +190,3 @@ defer: true,

}
}).add("gm", {
}).add("gm-file-file", {
defer: true,

@@ -120,9 +202,10 @@ fn: function(deferred) {

}
}).add("sharp-file", {
}).add("gm-file-buffer", {
defer: true,
fn: function(deferred) {
sharp.crop(inputPng, outputPng, width, height, function(err) {
gm(inputPng).crop(width, height).quality(80).toBuffer(function (err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);
deferred.resolve();

@@ -132,9 +215,20 @@ }

}
}).add("sharp-buffer", {
}).add("sharp-buffer-file", {
defer: true,
fn: function(deferred) {
sharp.crop(inputPng, sharp.buffer.png, width, height, function(err, buffer) {
sharp.resize(inputPngBuffer, outputPng, width, height, function(err) {
if (err) {
throw err;
} else {
deferred.resolve();
}
});
}
}).add("sharp-buffer-buffer", {
defer: true,
fn: function(deferred) {
sharp.resize(inputPngBuffer, sharp.buffer.png, width, height, function(err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);

@@ -145,2 +239,61 @@ deferred.resolve();

}
}).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp.resize(inputPng, outputPng, width, height, function(err) {
if (err) {
throw err;
} else {
deferred.resolve();
}
});
}
}).add("sharp-file-buffer", {
defer: true,
fn: function(deferred) {
sharp.resize(inputPng, sharp.buffer.png, width, height, 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.resize(inputPng, sharp.buffer.png, width, height, {sharpen: true}, 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.resize(inputPng, sharp.buffer.png, width, height, {progressive: true}, 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.resize(inputPng, sharp.buffer.png, width, height, {sequentialRead: true}, function(err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);
deferred.resolve();
}
});
}
}).on("cycle", function(event) {

@@ -147,0 +300,0 @@ console.log(" png " + String(event.target));

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc