Upgrade to 3.4.3

This commit is contained in:
Bastian Allgeier
2020-09-15 10:25:09 +02:00
parent 54b9ba3047
commit d8e797dd9b
108 changed files with 1750 additions and 523 deletions

View File

@@ -23,11 +23,25 @@ class Darkroom
protected $settings = [];
/**
* Darkroom constructor
*
* @param array $settings
*/
public function __construct(array $settings = [])
{
$this->settings = array_merge($this->defaults(), $settings);
}
/**
* Creates a new Darkroom instance for the given
* type/driver
*
* @param string $type
* @param array $settings
* @return mixed
* @throws \Exception
*/
public static function factory(string $type, array $settings = [])
{
if (isset(static::$types[$type]) === false) {
@@ -38,6 +52,11 @@ class Darkroom
return new $class($settings);
}
/**
* Returns the default thumb settings
*
* @return array
*/
protected function defaults(): array
{
return [
@@ -51,6 +70,12 @@ class Darkroom
];
}
/**
* Normalizes all thumb options
*
* @param array $options
* @return array
*/
protected function options(array $options = []): array
{
$options = array_merge($this->settings, $options);
@@ -84,6 +109,15 @@ class Darkroom
return $options;
}
/**
* Calculates the dimensions of the final thumb based
* on the given options and returns a full array with
* all the final options to be used for the image generator
*
* @param string $file
* @param array $options
* @return array
*/
public function preprocess(string $file, array $options = [])
{
$options = $this->options($options);
@@ -96,6 +130,14 @@ class Darkroom
return $options;
}
/**
* This method must be replaced by the driver to run the
* actual image processing job.
*
* @param string $file
* @param array $options
* @return array
*/
public function process(string $file, array $options = []): array
{
return $this->preprocess($file, $options);

View File

@@ -18,6 +18,13 @@ use Kirby\Image\Darkroom;
*/
class GdLib extends Darkroom
{
/**
* Processes the image with the SimpleImage library
*
* @param string $file
* @param array $options
* @return array
*/
public function process(string $file, array $options = []): array
{
$options = $this->preprocess($file, $options);
@@ -35,6 +42,14 @@ class GdLib extends Darkroom
return $options;
}
/**
* Activates the autoOrient option in SimpleImage
* unless this is deactivated
*
* @param \claviska\SimpleImage $image
* @param $options
* @return \claviska\SimpleImage
*/
protected function autoOrient(SimpleImage $image, $options)
{
if ($options['autoOrient'] === false) {
@@ -44,6 +59,13 @@ class GdLib extends Darkroom
return $image->autoOrient();
}
/**
* Wrapper around SimpleImage's resize and crop methods
*
* @param \claviska\SimpleImage $image
* @param array $options
* @return \claviska\SimpleImage
*/
protected function resize(SimpleImage $image, array $options)
{
if ($options['crop'] === false) {
@@ -53,6 +75,13 @@ class GdLib extends Darkroom
return $image->thumbnail($options['width'], $options['height'] ?? $options['width'], $options['crop']);
}
/**
* Applies the correct blur settings for SimpleImage
*
* @param \claviska\SimpleImage $image
* @param array $options
* @return \claviska\SimpleImage
*/
protected function blur(SimpleImage $image, array $options)
{
if ($options['blur'] === false) {
@@ -62,6 +91,13 @@ class GdLib extends Darkroom
return $image->blur('gaussian', (int)$options['blur']);
}
/**
* Applies grayscale conversion if activated in the options.
*
* @param \claviska\SimpleImage $image
* @param array $options
* @return \claviska\SimpleImage
*/
protected function grayscale(SimpleImage $image, array $options)
{
if ($options['grayscale'] === false) {

View File

@@ -17,6 +17,14 @@ use Kirby\Toolkit\F;
*/
class ImageMagick extends Darkroom
{
/**
* Activates imagemagick's auto-orient feature unless
* it is deactivated via the options
*
* @param string $file
* @param array $options
* @return string
*/
protected function autoOrient(string $file, array $options)
{
if ($options['autoOrient'] === true) {
@@ -24,6 +32,13 @@ class ImageMagick extends Darkroom
}
}
/**
* Applies the blur settings
*
* @param string $file
* @param array $options
* @return string
*/
protected function blur(string $file, array $options)
{
if ($options['blur'] !== false) {
@@ -31,6 +46,13 @@ class ImageMagick extends Darkroom
}
}
/**
* Keep animated gifs
*
* @param string $file
* @param array $options
* @return string
*/
protected function coalesce(string $file, array $options)
{
if (F::extension($file) === 'gif') {
@@ -38,11 +60,23 @@ class ImageMagick extends Darkroom
}
}
/**
* Creates the convert command with the right path to the binary file
*
* @param string $file
* @param array $options
* @return string
*/
protected function convert(string $file, array $options): string
{
return sprintf($options['bin'] . ' "%s"', $file);
}
/**
* Returns additional default parameters for imagemagick
*
* @return array
*/
protected function defaults(): array
{
return parent::defaults() + [
@@ -51,6 +85,13 @@ class ImageMagick extends Darkroom
];
}
/**
* Applies the correct settings for grayscale images
*
* @param string $file
* @param array $options
* @return string
*/
protected function grayscale(string $file, array $options)
{
if ($options['grayscale'] === true) {
@@ -58,6 +99,14 @@ class ImageMagick extends Darkroom
}
}
/**
* Applies the correct settings for interlaced JPEGs if
* activated via options
*
* @param string $file
* @param array $options
* @return string
*/
protected function interlace(string $file, array $options)
{
if ($options['interlace'] === true) {
@@ -65,6 +114,15 @@ class ImageMagick extends Darkroom
}
}
/**
* Creates and runs the full imagemagick command
* to process the image
*
* @param string $file
* @param array $options
* @return array
* @throws \Exception
*/
public function process(string $file, array $options = []): array
{
$options = $this->preprocess($file, $options);
@@ -95,11 +153,26 @@ class ImageMagick extends Darkroom
return $options;
}
/**
* Applies the correct JPEG compression quality settings
*
* @param string $file
* @param array $options
* @return string
*/
protected function quality(string $file, array $options): string
{
return '-quality ' . $options['quality'];
}
/**
* Creates the correct options to crop or resize the image
* and translates the crop positions for imagemagick
*
* @param string $file
* @param array $options
* @return string
*/
protected function resize(string $file, array $options): string
{
// simple resize
@@ -128,11 +201,26 @@ class ImageMagick extends Darkroom
return $command;
}
/**
* Makes sure to not process too many images at once
* which could crash the server
*
* @param string $file
* @param array $options
* @return string
*/
protected function save(string $file, array $options): string
{
return sprintf('-limit thread 1 "%s"', $file);
}
/**
* Removes all metadata from the image
*
* @param string $file
* @param array $options
* @return string
*/
protected function strip(string $file, array $options): string
{
return '-strip';

View File

@@ -66,7 +66,7 @@ class Dimensions
* Crops the dimensions by width and height
*
* @param int $width
* @param int $height
* @param int|null $height
* @return self
*/
public function crop(int $width, int $height = null)
@@ -159,7 +159,7 @@ class Dimensions
*
* </code>
*
* @param int $fit the max height
* @param int|null $fit the max height
* @param bool $force If true, the dimensions will be
* upscaled to fit the box if smaller
* @return self object with recalculated dimensions
@@ -173,7 +173,7 @@ class Dimensions
* Helper for fitWidth and fitHeight methods
*
* @param string $ref reference (width or height)
* @param int $fit the max width
* @param int|null $fit the max width
* @param bool $force If true, the dimensions will be
* upscaled to fit the box if smaller
* @return self object with recalculated dimensions
@@ -212,7 +212,7 @@ class Dimensions
*
* </code>
*
* @param int $fit the max width
* @param int|null $fit the max width
* @param bool $force If true, the dimensions will be
* upscaled to fit the box if smaller
* @return self object with recalculated dimensions
@@ -225,8 +225,8 @@ class Dimensions
/**
* Recalculates the dimensions by the width and height
*
* @param int $width the max height
* @param int $height the max width
* @param int|null $width the max height
* @param int|null $height the max width
* @param bool $force
* @return self
*/
@@ -362,8 +362,8 @@ class Dimensions
}
/**
* @param int $width
* @param int $height
* @param int|null $width
* @param int|null $height
* @param bool $force
* @return self
*/

View File

@@ -31,19 +31,19 @@ class Image extends File
protected $url;
/**
* @var Exif|null
* @var \Kirby\Image\Exif|null
*/
protected $exif;
/**
* @var Dimensions|null
* @var \Kirby\Image\Dimensions|null
*/
protected $dimensions;
/**
* Constructor
*
* @param string $root
* @param string|null $root
* @param string|null $url
*/
public function __construct(string $root = null, string $url = null)
@@ -132,8 +132,7 @@ class Image extends File
*/
public function header(bool $send = true)
{
$response = new Response();
$response->type($this->mime());
$response = new Response('', $this->mime());
return $send === true ? $response->send() : $response;
}
@@ -201,6 +200,7 @@ class Image extends File
*
* @param array $rules
* @return bool
* @throws \Exception
*/
public function match(array $rules): bool
{