Upgrade to 3.6.0
This commit is contained in:
@@ -2,33 +2,26 @@
|
||||
|
||||
namespace Kirby\Image;
|
||||
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Http\Response;
|
||||
use Kirby\Toolkit\File;
|
||||
use Kirby\Filesystem\File;
|
||||
use Kirby\Toolkit\Html;
|
||||
use Kirby\Toolkit\Mime;
|
||||
use Kirby\Toolkit\V;
|
||||
|
||||
/**
|
||||
* A representation of an image/media file
|
||||
* A representation of an image file
|
||||
* with dimensions, optional exif data and
|
||||
* a connection to our darkroom classes to resize/crop
|
||||
* images.
|
||||
*
|
||||
* Extends the `Kirby\Filesystem\File` class with
|
||||
* those image-specific methods.
|
||||
*
|
||||
* @package Kirby Image
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Image extends File
|
||||
{
|
||||
/**
|
||||
* optional url where the file is reachable
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* @var \Kirby\Image\Exif|null
|
||||
*/
|
||||
@@ -40,39 +33,52 @@ class Image extends File
|
||||
protected $dimensions;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string|null $root
|
||||
* @param string|null $url
|
||||
* @var array
|
||||
*/
|
||||
public function __construct(string $root = null, string $url = null)
|
||||
{
|
||||
parent::__construct($root);
|
||||
$this->url = $url;
|
||||
}
|
||||
public static $resizableTypes = [
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'gif',
|
||||
'png',
|
||||
'webp'
|
||||
];
|
||||
|
||||
/**
|
||||
* Improved `var_dump` output
|
||||
*
|
||||
* @return array
|
||||
* @var array
|
||||
*/
|
||||
public function __debugInfo(): array
|
||||
{
|
||||
return array_merge($this->toArray(), [
|
||||
'dimensions' => $this->dimensions(),
|
||||
'exif' => $this->exif(),
|
||||
]);
|
||||
}
|
||||
public static $viewableTypes = [
|
||||
'avif',
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'gif',
|
||||
'png',
|
||||
'svg',
|
||||
'webp'
|
||||
];
|
||||
|
||||
/**
|
||||
* Returns a full link to this file
|
||||
* Perfect for debugging in connection with echo
|
||||
* Validation rules to be used for `::match()`
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $validations = [
|
||||
'maxsize' => ['size', 'max'],
|
||||
'minsize' => ['size', 'min'],
|
||||
'maxwidth' => ['width', 'max'],
|
||||
'minwidth' => ['width', 'min'],
|
||||
'maxheight' => ['height', 'max'],
|
||||
'minheight' => ['height', 'min'],
|
||||
'orientation' => ['orientation', 'same']
|
||||
];
|
||||
|
||||
/**
|
||||
* Returns the `<img>` tag for the image object
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->root;
|
||||
return $this->html();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +92,13 @@ class Image extends File
|
||||
return $this->dimensions;
|
||||
}
|
||||
|
||||
if (in_array($this->mime(), ['image/jpeg', 'image/jp2', 'image/png', 'image/gif', 'image/webp'])) {
|
||||
if (in_array($this->mime(), [
|
||||
'image/jpeg',
|
||||
'image/jp2',
|
||||
'image/png',
|
||||
'image/gif',
|
||||
'image/webp'
|
||||
])) {
|
||||
return $this->dimensions = Dimensions::forImage($this->root);
|
||||
}
|
||||
|
||||
@@ -97,18 +109,6 @@ class Image extends File
|
||||
return $this->dimensions = new Dimensions(0, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Automatically sends all needed headers for the file to be downloaded
|
||||
* and echos the file's content
|
||||
*
|
||||
* @param string|null $filename Optional filename for the download
|
||||
* @return string
|
||||
*/
|
||||
public function download($filename = null): string
|
||||
{
|
||||
return Response::download($this->root, $filename ?? $this->filename());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the exif object for this file (if image)
|
||||
*
|
||||
@@ -116,23 +116,7 @@ class Image extends File
|
||||
*/
|
||||
public function exif()
|
||||
{
|
||||
if ($this->exif !== null) {
|
||||
return $this->exif;
|
||||
}
|
||||
$this->exif = new Exif($this);
|
||||
return $this->exif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an appropriate header for the asset
|
||||
*
|
||||
* @param bool $send
|
||||
* @return \Kirby\Http\Response|string
|
||||
*/
|
||||
public function header(bool $send = true)
|
||||
{
|
||||
$response = new Response('', $this->mime());
|
||||
return $send === true ? $response->send() : $response;
|
||||
return $this->exif = $this->exif ?? new Exif($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,6 +130,8 @@ class Image extends File
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the file to html
|
||||
*
|
||||
* @param array $attr
|
||||
* @return string
|
||||
*/
|
||||
@@ -195,80 +181,24 @@ class Image extends File
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a set of validations on the image object
|
||||
* Checks if the file is a resizable image
|
||||
*
|
||||
* @param array $rules
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function match(array $rules): bool
|
||||
public function isResizable(): bool
|
||||
{
|
||||
$rules = array_change_key_case($rules);
|
||||
return in_array($this->extension(), static::$resizableTypes) === true;
|
||||
}
|
||||
|
||||
if (is_array($rules['mime'] ?? null) === true) {
|
||||
$mime = $this->mime();
|
||||
|
||||
// determine if any pattern matches the MIME type;
|
||||
// once any pattern matches, `$carry` is `true` and the rest is skipped
|
||||
$matches = array_reduce($rules['mime'], function ($carry, $pattern) use ($mime) {
|
||||
return $carry || Mime::matches($mime, $pattern);
|
||||
}, false);
|
||||
|
||||
if ($matches !== true) {
|
||||
throw new Exception([
|
||||
'key' => 'file.mime.invalid',
|
||||
'data' => compact('mime')
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($rules['extension'] ?? null) === true) {
|
||||
$extension = $this->extension();
|
||||
if (in_array($extension, $rules['extension']) !== true) {
|
||||
throw new Exception([
|
||||
'key' => 'file.extension.invalid',
|
||||
'data' => compact('extension')
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($rules['type'] ?? null) === true) {
|
||||
$type = $this->type();
|
||||
if (in_array($type, $rules['type']) !== true) {
|
||||
throw new Exception([
|
||||
'key' => 'file.type.invalid',
|
||||
'data' => compact('type')
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$validations = [
|
||||
'maxsize' => ['size', 'max'],
|
||||
'minsize' => ['size', 'min'],
|
||||
'maxwidth' => ['width', 'max'],
|
||||
'minwidth' => ['width', 'min'],
|
||||
'maxheight' => ['height', 'max'],
|
||||
'minheight' => ['height', 'min'],
|
||||
'orientation' => ['orientation', 'same']
|
||||
];
|
||||
|
||||
foreach ($validations as $key => $arguments) {
|
||||
$rule = $rules[$key] ?? null;
|
||||
|
||||
if ($rule !== null) {
|
||||
$property = $arguments[0];
|
||||
$validator = $arguments[1];
|
||||
|
||||
if (V::$validator($this->$property(), $rule) === false) {
|
||||
throw new Exception([
|
||||
'key' => 'file.' . $key,
|
||||
'data' => [$property => $rule]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
/**
|
||||
* Checks if a preview can be displayed for the file
|
||||
* in the Panel or in the frontend
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isViewable(): bool
|
||||
{
|
||||
return in_array($this->extension(), static::$viewableTypes) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,38 +223,20 @@ class Image extends File
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the media object to a
|
||||
* plain PHP array
|
||||
* Converts the object to an array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_merge(parent::toArray(), [
|
||||
$array = array_merge(parent::toArray(), [
|
||||
'dimensions' => $this->dimensions()->toArray(),
|
||||
'exif' => $this->exif()->toArray(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the entire file array into
|
||||
* a json string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson(): string
|
||||
{
|
||||
return json_encode($this->toArray());
|
||||
}
|
||||
ksort($array);
|
||||
|
||||
/**
|
||||
* Returns the url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url()
|
||||
{
|
||||
return $this->url;
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user