Image classes: added support for dependencies and extra params

This commit is contained in:
Matias Griese
2022-02-22 11:47:36 +02:00
parent 987c43d03f
commit 035e533c02
2 changed files with 26 additions and 2 deletions

View File

@@ -5,12 +5,13 @@ namespace Grav\Framework\Image;
use Grav\Framework\Compat\Serializable;
use Grav\Framework\Contracts\Image\ImageOperationsInterface;
use Grav\Framework\Image\Traits\ImageOperationsTrait;
use JsonSerializable;
use RuntimeException;
/**
* Image class.
*/
class Image implements ImageOperationsInterface
class Image implements ImageOperationsInterface, JsonSerializable
{
use ImageOperationsTrait;
use Serializable;
@@ -25,6 +26,8 @@ class Image implements ImageOperationsInterface
protected $modified;
/** @var int */
protected $size;
/** @var array */
public $extra = [];
/**
* @param string $filepath
@@ -33,6 +36,8 @@ class Image implements ImageOperationsInterface
public function __construct(string $filepath, array $info)
{
$this->filepath = $filepath;
$this->modified = $info['modified'] ?? 0;
$this->size = $info['size'] ?? 0;
$this->origWidth = $this->width = $info['width'] ?? 0;
$this->origHeight = $this->height = $info['height'] ?? 0;
$this->orientation = isset($info['exif']['Orientation']) ? (int)$info['exif']['Orientation'] : null;
@@ -46,12 +51,16 @@ class Image implements ImageOperationsInterface
return [
'image' => 1,
'filepath' => $this->filepath,
'modified' => $this->modified,
'size' => $this->size,
'orientation' => $this->orientation,
'orig_width' => $this->origWidth,
'orig_height' => $this->origHeight,
'width' => $this->width,
'height' => $this->height,
'dependencies' => $this->dependencies,
'operations' => $this->operations,
'extra' => $this->extra
];
}
@@ -67,12 +76,24 @@ class Image implements ImageOperationsInterface
}
$this->filepath = $data['filepath'];
$this->modified = $data['modified'];
$this->size = $data['size'];
$this->origWidth = $data['orig_width'];
$this->origHeight = $data['orig_height'];
$this->orientation = $data['orientation'];
$this->width = $data['width'];
$this->height = $data['height'];
$this->dependencies = $data['dependencies'];
$this->operations = $data['operations'];
$this->extra = $data['extra'];
}
/**
* @return array
*/
public function jsonSerialize(): array
{
return ['hash' => $this->generateHash()] + $this->__serialize();
}
/**

View File

@@ -17,6 +17,8 @@ trait ImageOperationsTrait
/** @var int|null */
protected $orientation = null;
/** @var array */
protected $dependencies = [];
/** @var array */
protected $operations = [];
/**
@@ -475,8 +477,9 @@ trait ImageOperationsTrait
*/
public function merge(Image $other, int $x = 0, int $y = 0, int $width = 0, int $height = 0)
{
$serialized = $other->__serialize();
$serialized = $other->jsonSerialize();
$dependencies[] = $serialized;
$this->operations[] = ['merge', [$serialized, $x, $y, $width, $height]];
return $this;