From 5c3046d855b8263eb8952c19e0c95606e5989ed6 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Thu, 8 Aug 2024 22:31:52 +0200 Subject: [PATCH] feat: Add dynamic favicons --- bin/cron/updateData.php | 22 ++++++++++- html/images/eggs/.gitignore | 1 + html/images/eggs/chimera/.gitignore | 1 + html/index.php | 6 ++- lib/favicon.php | 11 ++++++ lib/updateData.php | 59 ++++++++++++++++++++++++++++- view/fragments/mobSelection.php | 8 ++++ view/layout.php | 3 ++ 8 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 html/images/eggs/.gitignore create mode 100644 html/images/eggs/chimera/.gitignore create mode 100644 lib/favicon.php diff --git a/bin/cron/updateData.php b/bin/cron/updateData.php index ca94b85..1889224 100644 --- a/bin/cron/updateData.php +++ b/bin/cron/updateData.php @@ -38,7 +38,7 @@ $mobs = array_reduce($mobs, function ($mobs, $mob) { echo "Downloading images...\n"; foreach ($mobs as &$mob) { echo " ... " . $mob["name"] . "\n"; - $filename = downloadImage($mob["image"], $mob["name"]); + $filename = downloadImage($mob["image"], $mob["name"], "mobs"); $mob["filename"] = $filename; } @@ -48,4 +48,22 @@ foreach ($mobs as &$mob) { addOrUpdateMob($mob["name"], $mob["filename"]); } -echo "Done."; \ No newline at end of file +echo "Fetching spawn egg image URLs...\n"; +$spawnEggs = getSpawnEggsImages(); + +echo "Downloading images...\n"; +foreach ($spawnEggs as &$spawnEgg) { + echo " ... " . $spawnEgg["name"] . "\n"; + $filename = downloadImage($spawnEgg["image"], $spawnEgg["name"], "eggs"); + $spawnEgg["filename"] = $filename; +} + +echo "Making chimeras...\n"; +foreach ($spawnEggs as $leftEgg) { + foreach ($spawnEggs as $rightEgg) { + echo " ... " . $leftEgg["name"] . " " . $rightEgg["name"] . "\n"; + echo " -> " . makeChimera($leftEgg, $rightEgg, "eggs") . "\n"; + } +} + +echo "Done.\n"; \ No newline at end of file diff --git a/html/images/eggs/.gitignore b/html/images/eggs/.gitignore new file mode 100644 index 0000000..f59ec20 --- /dev/null +++ b/html/images/eggs/.gitignore @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/html/images/eggs/chimera/.gitignore b/html/images/eggs/chimera/.gitignore new file mode 100644 index 0000000..f59ec20 --- /dev/null +++ b/html/images/eggs/chimera/.gitignore @@ -0,0 +1 @@ +* \ No newline at end of file diff --git a/html/index.php b/html/index.php index c656bc8..a3c46d8 100644 --- a/html/index.php +++ b/html/index.php @@ -3,6 +3,7 @@ require_once __DIR__ . "/../core.php"; require_once __DIR__ . "/../lib/pairing.php"; require_once __DIR__ . "/../lib/rating.php"; +require_once __DIR__ . "/../lib/favicon.php"; ensureSession(); @@ -13,12 +14,15 @@ function renderChoice(): void { $ajax = isset($_GET["ajax"]); + $favicon = getFaviconUrl($left, $right); + if ($ajax) { include __DIR__ . "/../view/fragments/mobSelection.php"; } else { $title = "MobMash - Vote"; $description = "Which Minecraft mob is the best? Vote for your favorite mob. MobMash uses a chess rating algorithm to calculate a definitive ranking."; - $content = function() use ($left, $right, $csrfToken) { + + $content = function() use ($left, $right, $csrfToken, $favicon) { include __DIR__ . "/../view/pages/mobSelection.php"; }; diff --git a/lib/favicon.php b/lib/favicon.php new file mode 100644 index 0000000..96e8760 --- /dev/null +++ b/lib/favicon.php @@ -0,0 +1,11 @@ +execute([$filename, $name]) or die("unable to update mob"); echo " updated\n"; } +} + +function getSpawnEggsImages(): array { + $wikiPage = handleWikiRequest("titles=Spawn_Egg&prop=revisions&rvprop=content"); + $wikiText = getWikiTextFromResponse($wikiPage); + + [, $iconSection] = explode("=== Icons ===", $wikiText); + [$iconSection, ] = explode("===", $iconSection); + + $matches = []; + preg_match_all("/([a-zA-Z0-9_ -]+.png)/", $iconSection, $matches); + + return array_filter( + array_map( + fn ($image) => [ + "name" => trim( + str_replace("Spawn Egg.png", "", $image) + ) ?: "Spawn Egg", + "image" => getImageUrlFromName($image) + ], + array_filter( + array_unique($matches[1]), + fn($image) => !str_contains($image, "BE") + ) + ), + fn ($image) => !!$image["image"] + ); +} + +function makeChimera(array $left, array $right, string $path): string +{ + $imageLeft = new Imagick(IMAGE_BASE_PATH . $path . "/" . $left["filename"]); + $imageRight = new Imagick(IMAGE_BASE_PATH . $path . "/" . $right["filename"]); + + $height = min($imageLeft->getImageHeight(), $imageRight->getImageHeight()); + $imageLeft->resizeImage(0, $height, Imagick::FILTER_LANCZOS, 1); + $imageLeft->cropImage($height / 2, $height, 0, 0); + $imageRight->resizeImage(0, $height, Imagick::FILTER_LANCZOS, 1); + $imageRight->cropImage($height / 2, $height, $height / 2, 0); + + $chimera = new Imagick(); + $chimera->newImage($height, $height, new ImagickPixel("transparent")); + $chimera->compositeImage($imageLeft, Imagick::COMPOSITE_DEFAULT, 0, 0); + $chimera->compositeImage($imageRight, Imagick::COMPOSITE_DEFAULT, $height / 2, 0); + + $name = strtolower(str_replace(" ", "_", $left["name"] . " " . $right["name"] . ".png")); + + $chimera->setImageFormat('png'); + $chimera->writeImage(IMAGE_BASE_PATH . $path . "/chimera/" . $name); + + $chimera->destroy(); + + return $name; } \ No newline at end of file diff --git a/view/fragments/mobSelection.php b/view/fragments/mobSelection.php index 87505a0..cc6d43c 100644 --- a/view/fragments/mobSelection.php +++ b/view/fragments/mobSelection.php @@ -1,5 +1,7 @@
+
diff --git a/view/layout.php b/view/layout.php index 5cff7ef..07bb13a 100644 --- a/view/layout.php +++ b/view/layout.php @@ -1,5 +1,7 @@ "> +