Verified Commit 5f7c6cb1 authored by Thibaut Broggi's avatar Thibaut Broggi
Browse files

Merge branch 'backend'

parents ad10e108 4effb2cf
Pipeline #1314 passed with stage
in 1 second
node_modules/
config.json
npm-debug.log
\ No newline at end of file
Ce projet est le générateur de slogan et d'image utilisé par LQDN pour la campagne de dons 2017-2018.
\ No newline at end of file
# Slogans generator
This project is the slogan generator used by LQDN for the donation compaign of years 2017-2018.
## Installation
The back-end server require node.js, npm and cairo. Here is the installation command for debian based systems:
```sh
apt install nodejs libcairo2-dev libjpeg62-turbo-dev libpango1.0-dev libgif-dev build-essential g++
```
```sh
cp config.json.default config.json
npm install
```
## Run
```sh
npm run
```
{
"port": "3000"
}
{
"name": "generateur-slogans",
"version": "1.0.0",
"description": "Ce projet est le générateur de slogan et d'image utilisé par LQDN pour la campagne de dons 2017-2018.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "./srv.js"
},
"repository": {
"type": "git",
"url": "gitlab@git.laquadrature.net:lqdn-interne/generateur-slogans.git"
},
"license": "GPL-3.0",
"dependencies": {
"canvas": "2.0.0-alpha.5",
"express": "^4.16.1"
}
}
#!/usr/bin/env node
const fs = require('fs');
const express = require('express');
const Canvas = require('canvas');
const Image = Canvas.Image;
const app = express();
const config = require('./config.json');
app.set('x-powered-by', false);
app.use(express.static('./static'));
// {name: filepath}
let fonts = {
'FiraSans': 'FiraSans-Regular.otf',
'bilbo-inc': 'BilboINC.ttf',
'bluu-next': 'BluuNext-Bold.otf',
'boeticher': 'Boeticher-Roman.otf',
'combat': 'Combat.otf',
'gulax': 'Gulax.otf',
'lineal': 'Lineal.otf',
'resistance': 'Resistance.otf',
'savate': 'savate-regular.otf',
'solid-mirage': 'SolideMirageMono.otf',
'steps-mono': 'Steps-Mono.otf',
'terminal': 'terminal-grotesque_open.otf'
};
for (let i in fonts) {
Canvas.registerFont('./static/fonts/' + fonts[i], {family: i});
}
Canvas.registerFont('./static/fonts/FiraSans-Bold.otf', {family: 'FiraSans', weight: 'bold'});
app.get('/generate.png', function (req, res) {
let bgColor = req.query['bg-color'] || '#f54358';
let fgColor = req.query['fg-color'] || '#ffd201';
let fillText = req.query.text ? req.query.text.trim() : 'démocratie';
let icon = req.query.icon || 1;
let font = req.query.font || 'bluu-next';
let signature = req.query.signature ? req.query.signature.trim() : '';
let width = 600;
let height = 413;
let fontSize = 44;
let canvas = Canvas.createCanvas(width, height);
let ctx = canvas.getContext('2d');
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fill();
// Write text
ctx.font = fontSize + 'pt FiraSans';
ctx.fillStyle = 'white';
ctx.textBaseline = 'top';
ctx.fillText('Internet,', width * 0.08, height * 0.2);
ctx.fillText('et libertés.', width * 0.08, height * 0.2 + fontSize * 2.8);
ctx.font = fontSize + 'pt ' + font;
ctx.fillStyle = fgColor;
ctx.fillText(fillText, width * 0.08, height * 0.2 + fontSize * 1.4);
// Add footer
ctx.fillStyle = 'white';
ctx.fillRect(0, height * 0.85, width, height * 0.15);
ctx.fill();
ctx.font = '12pt FiraSans';
ctx.fillStyle = '#497ed5';
let offset = height * 0.05;
if (signature) {
ctx.fillText('Faites comme ', offset, height * 0.9);
offset += ctx.measureText('Faites comme ').width;
ctx.font = 'bold 12pt FiraSans';
ctx.fillText(signature, offset, height * 0.9);
offset += ctx.measureText(signature).width;
ctx.font = '12pt FiraSans';
ctx.fillText(', s', offset, height * 0.9);
offset += ctx.measureText(', s').width;
} else {
ctx.fillText('S', offset, height * 0.9);
offset+= ctx.measureText('S').width;
}
ctx.fillText('outenez ', offset, height * 0.9);
offset += ctx.measureText('outenez ').width;
ctx.font = 'bold 12pt FiraSans';
ctx.fillText('La Quadrature du Net !', offset, height * 0.9);
// Draw picto
let img = new Image();
let imgXml = fs.readFileSync('./static/imgs/' + icon + '.svg');
imgXml = imgXml.toString('ascii').replace(/#FFD201/gi, fgColor);
img.src = 'data:image/svg+xml;base64,' + new Buffer(imgXml).toString('base64');
ctx.drawImage(img, 0, 0);
// Render image
let stream = canvas.pngStream();
stream.on('data', function(chunk) {
res.write(chunk);
});
stream.on('end', function() {
res.end();
})
});
app.get('/', function(req, res) {
res.sendfile('./static/index.html');
})
app.listen(process.env.PORT || config.port || 80);
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