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

png-image

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

png-image - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

test.spec.js

1

CHANGELOG.md
Changelog
=========
v1.0.1 - Update to pngjs v2.3.1 (29/04/16)
v1.0.0 - Initial release (16/12/14)

165

index.js

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

var fs = require('fs'),
'use strict';
const fs = require('fs'),
PNG = require('pngjs').PNG,

@@ -11,3 +13,3 @@ Promise = require('promise');

* @param {object} options
* @param {string} options.imagePath Path to in image file
* @param {object[]} options.imagePath Path to in image file
* @param {string} options.imageOutputPath Path to output image file

@@ -19,2 +21,3 @@ * @param {object} [options.cropImage=null] Cropping for image (default: no cropping)

* @param {int} [options.cropImage.height] Height of cropping region (default: Height that is left)
* @param {int} options.composeOffset Offset of last composed image to clip (default: no offset)
*

@@ -24,14 +27,63 @@ * @property {string} _imagePath

* @property {object} _cropImage
* @property {object} _image
* @property {object} _composeOffset
* @property {PNG[]} _imageQueue
* @property {PNG} _image
*/
function PNGImage(options) {
this._imagePath = options.imagePath;
this._imageOutputPath = options.imageOutputPath;
this._cropImage = options.cropImage;
class PNGImage {
constructor(options) {
this._imagePath = [].concat(options.imagePath);
this._imageOutputPath = options.imageOutputPath;
this._cropImage = options.cropImage;
this._composeOffset = options.composeOffset;
this._image = null;
}
this._imageQueue = [];
this._image = null;
}
PNGImage.prototype = {
/**
* Composes multiple images
*
* @method compose
* @returns {Promise}
*/
compose() {
let composeWidth = 0,
composeHeight = 0,
offsetY = 0;
Promise.resolve().then(() => {
return this._imagePath.reduce((promise, path) => {
return promise.then(() => {
return this._loadImage(path).then(image => {
this._imageQueue.push(image);
});
});
}, Promise.resolve())
}).then(() => {
if (this._composeOffset) {
// Last image requires cropping
this._image = this._imageQueue.pop();
this._imageQueue.push(this._crop({
x: 0,
y: this._composeOffset,
width: this._image.width,
height: (this._image.height - this._composeOffset)
}));
}
// Calculate total canvas size
for (let image of this._imageQueue) {
composeWidth = image.width;
composeHeight += image.height;
}
// Create PNG image
this._image = new PNG({width: composeWidth, height: composeHeight});
// Compose PNG
for (let image of this._imageQueue) {
image.bitblt(this._image, 0, 0, image.width, image.height, 0, offsetY);
offsetY += image.height;
}
return this._writeImage(this._image, this._imageOutputPath);
});
}
/**

@@ -41,7 +93,13 @@ * Runs with a promise

* @method runWithPromise
* @return {Promise}
* @returns {Promise}
*/
runWithPromise: function () {
return Promise.denodeify(this.run).call(this);
},
runWithPromise() {
return this._loadImage(this._imagePath[0]).then(image => {
this._image = image;
if (this._cropImage) {
this._image = this._crop(this._cropImage);
}
return this._writeImage(this._image, this._imageOutputPath);
});
}

@@ -52,19 +110,7 @@ /**

* @method run
* @param {function} fn
*/
run: function (fn) {
this._loadImage(this._imagePath, function (err, image) {
if (err) return fn(err);
run() {
return Promise.nodeify(this.runWithPromise);
}
this._image = image;
if (this._cropImage) {
this._image = this._crop(this._cropImage);
}
this._writeImage(this._imageOutputPath, function () {
fn(undefined);
});
}.bind(this));
},
/**

@@ -82,11 +128,7 @@ * Crops the current image to the specified size

*/
_crop: function (rect) {
_crop(rect) {
let image,
width = Math.min(rect.width, this._image.width - rect.x),
height = Math.min(rect.height, this._image.height - rect.y);
var image,
width,
height;
width = Math.min(rect.width, this._image.width - rect.x);
height = Math.min(rect.height, this._image.height - rect.y);
if ((width < 0) || (height < 0)) {

@@ -96,10 +138,8 @@ throw new Error('Width and height cannot be negative.');

image = new PNG({
width: width,
height: height
});
image = new PNG({ width: width, height: height });
this._image.bitblt(image, rect.x, rect.y, width, height, 0, 0);
return image;
},
}

@@ -111,21 +151,25 @@ /**

* @param {string|buffer} filename
* @param {function} fn Callback
* @returns {promise}
* @private
*/
_loadImage: function (filename, fn) {
_loadImage(filename) {
const image = new PNG();
var image = new PNG();
if (typeof filename === 'string') {
fs.createReadStream(filename).pipe(image).once('error', fn).on('parsed', function () {
fn(undefined, this);
});
return Promise.denodeify(fs.readFile).call(this, filename).then(buffer => {
return new Promise((resolve, reject) => {
return image.parse(buffer, (error, data) => {
if (error) {
reject(error);
}
resolve(data);
})
});
})
} else if (filename instanceof Buffer) {
image.parse(filename, function () {
fn(undefined, image);
});
return Promise.denodeify(image.parse).call(this, filename)
} else {
fn(new Error('Expected a valid read path, stream or buffer.'));
return Promise.reject('Expected a valid image path, stream or buffer.');
}
},
}

@@ -136,14 +180,11 @@ /**

* @method _writeImage
* @param {string} filename Path to file
* @param {function} fn Callback
* @param {string} filePath Path to save file
* @returns {promise}
* @private
*/
_writeImage: function (filename, fn) {
this._image.pack().pipe(fs.createWriteStream(filename))
.once('close', fn)
.once('error', fn);
_writeImage(image, filePath) {
return Promise.denodeify(fs.writeFile).call(this, filePath, PNG.sync.write(image));
}
};
}
module.exports = PNGImage;
{
"name": "png-image",
"version": "1.0.1",
"version": "1.1.0",
"description": "Crop and save PNGs",
"main": "index.js",
"dependencies": {
"pngjs": "^2.3.1",
"pngjs": "^3.0.0",
"promise": "^6.0.1"

@@ -9,0 +9,0 @@ },

@@ -60,2 +60,2 @@ PNG-Image

Copyright 2014 Koola.
Copyright 2016 Koola.

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