-
Notifications
You must be signed in to change notification settings - Fork 9
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
Replace sharp with jimp module #191
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,10 @@ module.exports = function(gulp, config) { | |
c.info('~ images') | ||
|
||
var imagemin = require('gulp-imagemin') | ||
var plumber = require('gulp-plumber') | ||
var jimp = require('jimp') | ||
var merge = require('merge-stream') | ||
var sharp = require('sharp') | ||
var path = require('path') | ||
var plumber = require('gulp-plumber') | ||
var stream = require('stream') | ||
|
||
var task = merge() | ||
|
@@ -66,8 +66,8 @@ module.exports = function(gulp, config) { | |
fileClone.path = path.join(name.dir, name.name + '-' + size + name.ext) | ||
fileClone.imageTransform = { | ||
width: size, | ||
height: file.imageData.aspectRatio ? Math.ceil(size/file.imageData.aspectRatio) : null, | ||
options: file.imageData.options || {}, | ||
height: file.imageData.aspectRatio ? Math.ceil(size/file.imageData.aspectRatio) : jimp.AUTO, | ||
quality: file.imageData.quality || 90, | ||
} | ||
this.push(fileClone) | ||
} | ||
|
@@ -85,11 +85,21 @@ module.exports = function(gulp, config) { | |
var transformStream = new stream.Transform({objectMode: true}); | ||
transformStream._transform = function (file, encoding, callback) { | ||
if(file.imageTransform) { | ||
sharp(file.contents).resize(file.imageTransform.width, file.imageTransform.height).withoutEnlargement().jpeg(file.imageTransform.options).toBuffer(function (err, buffer, info) { | ||
if(err) console.error(err) | ||
file.contents = buffer | ||
callback(null, file) | ||
}) | ||
jimp | ||
.read(file.contents) | ||
.then(function(image) { | ||
image | ||
.resize(file.imageTransform.width, file.imageTransform.height) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sharp's resize does crop. But JIMP just resizes it no matter what. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean that it does scale up but sharp doesn't? Or how does the cropping work in sharp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. If both height and width is specified it resized it (keeping aspect ratio) and then cropped it (to specified dimension). JIMP on the other hand resizes it but does not keep aspect ratio, which leads to distortion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I understand. Okay, I'll add the behavior. |
||
.quality(file.imageTransform.quality) | ||
.getBuffer(jimp.AUTO, function(err, buffer) { | ||
if(err) console.error(err) | ||
file.contents = buffer | ||
callback(null, file) | ||
}) | ||
}) | ||
.catch(function(err) { | ||
console.error(err) | ||
}) | ||
} else { | ||
callback(null, file) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe print some warning if user uses options that it's not working anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!