From 85e7b0f6bcb6140cea3b977f2e1cf5d0c1b81d34 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 7 Jan 2022 19:57:07 -0500 Subject: [PATCH] Use image builder class to simplify createPlaceholderImage --- CHANGELOG.md | 4 +- console | 2 +- src/AnimeClient/AnimeClient.php | 97 ++++++++++----------------------- 3 files changed, 31 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e0a904..12ceddbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,8 @@ # Changelog -## Version 5.3 -* Updated to support PHP 8.1 - ## Version 5.2 * Updated PHP requirement to 8 +* Updated to support PHP 8.1 ## Version 5.1 * Added session check, so when coming back to a page, if the session is expired, the page will refresh. diff --git a/console b/console index 5019c37a..ff2d7ae1 100755 --- a/console +++ b/console @@ -26,7 +26,7 @@ try 'sync:lists' => Command\SyncLists::class ]))->run(); } -catch (\Throwable) +catch (Throwable) { } diff --git a/src/AnimeClient/AnimeClient.php b/src/AnimeClient/AnimeClient.php index 3a612abd..07effdd0 100644 --- a/src/AnimeClient/AnimeClient.php +++ b/src/AnimeClient/AnimeClient.php @@ -16,9 +16,8 @@ namespace Aviat\AnimeClient; -use Aviat\AnimeClient\Kitsu; +use Aviat\Ion\ImageBuilder; use Psr\SimpleCache\CacheInterface; -use Psr\SimpleCache\InvalidArgumentException; use Amp\Http\Client\Request; use Amp\Http\Client\Response; @@ -151,6 +150,21 @@ function tomlToArray(string $toml): array //! Misc Functions // ---------------------------------------------------------------------------- +if ( ! function_exists('array_is_list')) +{ + /** + * Polyfill for PHP 8 + * + * @see https://www.php.net/manual/en/function.array-is-list + * @param array $a + * @return bool + */ + function array_is_list(array $a): bool + { + return $a === [] || (array_keys($a) === range(0, count($a) - 1)); + } +} + /** * Is the array sequential, not associative? * @@ -164,15 +178,7 @@ function isSequentialArray(mixed $array): bool return FALSE; } - $i = 0; - foreach ($array as $k => $v) - { - if ($k !== $i++) - { - return FALSE; - } - } - return TRUE; + return array_is_list($array); } /** @@ -263,7 +269,7 @@ function getResponse (Request|string $request): Response * @param bool $webp * @return string */ -function getLocalImg (string $kitsuUrl, $webp = TRUE): string +function getLocalImg (string $kitsuUrl, bool $webp = TRUE): string { if (empty($kitsuUrl) || ( ! is_string($kitsuUrl))) { @@ -296,71 +302,26 @@ function getLocalImg (string $kitsuUrl, $webp = TRUE): string * * @codeCoverageIgnore * @param string $path - * @param int|null $width - * @param int|null $height + * @param int $width + * @param int $height * @param string $text * @return bool */ -function createPlaceholderImage (string $path, ?int $width, ?int $height, $text = 'Image Unavailable'): bool +function createPlaceholderImage (string $path, int $width = 200, int $height = 200, string $text = 'Image Unavailable'): bool { - $width = $width ?? 200; - $height = $height ?? 200; - - $img = imagecreatetruecolor($width, $height); - if ($img === FALSE) - { - return FALSE; - } - imagealphablending($img, TRUE); + $img = ImageBuilder::new($width, $height) + ->enableAlphaBlending(TRUE) + ->addBackgroundColor(255, 255, 255, 127) + ->addCenteredText($text, 64, 64, 64, 1); $path = rtrim($path, '/'); - // Background is the first color by default - $fillColor = imagecolorallocatealpha($img, 255, 255, 255, 127); - if ($fillColor === FALSE) - { - return FALSE; - } - imagefill($img, 0, 0, $fillColor); + $savedPng = $img->savePng($path . '/placeholder.png'); + $savedWebp = $img->saveWebp($path . '/placeholder.webp'); - $textColor = imagecolorallocate($img, 64, 64, 64); - if ($textColor === FALSE) - { - return FALSE; - } + $img->cleanup(); - imagealphablending($img, TRUE); - - // Generate placeholder text - $fontSize = 10; - $fontWidth = imagefontwidth($fontSize); - $fontHeight = imagefontheight($fontSize); - $length = strlen($text); - $textWidth = $length * $fontWidth; - $fxPos = (int) ceil((imagesx($img) - $textWidth) / 2); - $fyPos = (int) ceil((imagesy($img) - $fontHeight) / 2); - - // Add the image text - imagestring($img, $fontSize, $fxPos, $fyPos, $text, $textColor); - - // Save the images - imagesavealpha($img, TRUE); - imagepng($img, $path . '/placeholder.png', 9); - imagedestroy($img); - - $pngImage = imagecreatefrompng($path . '/placeholder.png'); - if ($pngImage === FALSE) - { - return FALSE; - } - imagealphablending($pngImage, TRUE); - imagesavealpha($pngImage, TRUE); - - imagewebp($pngImage, $path . '/placeholder.webp'); - - imagedestroy($pngImage); - - return TRUE; + return $savedPng && $savedWebp; } /**