mirror of
https://github.com/getgrav/grav.git
synced 2026-09-02 05:16:13 +02:00
Improve medium classes/trait
This commit is contained in:
@@ -9,12 +9,14 @@
|
||||
|
||||
namespace Grav\Common\Media\Traits;
|
||||
|
||||
use Grav\Common\Config\Config;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\ImageMediaInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Page\Medium\ImageFile;
|
||||
use Grav\Common\Page\Medium\ImageMedium;
|
||||
use Grav\Common\Page\Medium\MediumFactory;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
use function array_key_exists;
|
||||
use function extension_loaded;
|
||||
use function func_num_args;
|
||||
@@ -28,28 +30,20 @@ trait ImageMediaTrait
|
||||
{
|
||||
/** @var ImageFile|null */
|
||||
protected $image;
|
||||
|
||||
/** @var string */
|
||||
protected $format = 'guess';
|
||||
|
||||
/** @var int */
|
||||
protected $quality;
|
||||
|
||||
/** @var int */
|
||||
protected $default_quality;
|
||||
|
||||
/** @var bool */
|
||||
protected $debug_watermarked = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $auto_sizes;
|
||||
|
||||
/** @var bool */
|
||||
protected $aspect_ratio;
|
||||
|
||||
/** @var integer */
|
||||
protected $retina_scale;
|
||||
|
||||
/** @var bool */
|
||||
protected $watermark;
|
||||
|
||||
@@ -348,6 +342,7 @@ trait ImageMediaTrait
|
||||
*/
|
||||
protected function image()
|
||||
{
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
|
||||
// Use existing cache folder or if it doesn't exist, create it.
|
||||
@@ -370,6 +365,7 @@ trait ImageMediaTrait
|
||||
->setPrettyName($this->getImagePrettyName());
|
||||
|
||||
// Fix orientation if enabled
|
||||
/** @var Config $config */
|
||||
$config = Grav::instance()['config'];
|
||||
if ($config->get('system.images.auto_fix_orientation', false) &&
|
||||
extension_loaded('exif') && function_exists('exif_read_data')) {
|
||||
@@ -414,6 +410,7 @@ trait ImageMediaTrait
|
||||
$ratio = 1;
|
||||
}
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
$overlay = $locator->findResource("system://assets/responsive-overlays/{$ratio}x.png") ?: $locator->findResource('system://assets/responsive-overlays/unknown.png');
|
||||
$this->image->merge(ImageFile::open($overlay));
|
||||
|
||||
@@ -37,12 +37,7 @@ trait MediaFileTrait
|
||||
*/
|
||||
public function modified()
|
||||
{
|
||||
$path = $this->path(false);
|
||||
if (!file_exists($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return filemtime($path) ?: null;
|
||||
return $this->get('modified');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,12 +47,7 @@ trait MediaFileTrait
|
||||
*/
|
||||
public function size()
|
||||
{
|
||||
$path = $this->path(false);
|
||||
if (!file_exists($path)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return filesize($path) ?: 0;
|
||||
return $this->get('size');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +78,7 @@ trait MediaFileTrait
|
||||
}
|
||||
|
||||
$path = $this->path(false);
|
||||
$output = preg_replace('|^' . preg_quote(GRAV_ROOT, '|') . '|', '', $path) ?: $path;
|
||||
$output = preg_replace('|^' . preg_quote(GRAV_WEBROOT, '|') . '|', '', $path) ?: $path;
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $this->getGrav()['locator'];
|
||||
|
||||
@@ -31,31 +31,22 @@ trait MediaObjectTrait
|
||||
{
|
||||
/** @var string */
|
||||
protected $mode = 'source';
|
||||
|
||||
/** @var MediaObjectInterface|null */
|
||||
protected $_thumbnail;
|
||||
|
||||
/** @var array */
|
||||
protected $thumbnailTypes = ['page', 'default'];
|
||||
|
||||
/** @var string|null */
|
||||
protected $thumbnailType;
|
||||
|
||||
/** @var MediaObjectInterface[] */
|
||||
protected $alternatives = [];
|
||||
|
||||
/** @var array */
|
||||
protected $attributes = [];
|
||||
|
||||
/** @var array */
|
||||
protected $styleAttributes = [];
|
||||
|
||||
/** @var array */
|
||||
protected $metadata = [];
|
||||
|
||||
/** @var array */
|
||||
protected $medium_querystring = [];
|
||||
|
||||
/** @var string */
|
||||
protected $timestamp;
|
||||
|
||||
|
||||
@@ -181,17 +181,18 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
|
||||
{
|
||||
$grav = $this->getGrav();
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $grav['locator'];
|
||||
$image_path = (string)($locator->findResource('cache://images', true) ?: $locator->findResource('cache://images', true, true));
|
||||
$saved_image_path = $this->saved_image_path = $this->saveImage();
|
||||
|
||||
$output = preg_replace('|^' . preg_quote(GRAV_ROOT, '|') . '|', '', $saved_image_path) ?: $saved_image_path;
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = $grav['locator'];
|
||||
|
||||
if ($locator->isStream($output)) {
|
||||
$output = (string)($locator->findResource($output, false) ?: $locator->findResource($output, false, true));
|
||||
}
|
||||
|
||||
$image_path = (string)($locator->findResource('cache://images', true) ?: $locator->findResource('cache://images', true, true));
|
||||
if (Utils::startsWith($output, $image_path)) {
|
||||
$image_dir = $locator->findResource('cache://images', false);
|
||||
$output = '/' . $image_dir . preg_replace('|^' . preg_quote($image_path, '|') . '|', '', $output);
|
||||
|
||||
@@ -53,9 +53,14 @@ class Medium extends Data implements RenderableInterface, MediaFileInterface
|
||||
|
||||
$this->def('mime', 'application/octet-stream');
|
||||
|
||||
if (!$this->offsetExists('size')) {
|
||||
$path = $this->get('filepath');
|
||||
$this->def('size', filesize($path));
|
||||
$path = $this->get('filepath');
|
||||
if ($path && file_exists($path)) {
|
||||
if (!$this->offsetExists('size')) {
|
||||
$this->set('size', filesize($path));
|
||||
}
|
||||
if (!$this->offsetExists('modified')) {
|
||||
$this->set('modified', filemtime($path));
|
||||
}
|
||||
}
|
||||
|
||||
$this->reset();
|
||||
|
||||
Reference in New Issue
Block a user