2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
2015-11-16 11:40:01 -05:00
|
|
|
* Hummingbird Anime Client
|
|
|
|
*
|
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren
|
2016-01-04 16:58:33 -05:00
|
|
|
* @copyright Copyright (c) 2015 - 2016
|
2015-11-16 11:40:01 -05:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
* @license MIT
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-18 10:30:28 -04:00
|
|
|
use abeautifulsite\SimpleImage;
|
2015-10-05 16:54:25 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2015-06-18 10:30:28 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Common base for all Models
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
class Model {
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
use \Aviat\Ion\StringWrapper;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The global configuration object
|
2015-09-14 10:54:50 -04:00
|
|
|
* @var Config
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
protected $config;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
/**
|
|
|
|
* The container object
|
2015-10-06 11:38:20 -04:00
|
|
|
* @var ContainerInterface
|
2015-09-14 10:54:50 -04:00
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
2015-09-16 12:25:35 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->container = $container;
|
|
|
|
$this->config = $container->get('config');
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
2015-06-09 11:54:42 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path of the cached version of the image. Create the cached image
|
|
|
|
* if the file does not already exist
|
|
|
|
*
|
2015-06-26 12:03:42 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-09 11:54:42 -04:00
|
|
|
* @param string $api_path - The original image url
|
|
|
|
* @param string $series_slug - The part of the url with the series name, becomes the image name
|
|
|
|
* @param string $type - Anime or Manga, controls cache path
|
|
|
|
* @return string - the frontend path for the cached image
|
2016-04-19 13:02:50 -04:00
|
|
|
* @throws DomainException
|
2015-06-09 11:54:42 -04:00
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function get_cached_image($api_path, $series_slug, $type = "anime")
|
2015-06-09 11:54:42 -04:00
|
|
|
{
|
|
|
|
$path_parts = explode('?', basename($api_path));
|
|
|
|
$path = current($path_parts);
|
|
|
|
$ext_parts = explode('.', $path);
|
|
|
|
$ext = end($ext_parts);
|
2015-06-18 10:30:28 -04:00
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
// Workaround for some broken file extensions
|
2015-11-05 10:41:46 -05:00
|
|
|
if ($ext == "jjpg")
|
|
|
|
{
|
|
|
|
$ext = "jpg";
|
|
|
|
}
|
2015-06-18 10:30:28 -04:00
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
// Failsafe for weird urls
|
2015-11-09 11:10:15 -05:00
|
|
|
if (strlen($ext) > 3)
|
|
|
|
{
|
|
|
|
return $api_path;
|
|
|
|
}
|
2015-06-09 11:54:42 -04:00
|
|
|
|
2015-10-06 11:38:20 -04:00
|
|
|
$img_cache_path = $this->config->get('img_cache_path');
|
2015-06-09 11:54:42 -04:00
|
|
|
$cached_image = "{$series_slug}.{$ext}";
|
2015-10-06 11:38:20 -04:00
|
|
|
$cached_path = "{$img_cache_path}/{$type}/{$cached_image}";
|
2015-06-09 11:54:42 -04:00
|
|
|
|
|
|
|
// Cache the file if it doesn't already exist
|
|
|
|
if ( ! file_exists($cached_path))
|
|
|
|
{
|
2015-10-12 14:11:00 -04:00
|
|
|
if (function_exists('curl_init'))
|
2015-06-09 11:54:42 -04:00
|
|
|
{
|
|
|
|
$ch = curl_init($api_path);
|
|
|
|
$fp = fopen($cached_path, 'wb');
|
|
|
|
curl_setopt_array($ch, [
|
|
|
|
CURLOPT_FILE => $fp,
|
|
|
|
CURLOPT_HEADER => 0
|
|
|
|
]);
|
|
|
|
curl_exec($ch);
|
|
|
|
curl_close($ch);
|
2015-10-05 16:54:25 -04:00
|
|
|
fclose($fp);
|
2015-06-09 11:54:42 -04:00
|
|
|
}
|
2015-10-12 14:11:00 -04:00
|
|
|
else if (ini_get('allow_url_fopen'))
|
|
|
|
{
|
|
|
|
copy($api_path, $cached_path);
|
|
|
|
}
|
2015-06-09 11:54:42 -04:00
|
|
|
else
|
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
throw new DomainException("Couldn't cache images because they couldn't be downloaded.");
|
2015-06-09 11:54:42 -04:00
|
|
|
}
|
2015-06-18 10:30:28 -04:00
|
|
|
|
|
|
|
// Resize the image
|
|
|
|
if ($type == 'anime')
|
|
|
|
{
|
|
|
|
$resize_width = 220;
|
|
|
|
$resize_height = 319;
|
|
|
|
$this->_resize($cached_path, $resize_width, $resize_height);
|
|
|
|
}
|
2015-06-09 11:54:42 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
return "/public/images/{$type}/{$cached_image}";
|
2015-06-09 11:54:42 -04:00
|
|
|
}
|
2015-06-18 10:30:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize an image
|
|
|
|
*
|
2015-06-26 12:03:42 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-06-18 10:30:28 -04:00
|
|
|
* @param string $path
|
|
|
|
* @param string $width
|
|
|
|
* @param string $height
|
|
|
|
*/
|
|
|
|
private function _resize($path, $width, $height)
|
|
|
|
{
|
2016-04-19 13:02:50 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$img = new SimpleImage($path);
|
|
|
|
$img->resize($width, $height)->save();
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
// Catch image errors, since they don't otherwise affect
|
|
|
|
// functionality
|
|
|
|
}
|
2015-06-18 10:30:28 -04:00
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
// End of BaseModel.php
|