Created
August 24, 2017 09:26
-
-
Save abinavseelan/173ec2157ea629447a21edc4c2d3cda1 to your computer and use it in GitHub Desktop.
Medium - Image Manipulation using NodeJS - Cropping and center Endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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