Skip to content

Instantly share code, notes, and snippets.

@bazooka07
Created January 15, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bazooka07/28c404672d2b75858a51d19be0a1b4a5 to your computer and use it in GitHub Desktop.
Save bazooka07/28c404672d2b75858a51d19be0a1b4a5 to your computer and use it in GitHub Desktop.
Gére un avatar depuis une collection de blasons
<!DOCTYPE html>
<?php
// https://forum.pluxml.org/discussion/6935/image-personnalisee-pour-chaque-commentaire
// https://fr.wikipedia.org/wiki/Armorial_des_communes_de_France
// Demo : https://kazimentou.fr/divers/parthenay/
// wget -x -B https://armorial.cc-parthenay-gatine.fr/data/medias/ -i parthenay.lst
const URL_SOURCE = 'https://armorial.cc-parthenay-gatine.fr/';
const MEDIAS_PATH = 'data/medias/';
// const USE_PROXY = true;
function downloadListing() {
$listing = array();
foreach(array('', 'famille-de-la-cour', 'poitiers-1356') as $path) {
$ch = curl_init(URL_SOURCE . $path);
curl_setopt_array($ch, array(
CURLOPT_CRLF => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
));
$resp = curl_exec($ch);
curl_close($ch);
if(!empty($resp) and preg_match_all('@' . MEDIAS_PATH . '(art_\d+/armoiries_[^"]*)@', $resp, $matches)) {
# contrôle de l'unicité des blasons
foreach($matches[1] as $b) {
$k = strtolower(preg_replace('@.*armoiries_?(.*)\.png$@', '$1', $b));
if(!array_key_exists($k, $listing)) {
$listing[$k] = $b;
}
}
}
}
ksort($listing);
return array_values($listing);
}
function getAvatar($email, $nom, $listing) {
$value = trim(!empty($email) ? $email : $name);
if(empty($value)) { return false; }
$index = abs(crc32($value));
$n = $index % count($listing);
return $listing[$n];
}
function avatarName($avatar) {
return strtr(
preg_replace('@^armoiries_?@i', '', basename($avatar, '.png')),
array(
'_d_' => ' d\'',
'_' => ' ',
)
);
}
// $filename = sys_get_temp_dir() . '/parthenay.lst';
$filename = './parthenay.lst';
if(file_exists($filename) and time() - filemtime($filename) < 7200) {
$listing = array_map('trim', file($filename));
} else {
$listing = downloadListing();
file_put_contents($filename, implode(PHP_EOL, $listing));
}
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
if(!empty($name)) {
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$avatar = getAvatar($email, $name, $listing);
}
?>
<html lang="fr">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Armoiries du Poitou et autres</title>
<style>
body {
background-color: #eee;
display: grid;
grid-template-columns: 1fr 22rem;
grid-template-areas: 'header header' 'aside main' 'footer footer';
grid-template-rows: auto 1fr auto;
grid-gap: 0 1rem;
height: 100vh;
margin: 0;
}
header {
text-align: center;
grid-area: header;
}
#aside {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
justify-content: center;
grid-area: aside;
overflow-y: auto;
}
#main {
grid-area: main;
}
footer {
grid-area: footer;
margin: 0;
text-align: center;
}
footer p {
margin: 0;
}
img {
max-width: 100%;
}
figure {
width: 5rem;
margin: 0;
text-align: center;
border: 1px solid;
display: flex;
flex-direction: column;
align-items: center;
padding: 0.25rem;
}
figure img {
max-width: 3rem;
height: auto;
}
figure p {
margin: 0;
font-size: 75%;
}
figcaption {
flex-grow: 1;
}
#main > div {
min-height: 16rem;
text-align: center;
}
#main img {
max-width: 10rem;
}
form > label {
display: grid;
grid-template-columns: 1fr auto;
grid-gap: 1rem 0.5rem;
}
label input {
width: 15rem;
}
form > div {
text-align: center;
margin-top: 1rem;
}
</style>
</head><body>
<header>
<h1><?= count($listing) ?> armoiries à <a href="<?= URL_SOURCE ?>" target="_blank">Parthenay</a></h1>
</header>
<aside id="aside">
<?php
foreach($listing as $blason) {
$title = basename($blason, '.png');
if(defined('USE_PROXY')) {
$imgSize = getimagesize(MEDIAS_PATH . $blason);
$size = $imgSize[3];
}
?>
<figure>
<img src="<?= !defined('USE_PROXY') ? URL_SOURCE : '' ?><?= MEDIAS_PATH . $blason ?>" alt="<?= basename($blason) ?>" <?= !empty($size) ? $size : '' ?> />
<figcaption>
<p><?= avatarName($blason) ?></p>
</figcaption>
<?php
if(!empty($imgSize)) {
?>
<p><?= $imgSize[0] . 'x' . $imgSize[1] ?></p>
<?php
}
?>
</figure>
<?php
}
?>
</aside>
<section id="main">
<div>
<?php
if(!empty($avatar)) {
?>
<img src="<?= !defined('USE_PROXY') ? URL_SOURCE : '' ?><?= MEDIAS_PATH . $avatar ?>" alt="<?= basename($avatar) ?>" />
<p><?= avatarName($avatar) ?></p>
<?php
}
?>
</div>
<form method="post">
<label>
<span>Courriel</span>
<input type="email" name="email" value="<?= !empty($email) ? $email : '' ?>"/>
</label>
<label>
<span>Nom</span>
<input type="test" name="name" value="<?= !empty($name) ? $name : '' ?>" required />
</label>
<div>
<button>Voir mon blason</button>
</div>
</form>
</section>
<footer>
<p>
<span>Designed by <a href="https://kazimentou.fr/repo/" target="_blank">Jean-Pierre Pourrez "bazooka07"</a></span>
<a href="https://github.com/bazooka07" target="_blank">😛</a>
</p>
</footer>
<script>
(function() {
'use strict';
})();
</script>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment