imagemin / meta

General discussion repo for imagemin related projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

resize and crop

thisconnect opened this issue · comments

commented

Hi first: bigbig thanks to everyone who made imagemin.

Is imagemin only about compression or would you consider adding a imagemin-resize and imagemin-crop plugin?

commented

I am currently using Jimp which works fine, example jimp-dev/jimp#141

But I'd love if resize and crop would be part of the imagemin eco system

imagemin is short for Image Minification. Anything else is out of scope for this project.

But I'd love if resize and crop would be part of the imagemin eco system

Why? Just curious. You already pointed out a tool that works fine.

commented

Hi ok that's what I expected. Thanks for your quick reply. Here some "whys" hope that makes sense.

I am using Jimp + imagemin currently in a private project to scale down PNG's and JPEG's and compress them. imagemin is nice to setup for compression of different types webp, png and jpeg.

One thing about Jimp I do not like is you need to know the MIME type when working with Buffers image.getBuffer( mime, cb ). But that's a minor detail I can live with (currently accessing an internal image._originalMime).

Another minor thing is how Jimp Promises work. Please note: I am not 100% sure about this:
Only Jimp.read returns a Promise and to catch any issues you need to do all manipulations inside the Promise callback else they get not caught.

I.e. you can't do this and catch an error,

Jimp.read(filename)
.then(img => img.grayscale())
.then(img => img.resize())
.then(img => img.write('oups/this/dir/doesnt/exist/filename'))
.catch(err => console.log(err, 'not caugth'))

But you have to do (taked from the readme)

Jimp.read()
.then(function (lenna) {
    lenna.resize(256, 256) // note: no return!
         .quality(60) // quality first??
         .greyscale()
         .write("lena-small-bw.jpg")
})
.catch(function (err) {
    console.error(err); // this will catch 
});

Thirdly it is/will be hard to maintain for one person, if many people use it and issues and PR's explode https://github.com/oliver-moran/jimp/pulls .

At a company where I occasionally work with about 30 Web presences (for intra and internet) and 50+ content authors, they optimize/minify images into 4 types

  • scaled down to different sizes (always width only and height auto)
  • scaled and cropped into 2 different formats (width and height)
  • and one special format kind of negative cropping, filling up with white-space. This is used to bring logo's into a certain harmonized format
  • some images are "minified" on upload and some on request

I would recommend imagemin, but I hesitate currently to recommend Jimp. Hope some if it made sense to read.

The imagemin plugins are pretty agnostic, so someone could easily make a system like imagemin that accepted plugins for both resizing, cropping and minifying (these already being available). Maybe that's something @kevva or @shinnn would be interested in doing. Could indeed be useful to have a good pipeline for all of this.

One thing about Jimp I do not like is you need to know the MIME type when working with Buffers

Checkout file-type. It detects MIME type of Buffers.

Only Jimp.read returns a Promise and to catch any issues you need to do all manipulations inside the Promise callback else they get not caught.

Your first example should work fine, but the second example is IMHO more readable. You don't have to return a promise in the promise callback. You can return anything. Errors will be catched correctly.

But yeah, I agree the jimp API is not very good.

We could add a map option (like this) that'd allow you to map over every file and their buffers to do whatever you want with them (e.g. resizing).

FYI, I made a module for resizing images that has a simpler API than jimp https://github.com/kevva/resize-img.