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

Commit

Permalink
Reformat code and replace Parse.Promise to Native Promise (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
uluru-phatnguyen authored and flovilmart committed Dec 18, 2018
1 parent 646e78b commit a31a9da
Show file tree
Hide file tree
Showing 5 changed files with 1,698 additions and 220 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
sudo: required
node_js:
- '4.1'
- '8'
addons:
apt:
packages:
Expand Down
218 changes: 109 additions & 109 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,139 +1,139 @@
var gm = require('gm').subClass({imageMagick: true});
var gm = require('gm').subClass({ imageMagick: true });

// Require parse if undefined for the promise
if (typeof Parse == "undefined") {
Parse = require("parse").Parse;
if (typeof Parse == 'undefined') {
Parse = require('parse').Parse;
}

module.exports = Image = function(){}
module.exports = Image = function() {}

Image.prototype.setData = function(data, options){
return _setData(this, data, undefined, options);
Image.prototype.setData = function(data, options) {
return _setData(this, data, options);
}

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

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

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

Image.prototype.crop = 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 cropped = self._image.crop(w,h,l,t);
return _wrap(self, cropped, options);
}
Image.prototype.crop = function(options) {
const self = this;
let w = options.width;
let h = options.height;
const l = options.left || 0;
const t = options.top || 0;

Image.prototype.quality = function(quality, options) {
var self = this;
return _wrap(self, self._image.quality(quality), options);
}
const r = options.right || 0;
const b = options.bottom || 0;

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);
}
if (!options.width) {
w = self.width() - r - l;
}

Image.prototype.setFormat = function(format,options){
var self = this;
self._image.setFormat(format.toLowerCase());
return _wrap(self, self._image, options);
}
if (!options.height) {
h = self.height() - b - t;
}

Image.prototype.format = function(options){
var p = new Parse.Promise();
this._image.format(callbackify(p, options));
return p;
const cropped = self._image.crop(w, h, l, t);
return _wrap(self, cropped, options);
}

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")

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

Image.prototype.scale = function(options) {
const 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 _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;
callbackify(p, options)(err, self);
});
return p;
Image.prototype.setFormat = function(format, options) {
const self = this;
self._image.setFormat(format.toLowerCase());
return _wrap(self, self._image, 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);
}
}
Image.prototype.format = function(options) {
const self = this;
return new Promise(
(resolve, reject) => {
self._image.format((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}
);
}

var _wrap = function(self, gm, options){
var p = new Parse.Promise();
gm.toBuffer(_callback(self, p, options));
return p;
Image.prototype.pad = function(options) {
const self = this;
let w = options.width;
let h = options.height;
const l = options.left || 0;
const t = options.top || 0;

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

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

const padded = self._image.out('-background', options.color)
.borderColor(options.color)
.border(l, r)
.extent(w, h)
.out('-flatten');

return _wrap(self, padded, options);
}

var callbackify = function(p, options) {
options = options || {};
return function(err, res) {
if (err) {
options.error && options.error(err);
return p.reject(err);
} else {
options.success && options.success(err);
return p.resolve(res);
var _wrap = (self, gm, options) => {
return new Promise(
(resolve, reject) => {
gm.toBuffer((err, buf) => {
if (err) {
reject(err);
} else {
resolve(_setData(self, buf, options));
}
});
}
};
);
}

var _setData = (self, data, options) => {
return new Promise(
(resolve, reject) => {
self._data = data;
self._image = gm(data);
self._image.size({ bufferStream: true }, (err, size) => {
self._size = size;

if (err) {
reject(err);
} else {
resolve(self);
}
});
}
);
}
Loading

0 comments on commit a31a9da

Please sign in to comment.