Skip to content
This repository was archived by the owner on Jan 5, 2021. It is now read-only.

Switch from node-gm to sharp #14

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
result.png
/result-*.png

# Webstorm
.idea
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ node_js:
- '4.1'
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- graphicsmagick
- imagemagick
- g++-4.8
env:
CXX=g++-4.8
deploy:
provider: npm
email: [email protected]
Expand Down
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@ parse-image
[![npm version](https://img.shields.io/npm/v/parse-image.svg?style=flat)](https://www.npmjs.com/package/parse-image)


Port of Parse Image for parse-env based on node-gm

Auto install imagemagick and graphics magick with `homebrew` on OS X OR `apt` if available.

If the auto installation fails, you need to manually install imagemagick and graphicsmagick on you system.
Port of Parse Image for parse-env based on [sharp](https://github.com/lovell/sharp)

## Installation

just install it as a dependency:

`$ npm install --save parse-image`

There is a pre-install script that will attepmt to use `brew` or `apt` to install the imagemagick and graphicsmagick.

If the pre-install script fails, the installation continues but the module may not be working. (actually it won't work at all)
OS X, Windows (x64), Linux (x64, ARM) systems do not require the installation of any external runtime dependencies.

## Heroku installation

heroku comes with imagemagick (convert) by default.

You need to manually install [heroku-buildpack-graphicsmagick](https://github.com/mcollina/heroku-buildpack-graphicsmagick)

Follow the instructions in the link and the pre-install warnings should auto-magically disappear
See [Heroku install guide in sharp docs](http://sharp.dimens.io/en/stable/install/#heroku)
157 changes: 86 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var gm = require('gm').subClass({imageMagick: true});
var sharp = require('sharp');
// Require parse if undefined for the promise
if (typeof Parse == "undefined") {
Parse = require("parse").Parse;
Parse = require("parse").Parse;
}

module.exports = Image = function(){}
Expand All @@ -11,95 +11,111 @@ Image.prototype.setData = function(data, options){
}

Image.prototype.width = function(){
return this._size.width;
return this._size.width;
}

Image.prototype.height = function(){
return this._size.height;
return this._size.height;
}

Image.prototype.data = function(){
return Parse.Promise.as(this._data);
return this._image.toBuffer();
}

Image.prototype.crop = function(options){
var self = this;
var w = options.width;
var h = options.height;
var l = options.left || 0;
var self = this;
var w = options.width;
var h = options.height;
var l = options.left || 0;
var t = options.top || 0;

var r = options.right || 0;
var b = options.bottom || 0;
if (!options.width) {
w = self.width()-r-l;
}
if (!options.height) {
h = self.height()-b-t;
}
var cropped = self._image.crop(w,h,l,t);
return _wrap(self, cropped, options);
var r = options.right || 0;
var b = options.bottom || 0;
if (!options.width) {
w = self.width()-r-l;
}
if (!options.height) {
h = self.height()-b-t;
}
var cropped = self._image.extract({
left: parseInt(l),
top: parseInt(t),
width: parseInt(w),
height: parseInt(h)
});
return _wrap(self, cropped, options);
}

Image.prototype.quality = function(quality, options) {
var self = this;
return _wrap(self, self._image.quality(quality), options);
var self = this;
return _wrap(self, self._image.quality(quality), options);
}

Image.prototype.scale = function(options){
var self = this;
if(options.ratio){
options.width = options.ratio*self.width();
options.height = options.ratio*self.height();
}
return _wrap(self, self._image.scale(options.width, options.height),options);
var self = this;
if(options.ratio){
options.width = options.ratio*self.width();
options.height = options.ratio*self.height();
}
var scaledImg = self._image.resize(parseInt(options.width), parseInt(options.height));
return _wrap(self, scaledImg, options);
}

Image.prototype.setFormat = function(format,options){
var self = this;
self._image.setFormat(format.toLowerCase());
return _wrap(self, self._image, options);
var self = this;
self._image.toFormat(format.toLowerCase());
return _wrap(self, self._image, options);
}

Image.prototype.format = function(options){
var p = new Parse.Promise();
this._image.format(callbackify(p, options));
this._image.metadata(function(err, metadata) {
callbackify(p, options)(err, (metadata.format && metadata.format.toUpperCase()));
});
return p;
}

Image.prototype.pad = function(options) {
var self = this;
var w = options.width;
var h = options.height;
var l = options.left || 0;
var t = options.top || 0;

var r = options.right || 0;
var b = options.bottom || 0;

if (!options.width) {
w = self.width()+r+l;
}
if (!options.height) {
h = self.height()+b+t;
}

var padded = self._image.out("-background", options.color)
.borderColor(options.color)
.border(l, r)
.extent(w, h)
.out("-flatten")
var self = this;
var w = options.width;
var h = options.height;
var l = options.left || 0;
var t = options.top || 0;

return _wrap(self, padded, options);
var r = options.right || 0;
var b = options.bottom || 0;

if (w) {
r = options.width - self.width();
}
if (h) {
b = options.height - self.height();
}

var padded = self._image.background(options.color)
.extend({
top: parseInt(t),
bottom: parseInt(b),
left: parseInt(l),
right: parseInt(r)
});

return _wrap(self, padded, options);
}


var _setData = function(self, data, p, options) {
p = p || new Parse.Promise();
self._data = data;
self._image = gm(data);
self._image.size({bufferStream: true}, function(err, size){
self._size = size;
p = p || new Parse.Promise();
self._data = data.path || data;
self._image = sharp(self._data);
self._image.metadata(function(err, metadata){
if (!err && (!metadata || !metadata.width)) {
err = true;
}
self._size = {
width: metadata && metadata.width,
height: metadata && metadata.height
};
callbackify(p, options)(err, self);
});
return p;
Expand All @@ -108,20 +124,20 @@ var _setData = function(self, data, p, options) {
var _callback = function(self, p, options){
options = options || {};

return function(err, buf){
if (err) {
options.error && options.error(err);
p.reject(err);
}else{
_setData(self, buf, p, options);
}
}
return function(err, buf){
if (err) {
options.error && options.error(err);
p.reject(err);
}else{
_setData(self, buf, p, options);
}
}
}

var _wrap = function(self, gm, options){
var p = new Parse.Promise();
gm.toBuffer(_callback(self, p, options));
return p;
var _wrap = function(self, image, options){
var p = new Parse.Promise();
image.toBuffer(_callback(self, p, options));
return p;
}

var callbackify = function(p, options) {
Expand All @@ -136,4 +152,3 @@ var callbackify = function(p, options) {
}
};
}

60 changes: 0 additions & 60 deletions install.sh

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"main": "index.js",
"version": "0.2.0",
"dependencies": {
"gm": "~1.21.1",
"parse": "^1.3.5"
"parse": "^1.3.5",
"sharp": "^0.16.0"
},
"devDependencies": {
"codecov": "^1.0.1",
Expand All @@ -14,7 +14,6 @@
"mocha": "^2.3.4"
},
"scripts": {
"preinstall": "./install.sh",
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec && ./node_modules/.bin/codecov"
},
"repository": {
Expand Down
Binary file modified test/result-pad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/result-scale-200-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/result-scale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ describe('Image', function() {
assert(false, "Should not succeed");
done();
}, function(err){
assert.equal(1, err.code);
assert.equal(1, errHit);
done();
})
Expand Down