-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
69 lines (60 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var request = require('request')
, Canvas = require('canvas')
, Image = Canvas.Image
, fs = require('fs')
exports.readImage = function(fn, cb) {
fs.readFile(fn, function(err, data) {
if (err) return cb(err)
var img = new Image()
img.src = data
cb(null, img, data)
})
}
exports.writeImage = function(img, fn, w, h, cb) {
var canvas = new Canvas(w, h)
, ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, w, h)
var out = fs.createWriteStream(fn)
, stream = canvas.createPNGStream()
stream.on('data', out.write.bind(out))
stream.on('end', cb)
}
exports.files = [
{ 'filename': 'touch-icon-iphone.png', size: 57 },
{ 'filename': 'touch-icon-iphone-retina.png', size: 114 },
{ 'filename': 'touch-icon-ipad-retina.png', size: 144 },
{ 'filename': 'tileicon.png', size: 144 },
{ 'filename': 'favicon.png', size: 96 }
]
exports.bufferToIco = function(data, cb) {
var mp = [{
'Content-Disposition': 'form-data; name="imgfile"; filename="favicon.png"',
'Content-Type': 'image/png',
body: data
}, {
'Content-Disposition': 'form-data; name="remoteimgfile"',
body: 'http://'
}, {
'Content-Disposition': 'form-data; name="selected"',
body: 'file'
}]
request({
url: 'http://convertico.com/appleJax.php',
method: 'POST',
multipart: mp,
headers: {
'content-type': 'multipart/form-data'
}
}, function(err, res, body) {
if (err) return cb(new Error(err.message))
if (res.statusCode !== 200) return cb(new Error(res.statusCode + ': ' + body))
try {
body = JSON.parse(body)
} catch(e) {
return cb(new Error(body))
}
if (body.error.length) return cb(new Error(body.error[0]))
request('http://convertico.com' + body.New, cb)
.pipe(fs.createWriteStream('favicon.ico'))
})
}