-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (118 loc) · 3.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const koa = require('koa');
const logger = require('koa-logger');
const route = require('koa-route');
const views = require('co-views');
const serve = require('koa-static');
const _ = require('lodash');
const app = koa();
app.use(logger());
app.listen(2333);
app.use(serve(__dirname +'/src'));
const render = views(__dirname + '/page',{
map: {html:'ejs'}
});
const fs = require('fs');
const jimp = require('jimp');
const dir = __dirname + '/img/';
let imgNums,court = 0,imgRoaded = [];
fs.readdir(dir,(err,files) => {
imgNums = files.length;
console.log('image loading...');
for ( let i = 0; i< imgNums; i++ ){
jimp.read(dir+ files[i]).then(image => {
court++;
imgRoaded.push(image);
if (court === imgNums){
console.log('All image loaded over! And listening 2333')
}
}).catch(err => {console.error(err)})
}
});
const randomNum = (width) => Math.ceil(Math.random()*Number(width) + Number(width)/2);
const numRegx = (num,cb1,cb2) => {
if(/\d[x]\d/.test(num)){
cb1(Number(num.split('x')[0]),Number(num.split('x')[1]));
}else if(/^\d+$/.test(num)){
cb2();
}
};
const imgRandom = () => _.cloneDeep(imgRoaded[Math.ceil(Math.random()*(imgNums-1))]);
function *index(){
this.body = yield render('index',{})
}
function imgResizeCrop(w,h,image,cb){
let rW,rH,x = 0,y = 0;
if (w > h){
rW = w;
rH = jimp.AUTO;
y = (w - h)/2;
}else{
rW = jimp.AUTO;
rH = h;
x = (h - w)/2;
}
image.resize(rW,rH)
.crop(x,y,w,h)
.getBuffer(jimp.MIME_JPEG,(err,buffer) => {
if (err) throw err;
cb(null,buffer);
})
}
function *resetImg(num){
this.type = 'image/jpeg';
this.body = yield cb => {
let image = imgRandom();
numRegx(num,(w,h) => imgResizeCrop(w,h,image,cb)
,() => {
image.resize(Number(num),Number(num))
.getBuffer(jimp.MIME_JPEG,function(err,buffer){
if (err) throw err;
cb(null,buffer);
})
})
};
}
function *resetImgRandom(num){
this.type = 'image/jpeg';
this.body = yield cb => {
let image = imgRandom();
numRegx(num,(w,h) => imgResizeCrop(randomNum(w),randomNum(h),image,cb),
() => imgResizeCrop(randomNum(num),randomNum(num),image,cb)
)
};
}
function drawSolidColorImg(w,h,colorNum,cb){
let fontPath = colorNum > Number('0x'+808080+'FF') ? jimp.FONT_SANS_16_BLACK: jimp.FONT_SANS_16_WHITE;
new jimp(w,h,colorNum,(err,image) => {
jimp.loadFont(fontPath).then(font => {
image.print(font,w/2-28,h/2-8,w+'x'+h,w).getBuffer(jimp.MIME_JPEG,(err,buffer) => {
if (err) throw err;
cb(null,buffer);
})
})
})
}
function *drawImg(color,num){
this.type = 'image/jpeg';
this.body = yield cb => {
let colorNum = Number('0x'+color+'FF');
numRegx(num,(w,h) => drawSolidColorImg(w,h,colorNum,cb),
() => drawSolidColorImg(num-0,num-0,colorNum,cb)
);
};
}
function *drawImgRandom(color,num){
this.type = 'image/jpeg';
this.body = yield cb => {
let colorNum = Number('0x'+color+'FF');
numRegx(num,(w,h) => drawSolidColorImg(randomNum(w),randomNum(h),colorNum,cb),
() => drawSolidColorImg(randomNum(num),randomNum(num),colorNum,cb)
)
};
}
//router
app.use(route.get('/',index));
app.use(route.get('/r@:color/:num',drawImgRandom));
app.use(route.get('/@:color/:num',drawImg));
app.use(route.get('/:num',resetImg));
app.use(route.get('/r/:num',resetImgRandom));