Unverified Commit fa2fe959 authored by Bennett Piater's avatar Bennett Piater
Browse files

add watermark function

parent d70440ac
......@@ -8,6 +8,11 @@ zip -ru ../schedulor.zip * || true
popd >/dev/null
$wsk -i action update --web true schedulor $(dirname $0)/schedulor.zip --kind nodejs:8
pushd >/dev/null $(dirname $0)/watermark
zip -ru ../watermark.zip * || true
popd >/dev/null
$wsk -i action update watermark $(dirname $0)/watermark.zip --kind nodejs:8
pushd >/dev/null $(dirname $0)/enhance
zip -ru ../enhance.zip * || true
popd >/dev/null
......
const gm = require('gm').subClass({imageMagick: true});
const { Kafka, logLevel } = require('kafkajs')
function main({img}) {
let data = Buffer.from(img, "base64");
// First, watermark the image
return new Promise((resolve, reject) => {
gm(data, "test.png")
.fontSize(20)
.fill("#FF0000")
.drawText(10, 10, "Bennett")
.toBuffer((err, buffer) => {
if (err)
reject({ "error": err });
else
resolve({ "img": buffer.toString("base64") });
});
async function writeToKafka(topic, key, json) {
const kafka = new Kafka({
clientId: 'enhancer',
brokers: ['kafka03-prod02.messagehub.services.eu-gb.bluemix.net:9093', 'kafka02-prod02.messagehub.services.eu-gb.bluemix.net:9093', 'kafka01-prod02.messagehub.services.eu-gb.bluemix.net:9093'],
ssl: true,
sasl: {
mechanism: 'plain',
username: 'urGiSyjzBjRRNAHE',
password: 'rHICukLEqbSb9DIKrYmejTRRkMgDUkZK'
}
});
const producer = kafka.producer()
await producer.connect()
await producer.send({
topic: topic,
messages: [{
key: key,
value: json
}]
})
await producer.disconnect()
}
async function processImage(filename, data) {
const image = Buffer.from(data, "base64");
gm(data, "test.jpg")
.fontSize(20)
.fill("#FF0000")
.drawText(10, 10, "Watermark")
.toBuffer((err, buffer) => {
if (err)
return;
writeToKafka("topic2", filename + ".watermarked.jpg", JSON.stringify({data: buffer.toString("base64")}));
writeToKafka("topic2a", filename + ".watermarked.jpg", JSON.stringify({data: buffer.toString("base64")}));
});
}
function main({messages}) {
messages.forEach(msg => processImage(msg.key, msg.value.data))
}
exports.main = main
{
"name": "schedulor",
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"kafkajs": {
"version": "1.4.4",
"resolved": "https://registry.npmjs.org/kafkajs/-/kafkajs-1.4.4.tgz",
"integrity": "sha512-f+cYW9QSkCsThY5qlzAKTedF9WpyNq+mzGZF/4lx2OJwqFkOHyMaqXgp2ILcIJZ+8O7fo7uqxuH9ODoJqy5BfQ==",
"requires": {
"long": "^4.0.0"
}
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
}
}
}
{
"name": "schedulor",
"main": "index.js",
"dependencies": {
"kafkajs": "*"
}
}
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