Skip to content
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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ before_install:
fi
- if [ $TRAVIS_OS_NAME == "osx" ]; then
brew update;
brew install vips --with-webp --with-graphicsmagick;
brew install python;
export PATH="/usr/local/opt/python/libexec/bin:$PATH";
export TMPDIR="/tmp/";
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ npm install -g mango-cli

Just a few requirements you already have: latest [Node.js](http://nodejs.org) and [Git](http://git-scm.com) executable in `PATH`.

If you are running __Windows__, there are even some more [special requirements because of node-gyp](https://github.com/TooTallNate/node-gyp).<br>On __OS X__ you can come across a problem with missing vips library. Follow these [instructions](http://sharp.dimens.io/en/stable/install/#mac-os) in sharp module docs.
If you are running __Windows__, there are even some more [special requirements because of node-gyp](https://github.com/TooTallNate/node-gyp).

If you're still having problems with the installation, check out prepared [release packages](https://github.com/manGoweb/mango-cli/releases). Extract them locally and run `npm link` in the `mango-cli` folder.

Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ All options can be overridden in `mango.local.yaml` (or `mango.local.json`) file
* `src` - string (glob) source of images
* `sizes` - array of widths (int)
* `aspectRatio` - aspect ratio of image on output (float = width/height), if undefined or false aspect ratio of image is used
* `options` - [output options](http://sharp.dimens.io/en/stable/api-output/#jpeg) for [sharp](http://sharp.dimens.io/en/stable/) resizing engine
* `quality` - jpeg quality 0-100

---

Expand Down
28 changes: 19 additions & 9 deletions lib/tasks/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 || {},
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

height: file.imageData.aspectRatio ? Math.ceil(size/file.imageData.aspectRatio) : jimp.AUTO,
quality: file.imageData.quality || 90,
}
this.push(fileClone)
}
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
Expand Down
Loading