Use image builder class to simplify createPlaceholderImage

This commit is contained in:
Timothy Warren 2022-01-07 19:57:07 -05:00
parent 854987bd44
commit ed558a6484
3 changed files with 31 additions and 72 deletions

View File

@ -1,10 +1,8 @@
# Changelog # Changelog
## Version 5.3
* Updated to support PHP 8.1
## Version 5.2 ## Version 5.2
* Updated PHP requirement to 8 * Updated PHP requirement to 8
* Updated to support PHP 8.1
## Version 5.1 ## Version 5.1
* Added session check, so when coming back to a page, if the session is expired, the page will refresh. * Added session check, so when coming back to a page, if the session is expired, the page will refresh.

View File

@ -26,7 +26,7 @@ try
'sync:lists' => Command\SyncLists::class 'sync:lists' => Command\SyncLists::class
]))->run(); ]))->run();
} }
catch (\Throwable) catch (Throwable)
{ {
} }

View File

@ -16,9 +16,8 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use Aviat\AnimeClient\Kitsu; use Aviat\Ion\ImageBuilder;
use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Amp\Http\Client\Request; use Amp\Http\Client\Request;
use Amp\Http\Client\Response; use Amp\Http\Client\Response;
@ -151,6 +150,21 @@ function tomlToArray(string $toml): array
//! Misc Functions //! 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? * Is the array sequential, not associative?
* *
@ -164,15 +178,7 @@ function isSequentialArray(mixed $array): bool
return FALSE; return FALSE;
} }
$i = 0; return array_is_list($array);
foreach ($array as $k => $v)
{
if ($k !== $i++)
{
return FALSE;
}
}
return TRUE;
} }
/** /**
@ -263,7 +269,7 @@ function getResponse (Request|string $request): Response
* @param bool $webp * @param bool $webp
* @return string * @return string
*/ */
function getLocalImg (string $kitsuUrl, $webp = TRUE): string function getLocalImg (string $kitsuUrl, bool $webp = TRUE): string
{ {
if (empty($kitsuUrl) || ( ! is_string($kitsuUrl))) if (empty($kitsuUrl) || ( ! is_string($kitsuUrl)))
{ {
@ -296,71 +302,26 @@ function getLocalImg (string $kitsuUrl, $webp = TRUE): string
* *
* @codeCoverageIgnore * @codeCoverageIgnore
* @param string $path * @param string $path
* @param int|null $width * @param int $width
* @param int|null $height * @param int $height
* @param string $text * @param string $text
* @return bool * @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; $img = ImageBuilder::new($width, $height)
$height = $height ?? 200; ->enableAlphaBlending(TRUE)
->addBackgroundColor(255, 255, 255, 127)
$img = imagecreatetruecolor($width, $height); ->addCenteredText($text, 64, 64, 64, 1);
if ($img === FALSE)
{
return FALSE;
}
imagealphablending($img, TRUE);
$path = rtrim($path, '/'); $path = rtrim($path, '/');
// Background is the first color by default $savedPng = $img->savePng($path . '/placeholder.png');
$fillColor = imagecolorallocatealpha($img, 255, 255, 255, 127); $savedWebp = $img->saveWebp($path . '/placeholder.webp');
if ($fillColor === FALSE)
{
return FALSE;
}
imagefill($img, 0, 0, $fillColor);
$textColor = imagecolorallocate($img, 64, 64, 64); $img->cleanup();
if ($textColor === FALSE)
{
return FALSE;
}
imagealphablending($img, TRUE); return $savedPng && $savedWebp;
// 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;
} }
/** /**