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

Replace sharp with jimp module #191

wants to merge 3 commits into from

Conversation

enzy
Copy link
Contributor

@enzy enzy commented Jan 3, 2018

  • uses https://github.com/oliver-moran/jimp and streams
  • removes libvips dependency
  • simplifies installation process
  • works on Node 9
  • adds more overhead to the resize process (it takes more memory and time to resize images)

Todos

  • Never distort an image and keep original aspect ratio by cropping it
  • Notify about options are no longer available

Opinions? Can you please test it if it works for you?

Thanks ❤️


Fixes #187, closes #190, see #180

- removes libvips dependency
- simplifies installation process
- should work on Node 9
Copy link
Contributor

@VojtaStanek VojtaStanek left a comment

Choose a reason for hiding this comment

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

How big is the performance difference between sharp and jimp?

@@ -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!

.resize(file.imageTransform.width, file.imageTransform.height)
.quality(image.quality)
.getBuffer(jimp.AUTO, function(err, buffer) {
if(err) console.error(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Was so in sharp version as well, but: Shouldn't we pass the error up in the callback? Not just silently dismiss it?

Copy link
Contributor Author

@enzy enzy Jan 3, 2018

Choose a reason for hiding this comment

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

That's the question - is a broken image a fatal fail or just a inconvience?

Copy link
Contributor

Choose a reason for hiding this comment

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

If the image is then missing it's a fail.

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

.then(function(image) {
image
.resize(file.imageTransform.width, file.imageTransform.height)
.quality(image.quality)
Copy link
Contributor

Choose a reason for hiding this comment

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

From what I understand it will fail if undefined (source).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

default is set at 90 few lines above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(but was undefined anyway, thx for the sharp eye!)

@enzy
Copy link
Contributor Author

enzy commented Jan 3, 2018

The speed difference is significant (in favor for sharp), but that is expected:

image

I would still add some caching strategy to the whole image optimization process (like store original hash, settings for that file etc.) and optional storage outside of dist folder - perhaps AWS S3 bucket?

@enzy
Copy link
Contributor Author

enzy commented Jan 23, 2018

Closing this for now. More in the #187

@enzy enzy closed this Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants