Add blueprints and fake content

This commit is contained in:
Paul Nicoué 2021-11-18 17:44:47 +01:00
parent 1ff19bf38f
commit 8235816462
592 changed files with 22385 additions and 31535 deletions

View file

@ -21,6 +21,9 @@ class Darkroom
'im' => 'Kirby\Image\Darkroom\ImageMagick'
];
/**
* @var array
*/
protected $settings = [];
/**
@ -61,8 +64,9 @@ class Darkroom
{
return [
'autoOrient' => true,
'crop' => false,
'blur' => false,
'crop' => false,
'format' => null,
'grayscale' => false,
'height' => null,
'quality' => 90,

View file

@ -2,9 +2,8 @@
namespace Kirby\Image\Darkroom;
ini_set('memory_limit', '512M');
use claviska\SimpleImage;
use Kirby\Filesystem\Mime;
use Kirby\Image\Darkroom;
/**
@ -28,6 +27,7 @@ class GdLib extends Darkroom
public function process(string $file, array $options = []): array
{
$options = $this->preprocess($file, $options);
$mime = $this->mime($options);
$image = new SimpleImage();
$image->fromFile($file);
@ -37,7 +37,7 @@ class GdLib extends Darkroom
$image = $this->blur($image, $options);
$image = $this->grayscale($image, $options);
$image->toFile($file, null, $options['quality']);
$image->toFile($file, $mime, $options['quality']);
return $options;
}
@ -106,4 +106,19 @@ class GdLib extends Darkroom
return $image->desaturate();
}
/**
* Returns mime type based on `format` option
*
* @param array $options
* @return string|null
*/
protected function mime(array $options): ?string
{
if ($options['format'] === null) {
return null;
}
return Mime::fromExtension($options['format']);
}
}

View file

@ -3,8 +3,8 @@
namespace Kirby\Image\Darkroom;
use Exception;
use Kirby\Filesystem\F;
use Kirby\Image\Darkroom;
use Kirby\Toolkit\F;
/**
* ImageMagick
@ -211,6 +211,10 @@ class ImageMagick extends Darkroom
*/
protected function save(string $file, array $options): string
{
if ($options['format'] !== null) {
$file = pathinfo($file, PATHINFO_DIRNAME) . '/' . pathinfo($file, PATHINFO_FILENAME) . '.' . $options['format'];
}
return sprintf('-limit thread 1 "%s"', $file);
}

View file

@ -17,7 +17,7 @@ class Exif
{
/**
* the parent image object
* @var Image
* @var \Kirby\Image\Image
*/
protected $image;
@ -207,9 +207,11 @@ class Exif
*/
protected function read(): array
{
// @codeCoverageIgnoreStart
if (function_exists('exif_read_data') === false) {
return [];
}
// @codeCoverageIgnoreEnd
$data = @exif_read_data($this->image->root());
return is_array($data) ? $data : [];
@ -253,7 +255,7 @@ class Exif
}
/**
* Teturn the focal length
* Return the focal length
*
* @return string|null
*/

View file

@ -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;
}
/**