generate favicon

This commit is contained in:
Adrien MALINGREY 2020-10-26 01:59:20 +01:00
parent 0c79b4fd53
commit a93be073aa
2 changed files with 65 additions and 2 deletions

61
favicon.php Normal file
View File

@ -0,0 +1,61 @@
<?php
header ("Content-type: image/png");
const UNKNOWN = ".";
$gridStr = strip_tags($_GET['grid']);
$size = (int) $_GET['size'];
$icon = imagecreate($size, $size);
$transparent = imagecolorallocate($icon, 1, 1, 1);
imagecolortransparent($icon, $transparent);
$gridBorder = imagecolorallocate($icon, 0, 0, 0);
$known = imagecolorallocate($icon, 102, 102, 255);
$unknown = imagecolorallocate($icon, 255, 255, 255);
if ($size == 16) {
ImageLine($icon, 2, 1, 12, 1, $gridBorder);
ImageLine($icon, 2, 5, 12, 5, $gridBorder);
ImageLine($icon, 2, 9, 12, 9, $gridBorder);
ImageLine($icon, 2, 13, 12, 13, $gridBorder);
ImageLine($icon, 1, 2, 1, 12, $gridBorder);
ImageLine($icon, 5, 2, 5, 12, $gridBorder);
ImageLine($icon, 9, 2, 9, 12, $gridBorder);
ImageLine($icon, 13, 2, 13, 12, $gridBorder);
$x = 1;
$y = 0;
foreach(str_split($gridStr) as $i => $value) {
$x++;
if ($i % 3 == 0) $x++;
if ($i % 9 == 0) {
$y++;
$x = 2;
}
if ($i % 27 == 0) $y++;
if ($value == UNKNOWN) $pixelColor = $unknown;
else $pixelColor = $known;
ImageSetPixel($icon, $x, $y, $pixelColor);
}
} else {
$boxSize = floor(($size-5) / 9);
$start = 1;
$end = 9*$boxSize + 2;
for ($y=0; $y < $size; $y += 3*$boxSize + 1)
ImageLine($icon, $start, $y, $end, $y, $gridBorder);
for ($x=0; $x < $size; $x += 3*$boxSize +1)
ImageLine($icon, $x, $start, $x, $end, $gridBorder);
$x = 0;
$y = 0;
$boxSizeMinusOne = $boxSize - 1;
foreach(str_split($gridStr) as $i => $value) {
if ($i % 3 == 0) $x++;
if ($i % 27 == 0) $y++;
if ($value == UNKNOWN) $color = $unknown;
else $color = $known;
imagefilledrectangle($icon, $x, $y, $x+$boxSizeMinusOne, $y+$boxSizeMinusOne, $color);
$x += $boxSize;
if ($i % 9 == 8) {
$y += $boxSize;
$x = 0;
}
}
}
imagepng($icon);
?>

View File

@ -13,7 +13,8 @@
<title>Sudoku</title> <title>Sudoku</title>
<link rel='stylesheet' type='text/css' href='style.css' /> <link rel='stylesheet' type='text/css' href='style.css' />
<script src='app.js'></script> <script src='app.js'></script>
<link rel="icon" type="image/png" href="favicon.png"> <link rel="icon" type="image/png" href="favicon.php?size=16&grid=<?=$gridStr?>" sizes="16x16">
<link rel="icon" type="image/png" href="favicon.php?size=32&grid=<?=$gridStr?>" sizes="32x32">
</head> </head>
<body> <body>
<header> <header>
@ -102,12 +103,13 @@
$urlExample = $urlDir . "/" . $grid->toString(); $urlExample = $urlDir . "/" . $grid->toString();
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang='fr'>
<head> <head>
<meta charset='utf-8' /> <meta charset='utf-8' />
<meta name='viewport' content='width=device-width' /> <meta name='viewport' content='width=device-width' />
<title>Grille incorrecte</title> <title>Grille incorrecte</title>
<link rel='stylesheet' type='text/css' href='style.css' /> <link rel='stylesheet' type='text/css' href='style.css' />
<link rel="icon" type="image/png" href="favicon.png">
</head> </head>
<body> <body>
<header> <header>