Created
August 24, 2017 09:22
-
-
Save abinavseelan/efe1e8f0248e749a94a2585a670462c1 to your computer and use it in GitHub Desktop.
Medium - Image Manipulation using NodeJS - Cropping 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'); // Require the GraphicsMagick wrapper for node | |
const fs = require('fs'); // To clean up after the we generate the output image file | |
const port = 1337; | |
app.get('/ping', (request, response) => { | |
response.send('Pong!'); | |
}); | |
app.get('/crop/:width/:height', (request, response) => { | |
const { width, height } = request.params; // Get the width and height from the request parameters | |
gm('/path/to/image/file') | |
.crop(width, height) | |
.write('./tmp.png', (err) => { | |
if (err) { | |
console.log(err); | |
} else { | |
response.sendFile('./tmp.png'); | |
} | |
}) | |
fs.unlinkSync('./tmp.png'); // Delete the temporary file that we created in the cropping task | |
}); | |
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