Manipulating images

Let's add the other image manipulation options we mentioned before. Let's use the Sharp naming for flipping. We'll use the query parameter flip to flip the image vertically, and the query parameter flop to flip it horizontally. We'll also add the optional query parameters blur and sharpen, which should be positive numbers.

Our image download function can be reintegrated directly in our download route, since we only have one route now. In the end, we would end up with something like this:

app.get("/uploads/:image", (req, res) => {
fs.access(req.localpath, fs.constants.R_OK , (err) => {
if (err) return res.status(404).end();

let image = sharp(req.localpath);
let width = +req.query.width;
let height = +req.query.height;
let blur = +req.query.blur;
let sharpen = +req.query.sharpen;
let greyscale = [ "y", "yes", "1",

"on"].includes(req.query.greyscale);
let flip = [ "y", "yes", "1",
"on"].includes(req.query.flip);
let flop = [ "y", "yes", "1",
"on"].includes(req.query.flop);

if (width > 0 && height > 0) {
image.ignoreAspectRatio();
}

if (width > 0 || height > 0) {
image.resize(width || null, height || null);
}

if (flip) image.flip();
if (flop) image.flop();
if (blur > 0) image.blur(blur);
if (sharpen > 0) image.sharpen(sharpen);
if (greyscale) image.greyscale();

res.setHeader("Content-Type", "image/" +
path.extname(req.image).substr(1));

image.pipe(res);
});
});

We should be able to play with it now; flipping and sharpening the image leads to something like this:

Blurring the image leads to something similar to this:

Well, that looks awesome for a first iteration. Let's see what it would look like if we use Hydra instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset