Sudoku/thumbnail.png.php
2020-11-02 21:12:43 +01:00

113 lines
4.3 KiB
PHP

<?php
header ("Content-type: image/png");
const UNKNOWN = ".";
$gridStr = strip_tags($_GET['grid']);
$size = (int) $_GET['size'];
$thumbnail = imagecreate($size, $size);
$transparent = imagecolorallocate($thumbnail, 1, 1, 1);
imagecolortransparent($thumbnail, $transparent);
$black = imagecolorallocate($thumbnail, 0, 0, 0);
$grey = imagecolorallocate($thumbnail, 128, 128, 128);
$blue = imagecolorallocate($thumbnail, 102, 102, 255);
$white = imagecolorallocate($thumbnail, 255, 255, 255);
if ($size <= 36) {
$boxSize = floor(($size-4) / 9);
$gridSize = 9*$boxSize + 4;
$start = floor(($size-$gridSize) / 2);
$end = $start + $gridSize;
$lineStart = $start + 1;
$lineEnd = $end - 2;
for ($i = $start; $i < $end; $i += 3*$boxSize + 1) {
ImageLine($thumbnail, $lineStart, $i, $lineEnd, $i, $black);
ImageLine($thumbnail, $i, $lineStart, $i, $lineEnd, $black);
}
$x = $start;
$y = $start;
$boxSizeMinusOne = $boxSize - 1;
foreach(str_split($gridStr) as $i => $value) {
if ($i % 3 == 0) $x++;
if ($i % 27 == 0) $y++;
if ($value == UNKNOWN) {
$bgColor = $white;
} else {
$bgColor = $blue;
}
imagefilledrectangle($thumbnail, $x, $y, $x+$boxSizeMinusOne, $y+$boxSizeMinusOne, $bgColor);
$x += $boxSize;
if ($i % 9 == 8) {
$y += $boxSize;
$x = $start;
}
}
} else if ($size < 82) {
$boxSize = floor(($size-1) / 9);
$gridSize = 9*$boxSize + 1;
$start = floor(($size-$gridSize) / 2);
$end = $start + $gridSize;
$lineStart = $start + 1;
$lineEnd = $end - 2;
for ($i = $start + $boxSize; $i < $end - $boxSize; $i += $boxSize) {
ImageLine($thumbnail, $lineStart, $i, $lineEnd, $i, $grey);
ImageLine($thumbnail, $i, $lineStart, $i, $lineEnd, $grey);
}
for ($i = $start; $i < $end; $i += 3*$boxSize) {
ImageLine($thumbnail, $lineStart, $i, $lineEnd, $i, $black);
ImageLine($thumbnail, $i, $lineStart, $i, $lineEnd, $black);
}
$x = $start + 1;
$y = $start + 1;
$boxSizeMinusTwo = $boxSize - 2;
foreach(str_split($gridStr) as $i => $value) {
if ($value == UNKNOWN) {
$bgColor = $white;
} else {
$bgColor = $blue;
}
imagefilledrectangle($thumbnail, $x, $y, $x+$boxSizeMinusTwo, $y+$boxSizeMinusTwo, $bgColor);
$x += $boxSize;
if ($i % 9 == 8) {
$y += $boxSize;
$x = $start + 1;
}
}
} else {
$boxSize = floor(($size-1) / 9);
$gridSize = 9*$boxSize + 1;
$start = floor(($size-$gridSize) / 2);
$end = $start + $gridSize;
$lineStart = $start + 1;
$lineEnd = $end - 2;
$fontSize = floor($boxSize/2) - 4;
$fdx = floor(($boxSize - imagefontwidth($fontSize)) / 2);
$fdy = ceil(($boxSize - imagefontheight($fontSize)) / 2) - 1;
$fontColor = $white;
for ($i = $start + $boxSize; $i < $end - $boxSize; $i += $boxSize) {
ImageLine($thumbnail, $lineStart, $i, $lineEnd, $i, $grey);
ImageLine($thumbnail, $i, $lineStart, $i, $lineEnd, $grey);
}
for ($i = $start; $i < $end; $i += 3*$boxSize) {
ImageLine($thumbnail, $lineStart, $i, $lineEnd, $i, $black);
ImageLine($thumbnail, $i, $lineStart, $i, $lineEnd, $black);
}
$x = $start + 1;
$y = $start + 1;
$boxSizeMinusTwo = $boxSize - 2;
foreach(str_split($gridStr) as $i => $value) {
if ($value == UNKNOWN) {
$bgColor = $white;
} else {
$bgColor = $blue;
}
imagefilledrectangle($thumbnail, $x, $y, $x+$boxSizeMinusTwo, $y+$boxSizeMinusTwo, $bgColor);
if ($value != UNKNOWN) imagestring($thumbnail, $fontSize, $x + $fdx, $y + $fdy, $value, $fontColor);
$x += $boxSize;
if ($i % 9 == 8) {
$y += $boxSize;
$x = $start + 1;
}
}
}
imagepng($thumbnail);
?>