-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (64 loc) · 1.83 KB
/
app.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
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const md5 = require('md5')
const fs = require('fs')
const request = require('request')
const im = require('imagemagick')
const port = 4002
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}))
app.use(bodyParser.json({limit: '50mb'}))
app.get('/', (req, res) => res.send('This is image watermarking app'))
app.post('/', (req, res) => { do_watermarking(req,res) })
const do_watermarking = async (req, res) => {
// valid image urls in array
const urls = [
"https://www.gstatic.com/webp/gallery/4.sm.webp"
]
const watermark = 'TEXT HERE'
let fileInputPath, fileOutputPath
urls.map(async url => {
var imgURL = url
var imgKey = md5(imgURL)
fileInputPath = `input_${imgKey}.jpg`
fileOutputPath = `result_${imgKey}.jpg`
await new Promise(resolve =>
request(imgURL)
.pipe(fs.createWriteStream(fileInputPath))
.on('finish', resolve))
await convertImg(watermark, fileInputPath, fileOutputPath).then(d => {
// do something with your output files here
// return 'OK' since imagemagick convert return nothing
res.json('OK')
}).catch(err => {
res.json(err)
})
})
}
const convertImg = (watermark, fileInputPath, fileOutputPath) => {
return new Promise((resolve, reject) => {
var args = [
`${fileInputPath}`,
"-background", "#0008",
"-fill", "white",
"-gravity", "south",
"-size", "100x25",
"-pointsize", "15",
`caption:${watermark}`,
// "+swap",
"-gravity", "southeast",
"-size", "1000x600",
"-composite",
`${fileOutputPath}`
];
try {
im.convert(args, function(err, stdout){
if (err){ reject(err) }
resolve(stdout)
});
} catch (error) {
reject(error)
}
})
}
app.listen(port, () => { console.log(`image-watermark-app listening at ${port}`) })