imagemin / meta

General discussion repo for imagemin related projects
6 stars 2 forks source link

resize and crop #11

Closed thisconnect closed 8 years ago

thisconnect commented 8 years ago

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?

thisconnect commented 8 years ago

I am currently using Jimp which works fine, example https://github.com/oliver-moran/jimp/issues/141

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

sindresorhus commented 8 years ago

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.

thisconnect commented 8 years ago

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

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

sindresorhus commented 8 years ago

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.

sindresorhus commented 8 years ago

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.

kevva commented 8 years ago

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.