This lib looks pretty handy, although it would be nice if we could pipe the image transformations directly to the browser with streams.
Something like this.
app.get("/my-dynamic-image", function (req, res) {
new Jimp("lenna.png", function (err, image) {
this.resize(512, 512); // resize
this.pipe(res); // Send off.
});
});
However I think that the api would be more intuative without forcing a callback into the Jimp constructor, and also making new optional.
app.get("/my-dynamic-image", function (req, res) {
Jimp("lenna.png")
.resize(512, 512)
.pipe(res);
});
Sounds like a good idea.
I'm not sure what would be involved in a pipe method. I expect not an awful lot. I can dig into it and see but it might be a while before I get around to it. If you want to do up a function using the following:
this.getBuffer(Jimp.JPEG, function(err, buffer) {
// buffer is JPEG as a buffer
});
Either fork and do a pull request or just post your code here and I'll add it.
(BTW The constructor has a call back because it needs to read a file. It could do a readFileSync but that would be blocking on a server. I'd prefer if it could be chained easily too.)
This can be done using buffers:
var Jimp = require("jimp");
var express = require("express");
var app = express();
app.get("/my-dynamic-image", function(req, res){
Jimp.read("lenna.png", function(err, lenna) {
lenna.resize(64, 64).quality(60).getBuffer(Jimp.MIME_JPEG, function(err, buffer){
res.set("Content-Type", Jimp.MIME_JPEG);
res.send(buffer);
});
});
});
app.listen(3000);
Can this be made dynamic for all files types like Jpeg, png, bmp? Like, you are using getBuffer(Jimp.MIME_JPEG,function....
Can there be a generic input which can take the type of the input file?
It would be possible to remember the MIME of the original image (if there was one). The difference would be the same as between "Save" and "Save as..." I'm not convinced of its merits, however.
Is there a particular use case where you need this? Otherwise, I'd prefer to keep the output format explicitly stated.
Most helpful comment
This can be done using buffers: