Skip to content

Instantly share code, notes, and snippets.

@shrunyan
Last active August 26, 2016 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrunyan/e347f2c3b881643eb649da5aae2123e1 to your computer and use it in GitHub Desktop.
Save shrunyan/e347f2c3b881643eb649da5aae2123e1 to your computer and use it in GitHub Desktop.
Streaming pipeline for requesting, modifying and uploading an image.
'use strict'
var http = require('http')
var request = require('request')
var gm = require('gm')
var fs = require('fs')
var FormData = require('form-data')
let r1 = request('https://upload.wikimedia.org/wikipedia/commons/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg')
let imageMagick = gm.subClass({imageMagick: true})
let i1 = imageMagick(r1)
i1.resize(200)
let s1 = i1.stream()
let i2 = imageMagick(s1)
i2.monochrome()
let s2 = i2.stream()
let form = new FormData()
form.append('modified', 'true')
form.append('file', s2, 'test.jpg')
let r2 = http.request({
method: 'post',
host: 'localhost',
port: 3008,
path: '/upload',
headers: form.getHeaders()
})
r2.on('response', (res) => {
console.log('STATUS', res.statusCode);
})
r2.on('error', (e) => {
console.log('ERROR', e);
})
form.pipe(r2)
@shrunyan
Copy link
Author

Example on how to request a remote image, run it through image magick for modifications and then post a multipart request to another service. Streaming the whole way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment