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

jimp

Package Overview
Dependencies
Maintainers
1
Versions
281
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jimp - npm Package Compare versions

Comparing version 0.2.10 to 0.2.11

132

index.js

@@ -30,2 +30,5 @@ var FS = require("fs");

// no operation
function noop(){};
// error checking methods

@@ -46,19 +49,2 @@

// MIME type methods
function getMIMEFromBuffer(buffer) {
if (FileType(buffer)) return FileType(buffer).mime;
else return "";
}
function getMIMEFromPath(path, cb) {
ReadChunk(path, 0, 262, function (err, buffer) {
if (err) { cb(null, ""); }
var fileType = FileType(buffer);
return cb && cb(null, fileType && fileType.mime || "");
});
}
//=> {ext: 'png', mime: 'image/png'}
/**

@@ -91,2 +77,3 @@ * Jimp constructor (from a file)

if ("number" == typeof arguments[0] && "number" == typeof arguments[1]) {
// create a new image
var w = arguments[0];

@@ -101,3 +88,3 @@ var h = arguments[1];

if ("undefined" == typeof cb) cb = function () {};
if ("undefined" == typeof cb) cb = noop;
if ("function" != typeof cb)

@@ -118,6 +105,7 @@ return throwError.call(this, "cb must be a function", cb);

} else if ("object" == typeof arguments[0] && arguments[0].constructor == Jimp) {
// clone an existing Jimp
var original = arguments[0];
var cb = arguments[1];
if ("undefined" == typeof cb) cb = function () {};
if ("undefined" == typeof cb) cb = noop;
if ("function" != typeof cb)

@@ -146,5 +134,7 @@ return throwError.call(this, "cb must be a function", cb);

} else if ("string" == typeof arguments[0]) {
// read from a path
var path = arguments[0];
var cb = arguments[1];
if ("undefined" == typeof cb) cb = noop;
if ("function" != typeof cb)

@@ -161,2 +151,3 @@ return throwError.call(this, "cb must be a function", cb);

} else if ("object" == typeof arguments[0]) {
// read from a buffer
var data = arguments[0];

@@ -179,2 +170,72 @@ var mime = getMIMEFromBuffer(data);

/**
* Read an image from a file or a Buffer
* @param src the path to the file or a Buffer containing the file data
* @param cb (optional) a callback function when the file is read
* @retuns a promise
*/
Jimp.read = function(src, cb) {
var promise = new Promise(
function(resolve, reject) {
cb = cb || function(err, image) {
if (err) reject(err);
else resolve(image);
}
if ("string" != typeof src && ("object" != typeof src || Buffer != src.constructor))
return throwError.call(this, "src must be a string or a Buffer", cb);
var img = new Jimp(src, cb);
}
);
return promise;
}
// MIME type methods
function getMIMEFromBuffer(buffer) {
if (FileType(buffer)) return FileType(buffer).mime;
else return "";
}
// gets a MIME type of a file from the path to it
function getMIMEFromPath(path, cb) {
ReadChunk(path, 0, 262, function (err, buffer) {
if (err) { cb(null, ""); }
var fileType = FileType(buffer);
return cb && cb(null, fileType && fileType.mime || "");
});
}
//=> {ext: 'png', mime: 'image/png'}
// parses a bitmap from the constructor to the JIMP bitmap property
function parseBitmap(data, mime, cb) {
var that = this;
switch (mime.toLowerCase()) {
case Jimp.MIME_PNG:
var png = new PNG();
png.parse(data, function(err, data) {
if (err) return throwError.call(that, err, cb);
that.bitmap = {
data: new Buffer(data.data),
width: data.width,
height: data.height
};
return cb.call(that, null, that);
});
break;
case Jimp.MIME_JPEG:
this.bitmap = JPEG.decode(data);
return cb.call(this, null, this);
case Jimp.MIME_BMP:
this.bitmap = BMP.decode(data);
return cb.call(this, null, this);
default:
return throwError.call(this, "Unsupported MIME type: " + mime, cb);
}
}
// used to auto resizing etc.

@@ -255,33 +316,2 @@ Jimp.AUTO = -1;

// parses a bitmap from the constructor to the JIMP bitmap property
function parseBitmap(data, mime, cb) {
var that = this;
switch (mime.toLowerCase()) {
case Jimp.MIME_PNG:
var png = new PNG();
png.parse(data, function(err, data) {
if (err) return throwError.call(that, err, cb);
that.bitmap = {
data: new Buffer(data.data),
width: data.width,
height: data.height
};
return cb.call(that, null, that);
});
break;
case Jimp.MIME_JPEG:
this.bitmap = JPEG.decode(data);
return cb.call(this, null, this);
case Jimp.MIME_BMP:
this.bitmap = BMP.decode(data);
return cb.call(this, null, this);
default:
return throwError.call(this, "Unsupported MIME type: " + mime, cb);
}
}
// An object representing a bitmap in memory, comprising:

@@ -288,0 +318,0 @@ // - data: a buffer of the bitmap data

{
"name": "jimp",
"version": "0.2.10",
"version": "0.2.11",
"description": "An image processing library written entirely in JavaScript (i.e. zero external or native dependencies).",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -13,73 +13,85 @@ # Jimp #

// open a file called "lenna.png"
var lenna = new Jimp("lenna.png", function (err, image) {
this.resize(512, 512) // resize
.write("lenna-small.png") // save
.quality(60) // set JPEG quality
.write("lenna-small.jpg") // save as JPEG
.greyscale() // set greyscale
.write("lena-small-bw.png") // save again
.crop(128, 192, 256, 128) // crop
.write("lena-small-bw-cropped.png"); // save again
Jimp.read("lenna.png", function (err, lenna) {
if (err) throw err;
lenna.resize(512, 512) // resize
.quality(60) // set JPEG quality
.greyscale() // set greyscale
.write("lena-small-bw.jpg"); // save
});
```
## Basic usage ##
Using promises:
The Jimp constructor takes two arguments, the path to a PNG, JPEG or BMP image and a Node-style callback:
```js
var image = new Jimp("./path/to/image.jpg", function (err, image) {
// this is the image
Jimp.read("lenna.png").then(function (lenna) {
lenna.resize(512, 512) // resize
.quality(60) // set JPEG quality
.greyscale() // set greyscale
.write("lena-small-bw.jpg") // save
}).catch(function (err) {
console.error(err);
});
```
Once the callback has fired, the following methods can be called on the image:
## Basic usage ##
The static `Jimp.read` method takes the path to a PNG, JPEG or BMP file and (optionally) a Node-style callback and returns a Promise:
```js
image.crop( x, y, w, h ); // crop to the given region
image.invert(); // invert the image colours
image.flip( horz, vert); // flip the image horizontally or vertically
image.gaussian( r ); // Gaussian blur the image by r pixels (VERY slow)
image.blur( r ); // fast blur the image by r pixels
image.greyscale(); // remove colour from the image
image.sepia(); // apply a sepia wash to the image
image.opacity( f ); // multiply the alpha channel by each pixel by the factor f, 0 - 1
image.resize( w, h ); // resize the image. Jimp.AUTO can be passed as one of the values.
image.scale( f ); // scale the image by the factor f
image.rotate( deg[, resize] ); // rotate the image clockwise by a number of degrees. Unless `false` is passed as the second parameter, the image width and height will be resized appropriately.
image.blit( src, x, y ); // blit the image with another Jimp image at x, y
image.composite( src, x, y ); // composites another Jimp image over this iamge at x, y
image.brightness( val ); // adjust the brighness by a value -1 to +1
image.contrast( val ); // adjust the contrast by a value -1 to +1
image.posterize( n ); // apply a posterization effect with n level
image.mask( src, x, y ); // masks the image with another Jimp image at x, y using average pixel value
image.dither565(); // ordered dithering of the image and reduce color space to 16-bits (RGB565)
image.cover( w, h ); // scale the image so that it fills the given width and height
image.contain( w, h ); // scale the image to the largest size so that fits inside the given width and height
image.background( hex ); // set the default new pixel colour (e.g. 0xFFFFFFFF or 0x00000000) for by some operations (e.g. image.contain and image.rotate) and when writing formats that don't support alpha channels
image.mirror( horz, vert); // an alias for flip
image.fade( f ); // an alternative to opacity, fades the image by a factor 0 - 1. 0 will haven no effect. 1 will turn the image image.opaque(); // set the alpha channel on every pixel to fully opaque
Jimp.read("./path/to/image.jpg", function (err, image) {
// do stuff with the image (if no exception)
});
Jimp.read("./path/to/image.jpg").then(function (image) {
// do stuff with the image
}).catch(function (err) {
// handle an exception
});
```
(Contributions of more methods are welcome!)
The method can also read a PNG, JPEG or BMP buffer:
### Cloning images ###
To clone a Jimp image, you can use:
```js
image.clone(); // returns the clone
Jimp.read(buffer, function (err, image) {
// do stuff with the image (if no exception)
});
```
The Jimp constructor can also be called using an existing image create a clone of that image:
Once the callback is filed or the promise fulfilled, the following methods can be called on the image:
```js
var clone = new Jimp(image, function (err, clone) {
// this is the clone
});
image.crop( x, y, w, h ); // crop to the given region
image.invert(); // invert the image colours
image.flip( horz, vert ); // flip the image horizontally or vertically
image.gaussian( r ); // Gaussian blur the image by r pixels (VERY slow)
image.blur( r ); // fast blur the image by r pixels
image.greyscale(); // remove colour from the image
image.sepia(); // apply a sepia wash to the image
image.opacity( f ); // multiply the alpha channel by each pixel by the factor f, 0 - 1
image.resize( w, h ); // resize the image. Jimp.AUTO can be passed as one of the values.
image.scale( f ); // scale the image by the factor f
image.rotate( deg[, resize] ); // rotate the image clockwise by a number of degrees. Unless `false` is passed as the second parameter, the image width and height will be resized appropriately.
image.blit( src, x, y ); // blit the image with another Jimp image at x, y
image.composite( src, x, y ); // composites another Jimp image over this iamge at x, y
image.brightness( val ); // adjust the brighness by a value -1 to +1
image.contrast( val ); // adjust the contrast by a value -1 to +1
image.posterize( n ); // apply a posterization effect with n level
image.mask( src, x, y ); // masks the image with another Jimp image at x, y using average pixel value
image.dither565(); // ordered dithering of the image and reduce color space to 16-bits (RGB565)
image.cover( w, h ); // scale the image so that it fills the given width and height
image.contain( w, h ); // scale the image to the largest size so that fits inside the given width and height
image.background( hex ); // set the default new pixel colour (e.g. 0xFFFFFFFF or 0x00000000) for by some operations (e.g. image.contain and image.rotate) and when writing formats that don't support alpha channels
image.mirror( horz, vert ); // an alias for flip
image.fade( f ); // an alternative to opacity, fades the image by a factor 0 - 1. 0 will haven no effect. 1 will turn the image
image.opaque(); // set the alpha channel on every pixel to fully opaque
image.clone(); // returns a clone of the image
```
(Contributions of more methods are welcome!)
## Writing to files and buffers ##
### Saving images ###
### Writing to files ###

@@ -92,49 +104,43 @@ The image can be written to disk in PNG, JPEG or BMP format (determined by the file extension) using:

The quality of saved JPEGs can be set with:
### Writing to Buffers ###
```js
image.quality( n ); // set the quality of saved JPEG, 0 - 100
```
A PNG, JPEG or BMP binary Buffer of an image (e.g. for storage in a database) can to got using:
The format of saved PNGs can be set with:
```js
image.rgba( bool ); // set whether PNGs are saved as RGBA (true, default) or RGB (false)
image.filterType( number ); // set the filter type for the saved PNG
image.deflateLevel( number ); // set the deflate level for the saved PNG
image.getBuffer( mime, cb ); // Node-style callback wil be fired with result
```
For convenience, supported filter types are available as static properties:
For convenience, supported MIME types are available as static properties:
```js
Jimp.PNG_FILTER_AUTO; // -1;
Jimp.PNG_FILTER_NONE; // 0;
Jimp.PNG_FILTER_SUB; // 1;
Jimp.PNG_FILTER_UP; // 2;
Jimp.PNG_FILTER_AVERAGE; // 3;
Jimp.PNG_FILTER_PAETH; // 4;
Jimp.MIME_PNG; // "image/png"
Jimp.MIME_JPEG; // "image/jpeg"
Jimp.BMP; // "image/bmp"
```
### Working with Buffers ###
### PNG and JPEG quality ###
A PNG, JPEG or BMP binary Buffer of an image (e.g. for storage in a database) can to got using:
The quality of JPEGs can be set with:
```js
image.getBuffer( mime, cb ); // Node-style callback wil be fired with result
image.quality( n ); // set the quality of saved JPEG, 0 - 100
```
For convenience, supported MIME types are available as static properties:
The format of PNGs can be set with:
```js
Jimp.MIME_PNG; // "image/png"
Jimp.MIME_JPEG; // "image/jpeg"
Jimp.BMP; // "image/bmp"
image.rgba( bool ); // set whether PNGs are saved as RGBA (true, default) or RGB (false)
image.filterType( number ); // set the filter type for the saved PNG
image.deflateLevel( number ); // set the deflate level for the saved PNG
```
The Jimp constructor can also be called passing a valid Buffer as the first argument to the Jimp constructor:
For convenience, supported filter types are available as static properties:
```js
var image = new Jimp(buffer, function (err, image) {
// this is the image
});
Jimp.PNG_FILTER_AUTO; // -1
Jimp.PNG_FILTER_NONE; // 0
Jimp.PNG_FILTER_SUB; // 1
Jimp.PNG_FILTER_UP; // 2
Jimp.PNG_FILTER_AVERAGE; // 3
Jimp.PNG_FILTER_PAETH; // 4
```

@@ -226,4 +232,6 @@

If you want to begin with an empty Jimp image, you can call the Jimp constructor passing the width and height of the image to create:
### Creating new images ###
If you want to begin with an empty Jimp image, you can call the Jimp constructor passing the width and height of the image to create and (optionally) a Node-style callback:
```js

@@ -245,6 +253,6 @@ var image = new Jimp(256, 256, function (err, image) {

All methods can be chained together, for example as follows:
All instance methods can be chained together, for example as follows:
```js
var lenna = new Jimp("lenna.png", function (err, image) {
Jimp.read("lenna.png", function (err, image) {
this.greyscale().scale(0.5).write("lena-half-bw.png");

@@ -257,3 +265,3 @@ });

```js
var lenna = new Jimp("lenna.png", function (err, image) {
Jimp.read("lenna.png", function (err, image) {
image.greyscale(function(err, image) {

@@ -260,0 +268,0 @@ image.scale(0.5, function (err, image) {

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