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.1.8 to 0.2.0

167

index.js

@@ -1,62 +0,124 @@

var sharp = require("./build/Release/sharp");
/*jslint node: true */
'use strict';
module.exports.buffer = {
jpeg: "__jpeg",
png: "__png",
webp: "__webp"
var sharp = require('./build/Release/sharp');
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.canvas = {
crop: "c",
embedWhite: "w",
embedBlack: "b"
Sharp.prototype.crop = function() {
this.options.canvas = 'c';
return this;
};
module.exports.resize = function(input, output, width, height, options, callback) {
"use strict";
if (typeof options === 'function') {
callback = options;
options = {};
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 {
options = options || {};
if (!Number.isNaN(width)) {
this.options.width = width;
} else {
throw 'Invalid width ' + width;
}
}
if (typeof input === 'string') {
options.inFile = input;
} else if (typeof input ==='object' && input instanceof Buffer) {
options.inBuffer = input;
if (!height) {
this.options.height = -1;
} else {
callback("Unsupported input " + typeof input);
return;
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) {
callback("Invalid output");
return;
throw 'Invalid output';
} else {
this._sharp(output, callback);
}
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;
}
if (outWidth < 1 && outHeight < 1) {
callback("Width and/or height required");
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, outWidth, outHeight, canvas, sharpen, progessive, sequentialRead, 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) {
"use strict";
if (Number.isNaN(limit)) {

@@ -67,12 +129,1 @@ limit = null;

};
/* 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, {canvas: "w", sharpen: true}, callback);
};
module.exports.embedBlack = function(input, output, width, height, callback) {
sharp.resize(input, output, width, height, {canvas: "b", sharpen: true}, callback);
};
{
"name": "sharp",
"version": "0.1.8",
"version": "0.2.0",
"author": "Lovell Fuller",

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

@@ -5,6 +5,6 @@ # sharp

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.

@@ -17,4 +17,6 @@ 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.

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.
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

@@ -25,8 +27,7 @@

### Install libvips on Mac OS via homebrew
### Install libvips on Mac OS
brew tap homebrew/science
brew install vips
brew install homebrew/science/vips
### Install libvips on Ubuntu Linux
### Install libvips on Ubuntu/Debian Linux

@@ -46,31 +47,10 @@ 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

## Usage
## Usage examples
var sharp = require("sharp");
```javascript
var sharp = require('sharp');
```
### 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.
`output` can either be a filename String or one of `sharp.buffer.jpeg`, `sharp.buffer.png` or `sharp.buffer.webp` to pass a Buffer containing JPEG, PNG or WebP image data to `callback`.
`width` is the Number of pixels wide the resultant image should be. Use a value of -1 to auto-scale the width to match the height.
`height` is the Number of pixels high the resultant image should be. Use a value of -1 to auto-scale the height to match the width.
`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.resize("input.jpg", "output.jpg", 300, 200, function(err) {
sharp('input.jpg').resize(300, 200).write('output.jpg', function(err) {
if (err) {

@@ -85,3 +65,3 @@ throw err;

```javascript
sharp.resize("input.jpg", sharp.buffer.jpeg, -1, 200, {progressive: true}, function(err, buffer) {
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, buffer) {
if (err) {

@@ -95,7 +75,7 @@ throw err;

```javascript
sharp.resize("input.webp", sharp.buffer.png, 300, -1, {sharpen: true}, function(err, buffer) {
sharp('input.png').resize(300).sharpen().webp(function(err, buffer) {
if (err) {
throw err;
}
// buffer contains sharpened PNG image data (converted from JPEG), 300 pixels wide
// buffer contains sharpened WebP image data (converted from PNG), 300 pixels wide
});

@@ -105,8 +85,8 @@ ```

```javascript
sharp.resize(buffer, "output.tiff", 200, 300, {canvas: sharp.canvas.embedWhite}, function(err) {
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
// of the image data contained in buffer embedded on a white canvas
// 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
});

@@ -116,3 +96,3 @@ ```

```javascript
sharp.resize("input.jpg", sharp.buffer.webp, 200, 300, {canvas: sharp.canvas.embedBlack}, function(err, buffer) {
sharp('input.jpg').resize(200, 300).embedBlack().webp(function(err, buffer) {
if (err) {

@@ -122,8 +102,78 @@ throw err;

// buffer contains WebP image data of a 200 pixels wide and 300 pixels high image
// containing a scaled version of input.png embedded on a black canvas
// containing a scaled version, embedded on a black canvas, of input.png
});
```
### cache([limit])
## 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.

@@ -130,0 +180,0 @@

@@ -14,3 +14,3 @@ var sharp = require("../index");

function(id, callback) {
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, function(err, buffer) {
sharp(inputJpg).resize(width, height).toBuffer(function(err, buffer) {
buffer = null;

@@ -17,0 +17,0 @@ callback(err, new Date().getTime() - start);

@@ -86,3 +86,3 @@ var sharp = require("../index");

fn: function(deferred) {
sharp.resize(inputJpgBuffer, outputJpg, width, height, function(err) {
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
if (err) {

@@ -98,3 +98,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputJpgBuffer, sharp.buffer.jpeg, width, height, function(err, buffer) {
sharp(inputJpgBuffer).resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -111,3 +111,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputJpg, outputJpg, width, height, function(err) {
sharp(inputJpg).resize(width, height).write(outputJpg, function(err) {
if (err) {

@@ -123,3 +123,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, function(err, buffer) {
sharp(inputJpg).resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -136,3 +136,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, {sharpen: true}, function(err, buffer) {
sharp(inputJpg).resize(width, height).sharpen().toBuffer(function(err, buffer) {
if (err) {

@@ -149,3 +149,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, {progressive: true}, function(err, buffer) {
sharp(inputJpg).resize(width, height).progressive().toBuffer(function(err, buffer) {
if (err) {

@@ -162,3 +162,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, {sequentialRead: true}, function(err, buffer) {
sharp(inputJpg).resize(width, height).sequentialRead().toBuffer(function(err, buffer) {
if (err) {

@@ -222,3 +222,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPngBuffer, outputPng, width, height, function(err) {
sharp(inputPngBuffer).resize(width, height).write(outputPng, function(err) {
if (err) {

@@ -234,3 +234,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPngBuffer, sharp.buffer.png, width, height, function(err, buffer) {
sharp(inputPngBuffer).resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -247,3 +247,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPng, outputPng, width, height, function(err) {
sharp(inputPng).resize(width, height).write(outputPng, function(err) {
if (err) {

@@ -259,3 +259,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPng, sharp.buffer.png, width, height, function(err, buffer) {
sharp(inputPng).resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -272,3 +272,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPng, sharp.buffer.png, width, height, {sharpen: true}, function(err, buffer) {
sharp(inputPng).resize(width, height).sharpen().toBuffer(function(err, buffer) {
if (err) {

@@ -285,3 +285,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPng, sharp.buffer.png, width, height, {progressive: true}, function(err, buffer) {
sharp(inputPng).resize(width, height).progressive().toBuffer(function(err, buffer) {
if (err) {

@@ -298,3 +298,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputPng, sharp.buffer.png, width, height, {sequentialRead: true}, function(err, buffer) {
sharp(inputPng).sequentialRead().resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -319,3 +319,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputWebpBuffer, outputWebp, width, height, function(err) {
sharp(inputWebpBuffer).resize(width, height).write(outputWebp, function(err) {
if (err) {

@@ -331,3 +331,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputWebpBuffer, sharp.buffer.webp, width, height, function(err, buffer) {
sharp(inputWebpBuffer).resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -344,3 +344,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputWebp, outputWebp, width, height, function(err) {
sharp(inputWebp).resize(width, height).write(outputWebp, function(err) {
if (err) {

@@ -356,3 +356,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputWebp, sharp.buffer.webp, width, height, function(err, buffer) {
sharp(inputWebp).resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -369,3 +369,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputWebp, sharp.buffer.webp, width, height, {sharpen: true}, function(err, buffer) {
sharp(inputWebp).resize(width, height).sharpen().toBuffer(function(err, buffer) {
if (err) {

@@ -382,3 +382,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputWebp, sharp.buffer.webp, width, height, {sequentialRead: true}, function(err, buffer) {
sharp(inputWebp).sequentialRead().resize(width, height).toBuffer(function(err, buffer) {
if (err) {

@@ -402,3 +402,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputTiff, outputTiff, width, height, function(err) {
sharp(inputTiff).resize(width, height).write(outputTiff, function(err) {
if (err) {

@@ -414,3 +414,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputTiff, outputTiff, width, height, {sharpen: true}, function(err) {
sharp(inputTiff).resize(width, height).sharpen().write(outputTiff, function(err) {
if (err) {

@@ -426,3 +426,3 @@ throw err;

fn: function(deferred) {
sharp.resize(inputTiff, outputTiff, width, height, {sequentialRead: true}, function(err) {
sharp(inputTiff).sequentialRead().resize(width, height).write(outputTiff, function(err) {
if (err) {

@@ -429,0 +429,0 @@ throw err;

@@ -60,3 +60,3 @@ var sharp = require("../index");

fn: function(deferred) {
sharp.resize(inputJpg, sharp.buffer.jpeg, randomDimension(), randomDimension(), function(err, buffer) {
sharp(inputJpg).resize(randomDimension(), randomDimension()).toBuffer(function(err, buffer) {
if (err) {

@@ -63,0 +63,0 @@ throw err;

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

@@ -8,10 +9,6 @@ var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/

sharp.resize(inputJpg, outputJpg, 320, 240, 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);
sharp.resize(inputJpg, outputJpg, 320, -1, function(err) {
async.series([
// Resize with exact crop
function(done) {
sharp(inputJpg).resize(320, 240).write(outputJpg, function(err) {
if (err) throw err;

@@ -21,16 +18,55 @@ imagemagick.identify(outputJpg, function(err, features) {

assert.strictEqual(320, features.width);
assert.strictEqual(262, features.height);
assert.strictEqual(240, features.height);
done();
});
sharp.resize(inputJpg, outputJpg, -1, 320, function(err) {
});
},
// 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;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
assert.strictEqual(392, features.width);
assert.strictEqual(320, features.height);
});
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();
});
});
}
]);

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