2018-11-01 22:15:20 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2020-03-11 15:15:05 -04:00
|
|
|
* PHP version 7.3
|
2018-11-01 22:15:20 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2018-11-01 22:15:20 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2018-11-01 22:15:20 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Helper;
|
|
|
|
|
|
|
|
use Aviat\Ion\Di\ContainerAware;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify picture elements
|
|
|
|
*/
|
|
|
|
final class Picture {
|
|
|
|
|
|
|
|
use ContainerAware;
|
|
|
|
|
2018-11-02 12:58:19 -04:00
|
|
|
private const MIME_MAP = [
|
|
|
|
'apng' => 'image/vnd.mozilla.apng',
|
|
|
|
'bmp' => 'image/bmp',
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
'ico' => 'image/x-icon',
|
|
|
|
'jpeg' => 'image/jpeg',
|
|
|
|
'jpf' => 'image/jpx',
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
'jpx' => 'image/jpx',
|
|
|
|
'png' => 'image/png',
|
|
|
|
'svg' => 'image/svg+xml',
|
|
|
|
'tif' => 'image/tiff',
|
|
|
|
'tiff' => 'image/tiff',
|
|
|
|
'webp' => 'image/webp',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const SIMPLE_IMAGE_TYPES = [
|
|
|
|
'gif',
|
|
|
|
'jpeg',
|
|
|
|
'jpg',
|
|
|
|
'png',
|
|
|
|
];
|
|
|
|
|
2018-11-01 22:15:20 -04:00
|
|
|
/**
|
2018-11-02 12:58:19 -04:00
|
|
|
* Create the html for an html picture element
|
2018-11-01 22:15:20 -04:00
|
|
|
*
|
2018-11-02 12:58:19 -04:00
|
|
|
* @param string $uri
|
2018-11-01 22:15:20 -04:00
|
|
|
* @param string $fallbackExt
|
|
|
|
* @param array $picAttrs
|
|
|
|
* @param array $imgAttrs
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-11-02 12:58:19 -04:00
|
|
|
public function __invoke(string $uri, string $fallbackExt = 'jpg', array $picAttrs = [], array $imgAttrs = []): string
|
2018-11-01 22:15:20 -04:00
|
|
|
{
|
|
|
|
$urlGenerator = $this->container->get('url-generator');
|
|
|
|
$helper = $this->container->get('html-helper');
|
2018-11-02 12:58:19 -04:00
|
|
|
|
2018-11-01 22:15:20 -04:00
|
|
|
// If it is a placeholder image, make the
|
|
|
|
// fallback a png, not a jpg
|
2018-11-02 12:58:19 -04:00
|
|
|
if (strpos($uri, 'placeholder') !== FALSE)
|
2018-11-01 22:15:20 -04:00
|
|
|
{
|
|
|
|
$fallbackExt = 'png';
|
|
|
|
}
|
|
|
|
|
2018-11-02 12:58:19 -04:00
|
|
|
if (strpos($uri, '//') === FALSE)
|
2018-11-01 22:15:20 -04:00
|
|
|
{
|
2018-11-02 12:58:19 -04:00
|
|
|
$uri = $urlGenerator->assetUrl($uri);
|
2018-11-01 22:15:20 -04:00
|
|
|
}
|
|
|
|
|
2018-11-02 12:58:19 -04:00
|
|
|
$urlParts = explode('.', $uri);
|
2018-11-01 22:15:20 -04:00
|
|
|
$ext = array_pop($urlParts);
|
|
|
|
$path = implode('.', $urlParts);
|
|
|
|
|
2018-11-02 12:58:19 -04:00
|
|
|
$mime = array_key_exists($ext, static::MIME_MAP)
|
|
|
|
? static::MIME_MAP[$ext]
|
|
|
|
: 'image/jpeg';
|
|
|
|
|
|
|
|
$fallbackMime = array_key_exists($fallbackExt, static::MIME_MAP)
|
|
|
|
? static::MIME_MAP[$fallbackExt]
|
|
|
|
: 'image/jpeg';
|
|
|
|
|
|
|
|
// For image types that are well-established, just return a
|
|
|
|
// simple <img /> element instead
|
|
|
|
if (
|
|
|
|
$ext === $fallbackExt ||
|
|
|
|
\in_array($ext, static::SIMPLE_IMAGE_TYPES, TRUE)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$attrs = ( ! empty($imgAttrs))
|
|
|
|
? $imgAttrs
|
|
|
|
: $picAttrs;
|
|
|
|
|
|
|
|
return $helper->img($uri, $attrs);
|
|
|
|
}
|
2018-11-01 22:15:20 -04:00
|
|
|
|
|
|
|
$fallbackImg = "{$path}.{$fallbackExt}";
|
|
|
|
|
|
|
|
$pictureChildren = [
|
|
|
|
$helper->void('source', [
|
2018-11-02 12:58:19 -04:00
|
|
|
'srcset' => $uri,
|
2018-11-01 22:15:20 -04:00
|
|
|
'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
|