Skip to content

Replace gm with sharp lib #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"gm": "^1.17.0",
"qs": "^2.2.4",
"request": "^2.44.0",
"serve-static": "^1.6.3"
"serve-static": "^1.6.3",
"sharp": "^0.8.3"
}
}
31 changes: 18 additions & 13 deletions process-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var PromiseA = require('bluebird').Promise
, path = require('path')
, fs = PromiseA.promisifyAll(require('fs'))
, gm = require('gm')
, sharp = require('sharp')
;

function process(conf, url, opts) {
Expand Down Expand Up @@ -51,19 +52,20 @@ function process(conf, url, opts) {
return PromiseA.reject(resp.statusCode);
}

var image = gm(blob)
var image = sharp(blob)
;

image.formatAsync = PromiseA.promisify(image.format);
image.sizeAsync = PromiseA.promisify(image.size);
image.writeAsync = PromiseA.promisify(image.write);
/*image.formatAsync = PromiseA.promisify(image.me);
image.sizeAsync = PromiseA.promisify(image.size);*/
image.writeAsync = PromiseA.promisify(image.toFile);

return image.metadata().then(function(metadata) {

return image.formatAsync().then(function(sourceFormat) {
var realpath
, meta
;

sourceFormat = sourceFormat
var sourceFormat = metadata.format
.toLowerCase()
.replace(/(jpe?g|jf?if)/i, 'jpg')
;
Expand Down Expand Up @@ -147,10 +149,10 @@ function process(conf, url, opts) {
if (data.image) {
image = data.image;
} else {
image = gm(path.resolve(conf.originalsFolder, opts.originalFilename + '.' + data.meta.format));
image.formatAsync = PromiseA.promisify(image.format);
image.sizeAsync = PromiseA.promisify(image.size);
image.writeAsync = PromiseA.promisify(image.write);
image = sharp(path.resolve(conf.originalsFolder, opts.originalFilename + '.' + data.meta.format));
/*image.formatAsync = PromiseA.promisify(image.format);
image.sizeAsync = PromiseA.promisify(image.size);*/
image.writeAsync = PromiseA.promisify(image.toFile);
}

// resize goals:
Expand All @@ -159,7 +161,9 @@ function process(conf, url, opts) {
// if w and h are given, choose the smallest best fit
// if crop and w and h are given, choose the ratio fit (may add letterboxing)
// always resize before crop?
return image.sizeAsync().then(function (size) {
return image.metadata().then(function (metadata) {
var size = {width: metadata.width, height: metadata.height};

var newsize
;

Expand All @@ -171,9 +175,10 @@ function process(conf, url, opts) {
image.resize(newsize.w, newsize.h);
}

if (isNewFormat) {
//The format is inferred from the extension, with JPEG, PNG, WebP and TIFF supported.
/*if (isNewFormat) {
image.setFormat(targetFormat);
}
}*/

if (quality) {
image.quality(quality);
Expand Down