Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Created August 24, 2017 09:26
Show Gist options
  • Save abinavseelan/173ec2157ea629447a21edc4c2d3cda1 to your computer and use it in GitHub Desktop.
Save abinavseelan/173ec2157ea629447a21edc4c2d3cda1 to your computer and use it in GitHub Desktop.
Medium - Image Manipulation using NodeJS - Cropping and center Endpoint
const express = require('express');
const app = express();
const gm = require('gm');
const fs = require('fs');
const port = 1337;
app.get('/ping', (request, response) => {
response.send('Pong!');
});
app.get('/crop/:width/:height', (request, response) => {
const { width, height } = request.params;
gm('/path/to/image/file')
.gravity('Center') // Move the starting point to the center of the image.
.crop(width, height)
.write('./tmp.png', (err) => {
if (err) {
console.log(err);
} else {
response.sendFile('./tmp.png');
}
})
fs.unlinkSync('./tmp.png');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment