Commit 4f9c980a authored by Stefan Scherer's avatar Stefan Scherer

Use jimp to print hostname to photo

parent c0d53b6b
......@@ -2,6 +2,7 @@ FROM chocolateyfest/node
WORKDIR /app
COPY img img
COPY package.json package.json
RUN npm install --production
COPY app.js app.js
FROM chocolateyfest/node:pure
WORKDIR /app
......
......@@ -2,7 +2,9 @@
const fs = require('fs');
const files = fs.readdirSync('img');
const Jimp = require('jimp');
const http = require('http');
const os = require('os');
const port = 8080;
for (let i = files.length - 1; i >= 0; i--) {
......@@ -13,17 +15,25 @@ const pos = Math.floor(Math.random() * files.length);
const file = files[pos];
console.log(`Got ${files.length} img files, choosing position ${pos}.`);
http.createServer((req, res) => {
const path = `img/${file}`;
fs.readFile(path, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end(`Error handling: ${path}\n`);
} else {
res.writeHead(200, { 'Content-Type': 'image/jpeg', 'Connection': 'close' });
res.end(data, 'binary');
}
})
}).listen(port);
const path = `img/${file}`;
console.log(`Listening on port ${port} serving file ${file}.`);
Jimp.read(path).then(image => {
console.log('Got image');
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(black => {
Jimp.loadFont(Jimp.FONT_SANS_32_WHITE).then(white => {
console.log('Got font');
image
.scaleToFit(800, 600)
.print(black, 10, 10, os.hostname())
.print(white, 8, 8, os.hostname())
.quality(98) // set JPEG quality
.getBuffer(Jimp.MIME_JPEG, (err, data) => {
console.log(`Listening on port ${port} serving file ${file}.`);
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': Jimp.MIME_JPEG, 'Connection': 'close' });
res.end(data, 'binary');
}).listen(port);
});
});
});
});
This diff is collapsed.
......@@ -7,5 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Stefan Scherer",
"license": "Apache-2.0"
"license": "Apache-2.0",
"dependencies": {
"jimp": "0.5.3"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment