HummingBirdAnimeClient/src/AnimeClient/Helper/Picture.php

118 lines
2.6 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Helper;
use Aviat\Ion\Di\ContainerAware;
use function in_array;
/**
* Simplify picture elements
*/
final class Picture
{
use ContainerAware;
private const SIMPLE_IMAGE_TYPES = [
'gif',
'jpeg',
'jpg',
'png',
];
/**
2020-12-10 15:59:37 -05:00
* Create the html for an html picture element.
* Uses .webp images with fallback
*/
public function __invoke(string $uri, string $fallbackExt = 'jpg', array $picAttrs = [], array $imgAttrs = []): string
{
$urlGenerator = $this->container->get('url-generator');
$helper = $this->container->get('html-helper');
2020-04-07 21:53:53 -04:00
$imgAttrs['loading'] = 'lazy';
$picAttrs['loading'] = 'lazy';
// If it is a placeholder image, make the
// fallback a png, not a jpg
2021-02-17 20:02:51 -05:00
if (str_contains($uri, 'placeholder'))
{
$fallbackExt = 'png';
}
2021-02-17 20:02:51 -05:00
if ( ! str_contains($uri, '//'))
{
$uri = $urlGenerator->assetUrl($uri);
}
$urlParts = explode('.', $uri);
$ext = array_pop($urlParts);
$path = implode('.', $urlParts);
$mime = match ($ext)
{
2021-02-23 12:00:22 -05:00
'avif' => 'image/avif',
'apng' => 'image/vnd.mozilla.apng',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ico' => 'image/x-icon',
'jpf', 'jpx' => 'image/jpx',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'tif', 'tiff' => 'image/tiff',
'webp' => 'image/webp',
default => 'image/jpeg',
};
$fallbackMime = match ($fallbackExt)
{
2021-02-23 12:00:22 -05:00
'gif' => 'image/gif',
'png' => 'image/png',
default => 'image/jpeg',
};
// For image types that are well-established, just return a
// simple <img /> element instead
if (
$ext === $fallbackExt
|| in_array($ext, Picture::SIMPLE_IMAGE_TYPES, TRUE)
) {
2021-02-23 12:00:22 -05:00
$attrs = (count($imgAttrs) > 1)
? $imgAttrs
: $picAttrs;
return $helper->img($uri, $attrs);
}
$fallbackImg = "{$path}.{$fallbackExt}";
$pictureChildren = [
$helper->void('source', [
'srcset' => $uri,
'type' => $mime,
]),
$helper->void('source', [
'srcset' => $fallbackImg,
'type' => $fallbackMime,
]),
$helper->img($fallbackImg, array_merge(['alt' => ''], $imgAttrs)),
];
$sources = implode('', $pictureChildren);
return $helper->elementRaw('picture', $sources, $picAttrs);
}
}
// End of Picture.php