Commit ea35eee3 authored by Chancellier Pierre-Jean's avatar Chancellier Pierre-Jean
Browse files

Génération d'une image

parent 0edec91b
<?php
header ("Content-type: image/png");
if(isset($_GET['lang'])) {
if(empty($_GET['lang'])) $langdetect = true;
else $lang = $_GET['lang'];
}
require('lib/locales.php');
include('lib/phpqrcode/qrlib.php');
$api_node = 'g1.duniter.fr';
$format_pubkey = '#^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{43,44}$#';
$units = ['quantitative','relative'];
date_default_timezone_set('Europe/Paris');
$today = new DateTime();
$format = 'Y-m-d';
function isDate(&$date, $format){
$champsDate = date_parse_from_format($format, $date);
$date = DateTime::createFromFormat($format, $date);
return checkdate($champsDate['month'], $champsDate['day'], $champsDate['year']);
}
function display_column($img,$icon_file,$icone,$txt1,$txt2,$x,$y,$color){
$fontsize_icon = 15;
$type_space = imagettfbbox($fontsize_icon, 0, $icon_file, $icone);
$icon_width = abs($type_space[4] - $type_space[0]);
$font = 3;
$font_width = imagefontwidth($font);
$txt_width1 = $font_width*strlen($txt1);
$txt_width2 = $font_width*strlen($txt2);
imagettftext($img, $fontsize_icon, 0, $x-$icon_width/4, $y, $color, $icon_file, json_decode($icone));
imagestring($img, $font, $x-$txt_width1/2, $y+$fontsize_icon, $txt1, $color);
imagestring($img, $font, $x-$txt_width2/2, $y+2*$fontsize_icon, $txt2, $color);
}
function display($img){
imagepng($img);
imagedestroy($img);
}
// Vérification du titre, du node et du background
if (!empty($_GET['title'])){$titre = $_GET['title'];}else{$titre = utf8_decode(tr('title_default'));}
$node = (!empty($_GET['node'])) ? $_GET['node'] : 'g1.duniter.org';
$background = (!empty($_GET['background'])) ? $_GET['background'] : 'junes';
$background = 'img/bg_' . $background . '.png';
if (!file_exists($background)){$background='img/bg_junes.png';}
// Création de l'image
$source = imagecreatefrompng($background);
$back = ImageColorAllocate($source,233,236,239); // #e9ecef
$border = ImageColorAllocate($source,52,58,64); // #343a40
$fill = ImageColorAllocate($source,255,193,7); // #FFC107
$noir = imagecolorallocate($source, 0, 0, 0);
$font900 = './lib/webfonts/900/fa-solid-900.ttf';
$font400 = './lib/webfonts/400/fa-regular-400.ttf';
$width = imagesx($source);
$height = imagesy($source);
ImageRectangle($source,0,0,$width-1,$height-1,$noir);
ImageRectangle($source,2,2,$width-3,$height-3,$noir);
// Position où on écrira dans l'image
$x = 200;
$y = 30;
// Police de titre
$font = 5;
// Vérification des dates et calcul du nombre de jours entre la date du jour et la date de fin
if (!empty($_GET['start_date'])){
$start_date = $_GET['start_date'];
$nb_col = 3;
if (!isDate($start_date, $format)){
imagestring($source, 1, $x, $y, utf8_decode(tr('error_date_start_invalid')), $noir);
display($source);
}
if (!empty($_GET['end_date'])){
if ($_GET['end_date'] !=0){
$end_date = $_GET['end_date'];
if (!isDate($end_date, $format)){
imagestring($source, 1, $x, $y, utf8_decode(tr('error_date_end_invalid')), $noir);
display($source);
}elseif ($end_date < $start_date) {
imagestring($source, 1, $x, $y, utf8_decode(tr('error_date_end_invalid')), $noir);
display($source);
}
}
}
}else{
// Si pas de start_date, on prend le mois courant
$start_date = new DateTime('first day of this month');
$end_date = new DateTime('last day of this month');
}
$start_date->setTime(0,0,0);
if (isset($end_date)){
$nb_col = 4;
if ($end_date < $today){
$days_left = 0;
}else{
$dteDiff = $end_date->diff($today);
$days_left = $dteDiff->format('%a');
}
}
// Vérification du format de la pubkey
if (!empty($_GET['pubkey'])){
if (preg_match($format_pubkey, $_GET['pubkey'])){
$pubkey = $_GET['pubkey'];
$contribution_url = 'https://' . $api_node . '/api/#/v1/payment/' . $pubkey . '?amount=10|20|50|100|1000&amp;comment=don&amp;redirect_url=https%3A%252F%252F' . $api_node . '&amp;cancel_url=https%3A%252F%252F' . $api_node;
$display_pubkey = (!empty($_GET['display_pubkey']));
$display_button = (!empty($_GET['display_button']));
$display_graph = (!empty($_GET['display_graph']));
// Génération du QRcode
$display_qrcode = (!empty($_GET['display_qrcode']));
$qrcode_path = 'qrcodes/' . $pubkey . '.png';
if ($display_qrcode) {
// TODO : voir pour intégrer le QRcode avec la lib
// QRcode::png($pubkey, $qrcode_path);
}
}
else {
imagestring($source, 1, $x, $y, utf8_decode(tr('error_pubkey_invalid')), $noir);
display($source);
}
}else{
imagestring($source, 1, $x, $y, utf8_decode(tr('error_pubkey_missing')), $noir);
display($source);
}
// Vérification du format de la valeur cible à atteindre
if (!empty($_GET['target'])){
$target = (int)$_GET['target'];
if (!is_int($target)){
imagestring($source, 1, $x, $y, utf8_decode(tr('error_target_not_int')), $noir);
display($source);
}elseif ($target<=0){
imagestring($source, 1, $x, $y, utf8_decode(tr('error_target_negative')), $noir);
display($source);
}
}else{
imagestring($source, 1, $x, $y, utf8_decode(tr('error_target_missing')), $noir);
display($source);
}
// Récupération des transactions entrantes entre la date de début et la date du jour
$start_timestamp = $start_date->getTimestamp();
$today_timestamp = $today->getTimestamp();
$url_json = 'https://' . $node . '/tx/history/' . $pubkey . '/times/' . $start_timestamp . '/' . $today_timestamp;
$json = @file_get_contents($url_json);
if ($json === false){
imagestring($source, 1, $x, $y, utf8_decode(tr('error_connect_node')), $noir);
display($source);
}
$json = json_decode($json);
$transactions = $json->history->received;
$total = 0;
$donneurs = [];
$tmp_timestamp = $start_timestamp;
$array_final = [];
foreach ($transactions as $transaction){
$donneur = $transaction->issuers[0];
if ($donneur != $pubkey){
if(!in_array($donneur, $donneurs)){
array_push($donneurs, $donneur);
}
$outputs = $transaction->outputs;
foreach ($outputs as $output){
if (strstr($output,$pubkey)){
$timestamp = $transaction->blockstampTime;
$output = explode(':', $output);
$montant_transaction = $output[0]/100;
if ($timestamp != $tmp_timestamp){
array_push($array_final, ['t'=>$tmp_timestamp*1000, 'y'=>(string) $total]);
}
$total += $montant_transaction;
$tmp_timestamp = $timestamp;
}
}
}
}
$donors = count($donneurs);
// Vérification de l'unité
$unit = (!empty($_GET['unit']))
? ((!in_array($_GET['unit'], $units)) ? 'quantitative' : $_GET['unit'])
: 'quantitative';
// Si l'unité est relative
if ($unit == 'relative'){
// On récupère le dernier block qui contient le DU
$url_json = 'https://' . $node . '/blockchain/with/ud';
$json = file_get_contents($url_json);
$json = json_decode($json);
$last_block_with_ud = end($json->result->blocks);
// Puis on récupère le montant du DU pour mettre à jour les données
$url_json = 'https://' . $node . '/blockchain/block/' . $last_block_with_ud;
$json = file_get_contents($url_json);
$json = json_decode($json);
$ud = $json->dividend/100;
$total = round($total/$ud);
}
$percentage = round($total/$target*100);
$percentage_bar = ($percentage>100) ? 100 : $percentage;
// ==================================================================================
// Pour écrire dans l'image
imagestring($source, $font, $x, $y, $titre, $noir);
// On dessine la barre de progression
$barwidth = 450;
$barheight = 25;
$whitespace = 25;
$remplissage = $percentage_bar*$barwidth/100;
ImageFilledRectangle($source,$x+1,$y+$whitespace+1,$x+$barwidth,$y+$whitespace+$barheight,$back);
ImageFilledRectangle($source,$x+1,$y+$whitespace+1,$x+$remplissage,$y+$whitespace+$barheight,$fill);
ImageRectangle($source,$x,$y+$whitespace,$x+$barwidth+1,$y+$whitespace+$barheight+1,$border);
imagestring($source, 3, $x+$remplissage/2, $y+$whitespace+$barheight/2-3, $percentage . '%', $border);
// Colonnes avec icônes Font awesome
$column = $barwidth/$nb_col;
$mid_column = $column/2;
$font = 3;
$font_width = imagefontwidth($font);
$fontsize = 15;
$font900 = './lib/webfonts/900/fa-solid-900.ttf';
$font400 = './lib/webfonts/400/fa-regular-400.ttf';
$icone_pos_y = $y+$whitespace+$barheight+$whitespace+$fontsize;
// Colonne 1
display_column($source,$font900,'"&#xF200;"',$percentage.'%','atteints',$x+$mid_column,$icone_pos_y,$noir);
// Colonne 2
display_column($source,$font400,'"&#xF007;"',$donors,'donateurs',$x+$column+$mid_column,$icone_pos_y,$noir);
// Colonne 3
$txt_target = tr('label_ontotal_short', array('total'=>$target, 'unit'=>(($unit=='relative') ? tr('ud').'G1' : 'G1')));
display_column($source,$font400,'"&#xF3D1;"',$total,$txt_target,$x+2*$column+$mid_column,$icone_pos_y,$noir);
// Colonne 4
if ($nb_col==4){
$txt_restants = 'restants';
display_column($source,$font400,'"&#xF073;"',$days_left,utf8_decode(tr('label_remaining_d')),$x+3*$column+$mid_column,$icone_pos_y,$noir);
}
// On affiche l'image finale
display($source);
?>
\ No newline at end of file
......@@ -17,6 +17,7 @@ $ttr = array(
'label_donators' => 'Donators',
'ud' => 'UD',
'label_ontotal' => 'Over a total of {{total}} {{unit}}',
'label_ontotal_short' => 'Over {{total}} {{unit}}',
'label_remaining_d' => 'days remaining',
'label_contribute' => 'Contribute now !',
'graph_title' => 'Evolution of the funding',
......
......@@ -17,6 +17,7 @@ $ttr = array(
'label_donators' => 'Donantoj',
'ud' => 'UD',
'label_ontotal' => 'Celo: {{total}} {{unit}}',
'label_ontotal_short' => 'Celo: {{total}} {{unit}}',
'label_remaining_d' => 'tagoj restas',
'label_contribute' => 'Kontribuu nun !',
'graph_title' => 'Evoluo de la financado',
......
......@@ -17,6 +17,7 @@ $ttr = array(
'label_donators' => 'Donateurs',
'ud' => 'DU',
'label_ontotal' => 'Sur un total de {{total}} {{unit}}',
'label_ontotal_short' => 'Sur {{total}} {{unit}}',
'label_remaining_d' => 'jours restants',
'label_contribute' => 'Contribuez maintenant !',
'graph_title' => 'Montant récolté',
......
Supports Markdown
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