Created
December 28, 2013 12:19
-
-
Save daxanya2/8158875 to your computer and use it in GitHub Desktop.
無料のサービスを使ってHubotで動的に画像を生成して返す ref: http://qiita.com/daxanya1/items/a352e894491bd396a97d
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
(function() { | |
var fabric, fs, mktemp, d3; | |
d3 = require('d3'); | |
mktemp = require('mktemp'); | |
fabric = require('fabric').fabric; | |
fs = require('fs'); | |
path = require('path'); | |
module.exports = function(robot) { | |
return robot.hear(/rect ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) (\w+)/, function(bot) { | |
var svg, temppath; | |
var width = 750; | |
var height = 750; | |
d3.select("body").node().innerHTML = ''; | |
var svgimage = d3.select("body").append("svg").attr({"width": width, "height": height }); | |
svgimage.append('rect') | |
.attr({ | |
x:bot.match[1], | |
y:bot.match[2], | |
width:bot.match[3], | |
height:bot.match[4], | |
fill:bot.match[5] | |
}); | |
robot.send(bot.envelope, "writing.."); | |
var canvas = fabric.createCanvasForNode(width, height); | |
temppath = path.join(__dirname, '..', 'tmp'); | |
url = process.env.HEROKU_URL | |
if (url === undefined) { | |
url = 'http://localhost:8080' | |
} | |
var svg = d3.select('body').node().innerHTML; | |
console.log(svg); | |
fabric.loadSVGFromString(svg , function(objects, options) { | |
options.top = 0; | |
options.left = 0; | |
for (var i=0; i<objects.length; i++) { | |
objects[i].set ({ angle: 10 }); | |
} | |
var svgGroups = fabric.util.groupSVGElements(objects, options); | |
canvas.add(svgGroups).renderAll(); | |
try { | |
fs.statSync(temppath); | |
} catch (e) { | |
console.log(e); | |
fs.mkdirSync(temppath, 0700); | |
} | |
mktemp.createFile(path.join(temppath,'XXXXXXXX.png'), function(err, filename) { | |
var outStream, stream; | |
stream = canvas.createPNGStream(); | |
outStream = fs.createWriteStream(filename); | |
outStream.on('close', function() { | |
robot.send(bot.envelope, url + "/hubot/tempget.png?id=" + path.basename(filename)); | |
}); | |
stream.on('data', function(chunk) { | |
outStream.write(chunk); | |
}); | |
stream.on('end', function(chunk) { | |
outStream.end(); | |
}); | |
}); | |
}); | |
}); | |
}; | |
}).call(this); |
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
$ npm install --save d3 |
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
$ npm install --save canvas | |
$ npm install --save canvg |
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
heroku config:add BUILDPACK_URL=https://github.com/mojodna/heroku-buildpack-multi.git#build-env |
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
$ npm install --save mktemp |
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
$ npm uninstall --save canvg | |
$ npm install --save fabric |
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
// Generated by CoffeeScript 1.6.3 | |
(function() { | |
var Canvas, canvg, fs, mktemp, d3; | |
d3 = require('d3'); | |
Canvas = require('canvas'); | |
mktemp = require('mktemp'); | |
canvg = require('canvg'); | |
fs = require('fs'); | |
path = require('path'); | |
module.exports = function(robot) { | |
return robot.hear(/svg ([0-9]+) ([0-9]+) ([0-9]+) (\w+)/, function(bot) { | |
var canvas, svg, temppath; | |
var width = 750; | |
var height = 750; | |
d3.select("body").node().innerHTML = ''; | |
var svgimage = d3.select("body").append("svg").attr({"width": width, "height": height }); | |
svgimage.append('circle') | |
.attr({ | |
cx:bot.match[1], | |
cy:bot.match[2], | |
r:bot.match[3], | |
fill:bot.match[4] | |
}); | |
robot.send(bot.envelope, "writing.."); | |
canvas = new Canvas(width, height); | |
temppath = path.join(__dirname, '..', 'tmp'); | |
url = process.env.HEROKU_URL | |
if (url === undefined) { | |
url = 'http://localhost:8080' | |
} | |
var svg = d3.select('body').node().innerHTML; | |
canvg(canvas, svg, { | |
renderCallback: function() { | |
try { | |
fs.statSync(temppath); | |
} catch (e) { | |
console.log(e); | |
fs.mkdirSync(temppath, 0700); | |
} | |
mktemp.createFile(path.join(temppath,'XXXXXXXX.png'), function(err, filename) { | |
var outStream, stream; | |
stream = canvas.createPNGStream(); | |
outStream = fs.createWriteStream(filename); | |
outStream.on('close', function() { | |
robot.send(bot.envelope, url + "/hubot/tempget.png?id=" + path.basename(filename)); | |
}); | |
stream.on('data', function(chunk) { | |
outStream.write(chunk); | |
}); | |
stream.on('end', function(chunk) { | |
outStream.end(); | |
}); | |
}); | |
} | |
}); | |
}); | |
}; | |
}).call(this); |
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
querystring = require('querystring') | |
fs = require('fs') | |
path = require('path') | |
module.exports = (robot) -> | |
robot.router.get "/hubot/tempget.png", (req, res) -> | |
query = querystring.parse(req._parsedUrl.query) | |
tmp = path.join(__dirname, '..', 'tmp',query.id) | |
path.exists(tmp, (exists) -> | |
if(exists) | |
console.log "exist! #{tmp}" | |
fs.readFile tmp,(err,data) -> | |
res.writeHead(200, {'Content-Type': 'image/png'}) | |
res.end(data); | |
else | |
console.log "not! #{tmp}" | |
res.status(404).send('Not found') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment