2018-11-07 14:29:21 -05: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 16:26:17 -04:00
|
|
|
* PHP version 7.2
|
2018-11-07 14:29:21 -05: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-07 14:29:21 -05: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-07 14:29:21 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Controller;
|
|
|
|
|
2019-12-06 15:46:56 -05:00
|
|
|
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
|
|
|
|
|
2018-11-07 14:29:21 -05:00
|
|
|
use function Amp\Promise\wait;
|
2018-11-29 11:00:50 -05:00
|
|
|
use function Aviat\AnimeClient\getResponse;
|
|
|
|
use function Aviat\AnimeClient\createPlaceholderImage;
|
2018-11-07 14:29:21 -05:00
|
|
|
|
|
|
|
use Aviat\AnimeClient\Controller as BaseController;
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use Throwable;
|
|
|
|
|
2018-11-07 14:29:21 -05:00
|
|
|
/**
|
|
|
|
* Controller for handling routes that don't fit elsewhere
|
|
|
|
*/
|
|
|
|
final class Images extends BaseController {
|
|
|
|
/**
|
|
|
|
* Get image covers from kitsu
|
|
|
|
*
|
|
|
|
* @param string $type The category of image
|
|
|
|
* @param string $file The filename to look for
|
|
|
|
* @param bool $display Whether to output the image to the server
|
|
|
|
* @return void
|
2019-12-06 15:46:56 -05:00
|
|
|
* @throws NotFoundException
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws Throwable
|
2019-12-06 15:46:56 -05:00
|
|
|
* @throws ContainerException
|
2018-11-07 14:29:21 -05:00
|
|
|
*/
|
|
|
|
public function cache(string $type, string $file, $display = TRUE): void
|
|
|
|
{
|
2019-12-06 15:46:56 -05:00
|
|
|
$currentUrl = (string)$this->request->getUri();
|
2018-11-07 14:29:21 -05:00
|
|
|
|
|
|
|
$kitsuUrl = 'https://media.kitsu.io/';
|
|
|
|
$fileName = str_replace('-original', '', $file);
|
|
|
|
[$id, $ext] = explode('.', basename($fileName));
|
|
|
|
|
|
|
|
$baseSavePath = $this->config->get('img_cache_path');
|
|
|
|
|
|
|
|
// Kitsu doesn't serve webp, but for most use cases,
|
|
|
|
// jpg is a safe assumption
|
|
|
|
$tryJpg = ['anime','characters','manga','people'];
|
2018-11-29 11:00:50 -05:00
|
|
|
if ($ext === 'webp' && \in_array($type, $tryJpg, TRUE))
|
2018-11-07 14:29:21 -05:00
|
|
|
{
|
|
|
|
$ext = 'jpg';
|
|
|
|
$currentUrl = str_replace('webp', 'jpg', $currentUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
$typeMap = [
|
|
|
|
'anime' => [
|
|
|
|
'kitsuUrl' => "anime/poster_images/{$id}/medium.{$ext}",
|
|
|
|
'width' => 220,
|
|
|
|
'height' => 312,
|
|
|
|
],
|
|
|
|
'avatars' => [
|
|
|
|
'kitsuUrl' => "users/avatars/{$id}/original.{$ext}",
|
|
|
|
'width' => null,
|
|
|
|
'height' => null,
|
|
|
|
],
|
|
|
|
'characters' => [
|
|
|
|
'kitsuUrl' => "characters/images/{$id}/original.{$ext}",
|
|
|
|
'width' => 225,
|
|
|
|
'height' => 350,
|
|
|
|
],
|
|
|
|
'manga' => [
|
|
|
|
'kitsuUrl' => "manga/poster_images/{$id}/medium.{$ext}",
|
|
|
|
'width' => 220,
|
|
|
|
'height' => 312,
|
|
|
|
],
|
|
|
|
'people' => [
|
|
|
|
'kitsuUrl' => "people/images/{$id}/original.{$ext}",
|
|
|
|
'width' => null,
|
|
|
|
'height' => null,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$imageType = $typeMap[$type] ?? NULL;
|
|
|
|
|
|
|
|
if (NULL === $imageType)
|
|
|
|
{
|
|
|
|
$this->getPlaceholder($baseSavePath, 200, 200);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$kitsuUrl .= $imageType['kitsuUrl'];
|
|
|
|
$width = $imageType['width'];
|
|
|
|
$height = $imageType['height'];
|
|
|
|
$filePrefix = "{$baseSavePath}/{$type}/{$id}";
|
|
|
|
|
2018-11-29 11:00:50 -05:00
|
|
|
$response = getResponse($kitsuUrl);
|
2018-11-07 14:29:21 -05:00
|
|
|
|
|
|
|
if ($response->getStatus() !== 200)
|
|
|
|
{
|
|
|
|
// Try a few different file types before giving up
|
|
|
|
// webm => jpg => png => gif
|
|
|
|
$nextType = [
|
|
|
|
'jpg' => 'png',
|
|
|
|
'png' => 'gif',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (array_key_exists($ext, $nextType))
|
|
|
|
{
|
|
|
|
$newUrl = str_replace($ext, $nextType[$ext], $currentUrl);
|
|
|
|
$this->redirect($newUrl, 303);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($display)
|
|
|
|
{
|
|
|
|
$this->getPlaceholder("{$baseSavePath}/{$type}", $width, $height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
createPlaceholderImage("{$baseSavePath}/{$type}", $width, $height);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:26:17 -04:00
|
|
|
$data = wait($response->getBody()->buffer());
|
2018-11-07 14:29:21 -05:00
|
|
|
|
|
|
|
[$origWidth] = getimagesizefromstring($data);
|
|
|
|
$gdImg = imagecreatefromstring($data);
|
|
|
|
$resizedImg = imagescale($gdImg, $width ?? $origWidth);
|
|
|
|
|
|
|
|
if ($ext === 'gif')
|
|
|
|
{
|
|
|
|
file_put_contents("{$filePrefix}.gif", $data);
|
|
|
|
\imagepalletetotruecolor($gdImg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the webp versions
|
|
|
|
imagewebp($gdImg, "{$filePrefix}-original.webp");
|
|
|
|
imagewebp($resizedImg, "{$filePrefix}.webp");
|
|
|
|
|
|
|
|
// save the scaled jpeg file
|
|
|
|
imagejpeg($resizedImg, "{$filePrefix}.jpg");
|
|
|
|
|
|
|
|
// And the original
|
|
|
|
file_put_contents("{$filePrefix}-original.jpg", $data);
|
|
|
|
|
|
|
|
imagedestroy($gdImg);
|
|
|
|
imagedestroy($resizedImg);
|
|
|
|
|
|
|
|
if ($display)
|
|
|
|
{
|
|
|
|
$contentType = ($ext === 'webp')
|
2018-11-29 11:00:50 -05:00
|
|
|
? 'image/webp'
|
2018-11-07 14:29:21 -05:00
|
|
|
: $response->getHeader('content-type')[0];
|
|
|
|
|
|
|
|
$outputFile = (strpos($file, '-original') !== FALSE)
|
|
|
|
? "{$filePrefix}-original.{$ext}"
|
|
|
|
: "{$filePrefix}.{$ext}";
|
|
|
|
|
|
|
|
header("Content-Type: {$contentType}");
|
|
|
|
echo file_get_contents($outputFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a placeholder for a missing image
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param int|null $width
|
|
|
|
* @param int|null $height
|
|
|
|
*/
|
|
|
|
private function getPlaceholder (string $path, ?int $width = 200, ?int $height = NULL): void
|
|
|
|
{
|
|
|
|
$height = $height ?? $width;
|
|
|
|
|
|
|
|
$filename = $path . '/placeholder.png';
|
|
|
|
|
|
|
|
if ( ! file_exists($path . '/placeholder.png'))
|
|
|
|
{
|
|
|
|
createPlaceholderImage($path, $width, $height);
|
|
|
|
}
|
|
|
|
|
|
|
|
header('Content-Type: image/png');
|
|
|
|
echo file_get_contents($filename);
|
|
|
|
}
|
2018-11-01 22:12:41 -04:00
|
|
|
}
|