Last active
September 10, 2019 09:29
-
-
Save Marak/217eda0471e3e9f54738 to your computer and use it in GitHub Desktop.
hook.io image hook for manipulating images using ImageMagick
This file contains hidden or 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
/* | |
http://hook.io/Marak/image | |
*/ | |
var gm = require('gm'); | |
module['exports'] = function image (hook, callback) { | |
var imageReadStream; | |
hook.debug('Opening read stream to image: ' + hook.params.image); | |
if (hook.streaming) { | |
readStream = hook.req; | |
} | |
else if (hook.params.image && hook.params.image._readableState.buffer.length > 0) { | |
imageReadStream = hook.params.image; | |
} else { | |
imageReadStream = hook.open(hook.params.url); | |
} | |
hook.debug('Opening write stream to grey scale transform'); | |
//var grayScale = hook.post('http://dev.hook.io:9999/Marak/image/greyscale'); | |
hook.debug('Opening write stream to opacity transform'); | |
//var transparency = hook.post('http://dev.hook.io:9999/Marak/image/transparency'); | |
hook.debug('Opening write stream to resize transform'); | |
var resize = hook.post('http://hook.io/Marak/image/resize?width=' + hook.params.width + "&height=" + hook.params.height); | |
hook.debug('Opening write stream to rotate transform'); | |
var rotate = hook.post('http://hook.io/Marak/image/rotate?degrees=' + hook.params.degrees); | |
hook.debug('Writing response headers'); | |
hook.res.writeHead(200, { 'Content-Type': 'image/png' }); | |
hook.debug('Piping source image through transform streams'); | |
imageReadStream | |
.pipe(resize) | |
// .pipe(grayScale) | |
// .pipe(transparency) | |
.pipe(rotate) | |
.pipe(hook.res); | |
}; | |
module['exports'].schema = { | |
"url": { | |
"type": "string", | |
"default": "http://hook.io/img/logo.png" | |
}, | |
"image": { | |
"type": "file" | |
}, | |
"width": { | |
"type": "number", | |
"default": 200, | |
"required": true | |
}, | |
"height": { | |
"type": "number", | |
"default": 200, | |
"required": true | |
}, | |
"degrees": { | |
"type": "number", | |
"default": 180 | |
} | |
}; |
This file contains hidden or 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
module['exports'] = function presentImageHook (opts, cb) { | |
var $ = this.$; | |
cb(null, $.html()); | |
} |
This file contains hidden or 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
<h1>image service</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment