1
0

downgrade to kirby v3

This commit is contained in:
Philip Wagner
2024-09-01 10:47:15 +02:00
parent a4b2aece7b
commit af86acb7a1
1085 changed files with 54743 additions and 65042 deletions

View File

@@ -13,9 +13,25 @@ namespace Kirby\Image;
*/
class Camera
{
protected string|null $make;
protected string|null $model;
/**
* Make exif data
*
* @var string|null
*/
protected $make;
/**
* Model exif data
*
* @var string|null
*/
protected $model;
/**
* Constructor
*
* @param array $exif
*/
public function __construct(array $exif)
{
$this->make = $exif['Make'] ?? null;
@@ -24,22 +40,28 @@ class Camera
/**
* Returns the make of the camera
*
* @return string
*/
public function make(): string|null
public function make(): ?string
{
return $this->make;
}
/**
* Returns the camera model
*
* @return string
*/
public function model(): string|null
public function model(): ?string
{
return $this->model;
}
/**
* Converts the object into a nicely readable array
*
* @return array
*/
public function toArray(): array
{
@@ -51,6 +73,8 @@ class Camera
/**
* Returns the full make + model name
*
* @return string
*/
public function __toString(): string
{
@@ -59,7 +83,8 @@ class Camera
/**
* Improved `var_dump` output
* @codeCoverageIgnore
*
* @return array
*/
public function __debugInfo(): array
{

View File

@@ -3,8 +3,6 @@
namespace Kirby\Image;
use Exception;
use Kirby\Image\Darkroom\GdLib;
use Kirby\Image\Darkroom\ImageMagick;
/**
* A wrapper around resizing and cropping
@@ -18,14 +16,23 @@ use Kirby\Image\Darkroom\ImageMagick;
*/
class Darkroom
{
public static array $types = [
'gd' => GdLib::class,
'im' => ImageMagick::class
public static $types = [
'gd' => 'Kirby\Image\Darkroom\GdLib',
'im' => 'Kirby\Image\Darkroom\ImageMagick'
];
public function __construct(
protected array $settings = []
) {
/**
* @var array
*/
protected $settings = [];
/**
* Darkroom constructor
*
* @param array $settings
*/
public function __construct(array $settings = [])
{
$this->settings = array_merge($this->defaults(), $settings);
}
@@ -33,9 +40,12 @@ class Darkroom
* 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 = []): object
public static function factory(string $type, array $settings = [])
{
if (isset(static::$types[$type]) === false) {
throw new Exception('Invalid Darkroom type');
@@ -47,6 +57,8 @@ class Darkroom
/**
* Returns the default thumb settings
*
* @return array
*/
protected function defaults(): array
{
@@ -60,13 +72,15 @@ class Darkroom
'quality' => 90,
'scaleHeight' => null,
'scaleWidth' => null,
'sharpen' => null,
'width' => null,
];
}
/**
* Normalizes all thumb options
*
* @param array $options
* @return array
*/
protected function options(array $options = []): array
{
@@ -94,13 +108,10 @@ class Darkroom
unset($options['bw']);
}
// normalize the sharpen option
if ($options['sharpen'] === true) {
$options['sharpen'] = 50;
if ($options['quality'] === null) {
$options['quality'] = $this->settings['quality'];
}
$options['quality'] ??= $this->settings['quality'];
return $options;
}
@@ -108,30 +119,28 @@ class Darkroom
* 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 = []): array
public function preprocess(string $file, array $options = [])
{
$options = $this->options($options);
$image = new Image($file);
$options['sourceWidth'] = $image->width();
$options['sourceHeight'] = $image->height();
$dimensions = $image->dimensions();
$thumbDimensions = $dimensions->thumb($options);
$dimensions = $image->dimensions();
$thumbDimensions = $dimensions->thumb($options);
$sourceWidth = $image->width();
$sourceHeight = $image->height();
$options['width'] = $thumbDimensions->width();
$options['height'] = $thumbDimensions->height();
// scale ratio compared to the source dimensions
$options['scaleWidth'] = Focus::ratio(
$options['width'],
$options['sourceWidth']
);
$options['scaleHeight'] = Focus::ratio(
$options['height'],
$options['sourceHeight']
);
$options['scaleWidth'] = $sourceWidth ? $options['width'] / $sourceWidth : null;
$options['scaleHeight'] = $sourceHeight ? $options['height'] / $sourceHeight : null;
return $options;
}
@@ -139,6 +148,10 @@ class Darkroom
/**
* 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
{

View File

@@ -5,7 +5,6 @@ namespace Kirby\Image\Darkroom;
use claviska\SimpleImage;
use Kirby\Filesystem\Mime;
use Kirby\Image\Darkroom;
use Kirby\Image\Focus;
/**
* GdLib
@@ -20,6 +19,10 @@ 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
{
@@ -33,9 +36,8 @@ class GdLib extends Darkroom
$image = $this->autoOrient($image, $options);
$image = $this->blur($image, $options);
$image = $this->grayscale($image, $options);
$image = $this->sharpen($image, $options);
$image->toFile($file, $mime, $options);
$image->toFile($file, $mime, $options['quality']);
return $options;
}
@@ -43,8 +45,12 @@ class GdLib extends Darkroom
/**
* Activates the autoOrient option in SimpleImage
* unless this is deactivated
*
* @param \claviska\SimpleImage $image
* @param $options
* @return \claviska\SimpleImage
*/
protected function autoOrient(SimpleImage $image, array $options): SimpleImage
protected function autoOrient(SimpleImage $image, $options)
{
if ($options['autoOrient'] === false) {
return $image;
@@ -55,48 +61,28 @@ class GdLib extends Darkroom
/**
* 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): SimpleImage
protected function resize(SimpleImage $image, array $options)
{
// just resize, no crop
if ($options['crop'] === false) {
return $image->resize($options['width'], $options['height']);
}
// crop based on focus point
if (Focus::isFocalPoint($options['crop']) === true) {
// get crop coords for focal point:
// if image needs to be cropped, crop before resizing
if ($focus = Focus::coords(
$options['crop'],
$options['sourceWidth'],
$options['sourceHeight'],
$options['width'],
$options['height']
)) {
$image->crop(
$focus['x1'],
$focus['y1'],
$focus['x2'],
$focus['y2']
);
}
return $image->thumbnail($options['width'], $options['height']);
}
// normal crop with crop anchor
return $image->thumbnail(
$options['width'],
$options['height'] ?? $options['width'],
$options['crop']
);
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): SimpleImage
protected function blur(SimpleImage $image, array $options)
{
if ($options['blur'] === false) {
return $image;
@@ -107,8 +93,12 @@ class GdLib extends Darkroom
/**
* 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): SimpleImage
protected function grayscale(SimpleImage $image, array $options)
{
if ($options['grayscale'] === false) {
return $image;
@@ -117,22 +107,13 @@ class GdLib extends Darkroom
return $image->desaturate();
}
/**
* Applies sharpening if activated in the options.
*/
protected function sharpen(SimpleImage $image, array $options): SimpleImage
{
if (is_int($options['sharpen']) === false) {
return $image;
}
return $image->sharpen($options['sharpen']);
}
/**
* Returns mime type based on `format` option
*
* @param array $options
* @return string|null
*/
protected function mime(array $options): string|null
protected function mime(array $options): ?string
{
if ($options['format'] === null) {
return null;

View File

@@ -5,7 +5,6 @@ namespace Kirby\Image\Darkroom;
use Exception;
use Kirby\Filesystem\F;
use Kirby\Image\Darkroom;
use Kirby\Image\Focus;
/**
* ImageMagick
@@ -21,49 +20,67 @@ 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): string|null
protected function autoOrient(string $file, array $options)
{
if ($options['autoOrient'] === true) {
return '-auto-orient';
}
return null;
}
/**
* Applies the blur settings
*
* @param string $file
* @param array $options
* @return string
*/
protected function blur(string $file, array $options): string|null
protected function blur(string $file, array $options)
{
if ($options['blur'] !== false) {
return '-blur ' . escapeshellarg('0x' . $options['blur']);
}
return null;
}
/**
* Keep animated gifs
*
* @param string $file
* @param array $options
* @return string
*/
protected function coalesce(string $file, array $options): string|null
protected function coalesce(string $file, array $options)
{
if (F::extension($file) === 'gif') {
return '-coalesce';
}
return null;
}
/**
* 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
{
$command = escapeshellarg($options['bin']);
// default is limiting to single-threading to keep CPU usage sane
$command .= ' -limit thread ' . escapeshellarg($options['threads']);
// limit to single-threading to keep CPU usage sane
$command .= ' -limit thread 1';
// add JPEG size hint to optimize CPU and memory usage
if (F::mime($file) === 'image/jpeg') {
// add hint only when downscaling
if ($options['scaleWidth'] < 1 && $options['scaleHeight'] < 1) {
$command .= ' -define ' . escapeshellarg(sprintf('jpeg:size=%dx%d', $options['width'], $options['height']));
}
}
// append input file
return $command . ' ' . escapeshellarg($file);
@@ -71,58 +88,53 @@ class ImageMagick extends Darkroom
/**
* Returns additional default parameters for imagemagick
*
* @return array
*/
protected function defaults(): array
{
return parent::defaults() + [
'bin' => 'convert',
'interlace' => false,
'threads' => 1,
];
}
/**
* Applies the correct settings for grayscale images
*
* @param string $file
* @param array $options
* @return string
*/
protected function grayscale(string $file, array $options): string|null
protected function grayscale(string $file, array $options)
{
if ($options['grayscale'] === true) {
return '-colorspace gray';
}
return null;
}
/**
* Applies sharpening if activated in the options.
*/
protected function sharpen(string $file, array $options): string|null
{
if (is_int($options['sharpen']) === false) {
return null;
}
$amount = max(1, min(100, $options['sharpen'])) / 100;
return '-sharpen ' . escapeshellarg('0x' . $amount);
}
/**
* 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): string|null
protected function interlace(string $file, array $options)
{
if ($options['interlace'] === true) {
return '-interlace line';
}
return null;
}
/**
* 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
@@ -139,7 +151,6 @@ class ImageMagick extends Darkroom
$command[] = $this->resize($file, $options);
$command[] = $this->quality($file, $options);
$command[] = $this->blur($file, $options);
$command[] = $this->sharpen($file, $options);
$command[] = $this->save($file, $options);
// remove all null values and join the parts
@@ -158,6 +169,10 @@ class ImageMagick extends Darkroom
/**
* Applies the correct JPEG compression quality settings
*
* @param string $file
* @param array $options
* @return string
*/
protected function quality(string $file, array $options): string
{
@@ -167,6 +182,10 @@ class ImageMagick extends Darkroom
/**
* 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
{
@@ -175,39 +194,20 @@ class ImageMagick extends Darkroom
return '-thumbnail ' . escapeshellarg(sprintf('%sx%s!', $options['width'], $options['height']));
}
// crop based on focus point
if (Focus::isFocalPoint($options['crop']) === true) {
if ($focus = Focus::coords(
$options['crop'],
$options['sourceWidth'],
$options['sourceHeight'],
$options['width'],
$options['height']
)) {
return sprintf(
'-crop %sx%s+%s+%s -resize %sx%s^',
$focus['width'],
$focus['height'],
$focus['x1'],
$focus['y1'],
$options['width'],
$options['height']
);
}
}
// translate the gravity option into something imagemagick understands
$gravity = match ($options['crop'] ?? null) {
$gravities = [
'top left' => 'NorthWest',
'top' => 'North',
'top right' => 'NorthEast',
'left' => 'West',
'center' => 'Center',
'right' => 'East',
'bottom left' => 'SouthWest',
'bottom' => 'South',
'bottom right' => 'SouthEast',
default => 'Center'
};
'bottom right' => 'SouthEast'
];
// translate the gravity option into something imagemagick understands
$gravity = $gravities[$options['crop']] ?? 'Center';
$command = '-thumbnail ' . escapeshellarg(sprintf('%sx%s^', $options['width'], $options['height']));
$command .= ' -gravity ' . escapeshellarg($gravity);
@@ -218,6 +218,10 @@ class ImageMagick extends Darkroom
/**
* Creates the option for the output file
*
* @param string $file
* @param array $options
* @return string
*/
protected function save(string $file, array $options): string
{
@@ -230,6 +234,10 @@ class ImageMagick extends Darkroom
/**
* Removes all metadata from the image
*
* @param string $file
* @param array $options
* @return string
*/
protected function strip(string $file, array $options): string
{

View File

@@ -2,8 +2,6 @@
namespace Kirby\Image;
use Kirby\Toolkit\Str;
/**
* The Dimension class is used to provide additional
* methods for images and possibly other objects with
@@ -18,15 +16,36 @@ use Kirby\Toolkit\Str;
*/
class Dimensions
{
public function __construct(
public int $width,
public int $height
) {
/**
* the height of the parent object
*
* @var int
*/
public $height = 0;
/**
* the width of the parent object
*
* @var int
*/
public $width = 0;
/**
* Constructor
*
* @param int $width
* @param int $height
*/
public function __construct(int $width, int $height)
{
$this->width = $width;
$this->height = $height;
}
/**
* Improved `var_dump` output
* @codeCoverageIgnore
*
* @return array
*/
public function __debugInfo(): array
{
@@ -35,6 +54,8 @@ class Dimensions
/**
* Echos the dimensions as width × height
*
* @return string
*/
public function __toString(): string
{
@@ -44,9 +65,11 @@ class Dimensions
/**
* Crops the dimensions by width and height
*
* @param int $width
* @param int|null $height
* @return $this
*/
public function crop(int $width, int|null $height = null): static
public function crop(int $width, int $height = null)
{
$this->width = $width;
$this->height = $width;
@@ -60,8 +83,10 @@ class Dimensions
/**
* Returns the height
*
* @return int
*/
public function height(): int
public function height()
{
return $this->height;
}
@@ -87,7 +112,7 @@ class Dimensions
* upscaled to fit the box if smaller
* @return $this object with recalculated dimensions
*/
public function fit(int $box, bool $force = false): static
public function fit(int $box, bool $force = false)
{
if ($this->width === 0 || $this->height === 0) {
$this->width = $box;
@@ -139,10 +164,8 @@ class Dimensions
* upscaled to fit the box if smaller
* @return $this object with recalculated dimensions
*/
public function fitHeight(
int|null $fit = null,
bool $force = false
): static {
public function fitHeight(int $fit = null, bool $force = false)
{
return $this->fitSize('height', $fit, $force);
}
@@ -155,11 +178,8 @@ class Dimensions
* upscaled to fit the box if smaller
* @return $this object with recalculated dimensions
*/
protected function fitSize(
string $ref,
int|null $fit = null,
bool $force = false
): static {
protected function fitSize(string $ref, int $fit = null, bool $force = false)
{
if ($fit === 0 || $fit === null) {
return $this;
}
@@ -197,10 +217,8 @@ class Dimensions
* upscaled to fit the box if smaller
* @return $this object with recalculated dimensions
*/
public function fitWidth(
int|null $fit = null,
bool $force = false
): static {
public function fitWidth(int $fit = null, bool $force = false)
{
return $this->fitSize('width', $fit, $force);
}
@@ -209,13 +227,11 @@ class Dimensions
*
* @param int|null $width the max height
* @param int|null $height the max width
* @param bool $force
* @return $this
*/
public function fitWidthAndHeight(
int|null $width = null,
int|null $height = null,
bool $force = false
): static {
public function fitWidthAndHeight(int $width = null, int $height = null, bool $force = false)
{
if ($this->width > $this->height) {
$this->fitWidth($width, $force);
@@ -237,8 +253,11 @@ class Dimensions
/**
* Detect the dimensions for an image file
*
* @param string $root
* @return static
*/
public static function forImage(string $root): static
public static function forImage(string $root)
{
if (file_exists($root) === false) {
return new static(0, 0);
@@ -250,8 +269,11 @@ class Dimensions
/**
* Detect the dimensions for a svg file
*
* @param string $root
* @return static
*/
public static function forSvg(string $root): static
public static function forSvg(string $root)
{
// avoid xml errors
libxml_use_internal_errors(true);
@@ -262,28 +284,13 @@ class Dimensions
$xml = simplexml_load_string($content);
if ($xml !== false) {
$attr = $xml->attributes();
$rawWidth = $attr->width;
$width = (int)$rawWidth;
$rawHeight = $attr->height;
$height = (int)$rawHeight;
// use viewbox values if direct attributes are 0
// or based on percentages
if (empty($attr->viewBox) === false) {
$box = explode(' ', $attr->viewBox);
// when using viewbox values, make sure to subtract
// first two box values from last two box values
// to retrieve the absolute dimensions
if (Str::endsWith($rawWidth, '%') === true || $width === 0) {
$width = (int)($box[2] ?? 0) - (int)($box[0] ?? 0);
}
if (Str::endsWith($rawHeight, '%') === true || $height === 0) {
$height = (int)($box[3] ?? 0) - (int)($box[1] ?? 0);
}
$attr = $xml->attributes();
$width = (int)($attr->width);
$height = (int)($attr->height);
if (($width === 0 || $height === 0) && empty($attr->viewBox) === false) {
$box = explode(' ', $attr->viewBox);
$width = (int)($box[2] ?? 0);
$height = (int)($box[3] ?? 0);
}
}
@@ -292,6 +299,8 @@ class Dimensions
/**
* Checks if the dimensions are landscape
*
* @return bool
*/
public function landscape(): bool
{
@@ -300,18 +309,20 @@ class Dimensions
/**
* Returns a string representation of the orientation
*
* @return string|false
*/
public function orientation(): string|false
public function orientation()
{
if (!$this->ratio()) {
return false;
}
if ($this->portrait() === true) {
if ($this->portrait()) {
return 'portrait';
}
if ($this->landscape() === true) {
if ($this->landscape()) {
return 'landscape';
}
@@ -320,6 +331,8 @@ class Dimensions
/**
* Checks if the dimensions are portrait
*
* @return bool
*/
public function portrait(): bool
{
@@ -336,6 +349,8 @@ class Dimensions
* // output: 1.5625
*
* </code>
*
* @return float
*/
public function ratio(): float
{
@@ -343,23 +358,24 @@ class Dimensions
return $this->width / $this->height;
}
return 0.0;
return 0;
}
/**
* Resizes image
* @param int|null $width
* @param int|null $height
* @param bool $force
* @return $this
*/
public function resize(
int|null $width = null,
int|null $height = null,
bool $force = false
): static {
public function resize(int $width = null, int $height = null, bool $force = false)
{
return $this->fitWidthAndHeight($width, $height, $force);
}
/**
* Checks if the dimensions are square
*
* @return bool
*/
public function square(): bool
{
@@ -369,9 +385,10 @@ class Dimensions
/**
* Resize and crop
*
* @param array $options
* @return $this
*/
public function thumb(array $options = []): static
public function thumb(array $options = [])
{
$width = $options['width'] ?? null;
$height = $options['height'] ?? null;
@@ -388,6 +405,8 @@ class Dimensions
/**
* Converts the dimensions object
* to a plain PHP array
*
* @return array
*/
public function toArray(): array
{
@@ -401,6 +420,8 @@ class Dimensions
/**
* Returns the width
*
* @return int
*/
public function width(): int
{

View File

@@ -16,33 +16,86 @@ use Kirby\Toolkit\V;
class Exif
{
/**
* The raw exif array
* the parent image object
* @var \Kirby\Image\Image
*/
protected array $data = [];
protected $image;
protected Camera|null $camera = null;
protected Location|null $location = null;
protected string|null $timestamp = null;
protected string|null $exposure = null;
protected string|null $aperture = null;
protected string|null $iso = null;
protected string|null $focalLength = null;
protected bool|null $isColor = null;
/**
* the raw exif array
* @var array
*/
protected $data = [];
public function __construct(
protected Image $image
) {
$this->data = $this->read();
$this->timestamp = $this->parseTimestamp();
$this->exposure = $this->data['ExposureTime'] ?? null;
$this->iso = $this->data['ISOSpeedRatings'] ?? null;
$this->focalLength = $this->parseFocalLength();
$this->aperture = $this->computed()['ApertureFNumber'] ?? null;
$this->isColor = V::accepted($this->computed()['IsColor'] ?? null);
/**
* the camera object with model and make
* @var Camera
*/
protected $camera;
/**
* the location object
* @var Location
*/
protected $location;
/**
* the timestamp
*
* @var string
*/
protected $timestamp;
/**
* the exposure value
*
* @var string
*/
protected $exposure;
/**
* the aperture value
*
* @var string
*/
protected $aperture;
/**
* iso value
*
* @var string
*/
protected $iso;
/**
* focal length
*
* @var string
*/
protected $focalLength;
/**
* color or black/white
* @var bool
*/
protected $isColor;
/**
* Constructor
*
* @param \Kirby\Image\Image $image
*/
public function __construct(Image $image)
{
$this->image = $image;
$this->data = $this->read();
$this->parse();
}
/**
* Returns the raw data array from the parser
*
* @return array
*/
public function data(): array
{
@@ -51,78 +104,106 @@ class Exif
/**
* Returns the Camera object
*
* @return \Kirby\Image\Camera|null
*/
public function camera(): Camera
public function camera()
{
return $this->camera ??= new Camera($this->data);
if ($this->camera !== null) {
return $this->camera;
}
return $this->camera = new Camera($this->data);
}
/**
* Returns the location object
*
* @return \Kirby\Image\Location|null
*/
public function location(): Location
public function location()
{
return $this->location ??= new Location($this->data);
if ($this->location !== null) {
return $this->location;
}
return $this->location = new Location($this->data);
}
/**
* Returns the timestamp
*
* @return string|null
*/
public function timestamp(): string|null
public function timestamp()
{
return $this->timestamp;
}
/**
* Returns the exposure
*
* @return string|null
*/
public function exposure(): string|null
public function exposure()
{
return $this->exposure;
}
/**
* Returns the aperture
*
* @return string|null
*/
public function aperture(): string|null
public function aperture()
{
return $this->aperture;
}
/**
* Returns the iso value
*
* @return int|null
*/
public function iso(): string|null
public function iso()
{
return $this->iso;
}
/**
* Checks if this is a color picture
*
* @return bool|null
*/
public function isColor(): bool|null
public function isColor()
{
return $this->isColor;
}
/**
* Checks if this is a bw picture
*
* @return bool|null
*/
public function isBW(): bool|null
public function isBW(): ?bool
{
return ($this->isColor !== null) ? $this->isColor === false : null;
}
/**
* Returns the focal length
*
* @return string|null
*/
public function focalLength(): string|null
public function focalLength()
{
return $this->focalLength;
}
/**
* Read the exif data of the image object if possible
*
* @return mixed
*/
protected function read(): array
{
@@ -138,6 +219,8 @@ class Exif
/**
* Get all computed data
*
* @return array
*/
protected function computed(): array
{
@@ -145,14 +228,27 @@ class Exif
}
/**
* Return the timestamp when the picture has been taken
* Pareses and stores all relevant exif data
*/
protected function parseTimestamp(): string
protected function parse()
{
$this->timestamp = $this->parseTimestamp();
$this->exposure = $this->data['ExposureTime'] ?? null;
$this->iso = $this->data['ISOSpeedRatings'] ?? null;
$this->focalLength = $this->parseFocalLength();
$this->aperture = $this->computed()['ApertureFNumber'] ?? null;
$this->isColor = V::accepted($this->computed()['IsColor'] ?? null);
}
/**
* Return the timestamp when the picture has been taken
*
* @return string|int
*/
protected function parseTimestamp()
{
if (isset($this->data['DateTimeOriginal']) === true) {
if ($time = strtotime($this->data['DateTimeOriginal'])) {
return (string)$time;
}
return strtotime($this->data['DateTimeOriginal']);
}
return $this->data['FileDateTime'] ?? $this->image->modified();
@@ -160,23 +256,24 @@ class Exif
/**
* Return the focal length
*
* @return string|null
*/
protected function parseFocalLength(): string|null
protected function parseFocalLength()
{
return
$this->data['FocalLength'] ??
$this->data['FocalLengthIn35mmFilm'] ??
null;
return $this->data['FocalLength'] ?? $this->data['FocalLengthIn35mmFilm'] ?? null;
}
/**
* Converts the object into a nicely readable array
*
* @return array
*/
public function toArray(): array
{
return [
'camera' => $this->camera()->toArray(),
'location' => $this->location()->toArray(),
'camera' => $this->camera() ? $this->camera()->toArray() : null,
'location' => $this->location() ? $this->location()->toArray() : null,
'timestamp' => $this->timestamp(),
'exposure' => $this->exposure(),
'aperture' => $this->aperture(),
@@ -188,7 +285,8 @@ class Exif
/**
* Improved `var_dump` output
* @codeCoverageIgnore
*
* @return array
*/
public function __debugInfo(): array
{

View File

@@ -1,110 +0,0 @@
<?php
namespace Kirby\Image;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
/**
* @package Kirby Image
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Focus
{
/**
* Generates crop coordinates based on focal point
*/
public static function coords(
string $crop,
int $sourceWidth,
int $sourceHeight,
int $width,
int $height
): array|null {
[$x, $y] = static::parse($crop);
// determine aspect ratios
$ratioSource = static::ratio($sourceWidth, $sourceHeight);
$ratioThumb = static::ratio($width, $height);
// no cropping necessary
if ($ratioSource == $ratioThumb) {
return null;
}
// defaults
$width = $sourceWidth;
$height = $sourceHeight;
if ($ratioThumb > $ratioSource) {
$height = $sourceWidth / $ratioThumb;
} else {
$width = $sourceHeight * $ratioThumb;
}
// calculate focus for original image
$x = $sourceWidth * $x;
$y = $sourceHeight * $y;
$x1 = max(0, $x - $width / 2);
$y1 = max(0, $y - $height / 2);
// off canvas?
if ($x1 + $width > $sourceWidth) {
$x1 = $sourceWidth - $width;
}
if ($y1 + $height > $sourceHeight) {
$y1 = $sourceHeight - $height;
}
return [
'x1' => (int)floor($x1),
'y1' => (int)floor($y1),
'x2' => (int)floor($x1 + $width),
'y2' => (int)floor($y1 + $height),
'width' => (int)floor($width),
'height' => (int)floor($height),
];
}
public static function isFocalPoint(string $value): bool
{
return Str::contains($value, '%') === true;
}
/**
* Transforms the focal point's string value (from content field)
* to a [x, y] array (values 0.0-1.0)
*/
public static function parse(string $value): array
{
// support for former Focus plugin
if (Str::startsWith($value, '{') === true) {
$focus = json_decode($value);
return [$focus->x, $focus->y];
}
preg_match_all("/(\d{1,3}\.?\d*)[%|,|\s]*/", $value, $points);
return A::map(
$points[1],
function ($point) {
$point = (float)$point;
$point = $point > 1 ? $point / 100 : $point;
return round($point, 3);
}
);
}
/**
* Calculates the image ratio
*/
public static function ratio(int $width, int $height): float
{
return $height !== 0 ? $width / $height : 0;
}
}

View File

@@ -2,8 +2,6 @@
namespace Kirby\Image;
use Kirby\Content\Content;
use Kirby\Exception\LogicException;
use Kirby\Filesystem\File;
use Kirby\Toolkit\Html;
@@ -24,10 +22,20 @@ use Kirby\Toolkit\Html;
*/
class Image extends File
{
protected Exif|null $exif = null;
protected Dimensions|null $dimensions = null;
/**
* @var \Kirby\Image\Exif|null
*/
protected $exif;
public static array $resizableTypes = [
/**
* @var \Kirby\Image\Dimensions|null
*/
protected $dimensions;
/**
* @var array
*/
public static $resizableTypes = [
'jpg',
'jpeg',
'gif',
@@ -35,7 +43,10 @@ class Image extends File
'webp'
];
public static array $viewableTypes = [
/**
* @var array
*/
public static $viewableTypes = [
'avif',
'jpg',
'jpeg',
@@ -47,8 +58,10 @@ class Image extends File
/**
* Validation rules to be used for `::match()`
*
* @var array
*/
public static array $validations = [
public static $validations = [
'maxsize' => ['size', 'max'],
'minsize' => ['size', 'min'],
'maxwidth' => ['width', 'max'],
@@ -60,6 +73,8 @@ class Image extends File
/**
* Returns the `<img>` tag for the image object
*
* @return string
*/
public function __toString(): string
{
@@ -68,19 +83,20 @@ class Image extends File
/**
* Returns the dimensions of the file if possible
*
* @return \Kirby\Image\Dimensions
*/
public function dimensions(): Dimensions
public function dimensions()
{
if ($this->dimensions !== null) {
return $this->dimensions;
}
if (in_array($this->mime(), [
'image/avif',
'image/gif',
'image/jpeg',
'image/jp2',
'image/png',
'image/gif',
'image/webp'
])) {
return $this->dimensions = Dimensions::forImage($this->root);
@@ -95,14 +111,18 @@ class Image extends File
/**
* Returns the exif object for this file (if image)
*
* @return \Kirby\Image\Exif
*/
public function exif(): Exif
public function exif()
{
return $this->exif ??= new Exif($this);
}
/**
* Returns the height of the asset
*
* @return int
*/
public function height(): int
{
@@ -111,29 +131,19 @@ class Image extends File
/**
* Converts the file to html
*
* @param array $attr
* @return string
*/
public function html(array $attr = []): string
{
// if no alt text explicitly provided,
// try to infer from model content file
if (
$this->model !== null &&
method_exists($this->model, 'content') === true &&
$this->model->content() instanceof Content &&
$this->model->content()->get('alt')->isNotEmpty() === true
) {
$attr['alt'] ??= $this->model->content()->get('alt')->value();
}
if ($url = $this->url()) {
return Html::img($url, $attr);
}
throw new LogicException('Calling Image::html() requires that the URL property is not null');
return Html::img($this->url(), $attr);
}
/**
* Returns the PHP imagesize array
*
* @return array
*/
public function imagesize(): array
{
@@ -142,6 +152,8 @@ class Image extends File
/**
* Checks if the dimensions of the asset are portrait
*
* @return bool
*/
public function isPortrait(): bool
{
@@ -150,6 +162,8 @@ class Image extends File
/**
* Checks if the dimensions of the asset are landscape
*
* @return bool
*/
public function isLandscape(): bool
{
@@ -158,6 +172,8 @@ class Image extends File
/**
* Checks if the dimensions of the asset are square
*
* @return bool
*/
public function isSquare(): bool
{
@@ -166,6 +182,8 @@ class Image extends File
/**
* Checks if the file is a resizable image
*
* @return bool
*/
public function isResizable(): bool
{
@@ -175,6 +193,8 @@ class Image extends File
/**
* Checks if a preview can be displayed for the file
* in the Panel or in the frontend
*
* @return bool
*/
public function isViewable(): bool
{
@@ -183,6 +203,8 @@ class Image extends File
/**
* Returns the ratio of the asset
*
* @return float
*/
public function ratio(): float
{
@@ -191,15 +213,19 @@ class Image extends File
/**
* Returns the orientation as string
* `landscape` | `portrait` | `square`
* landscape | portrait | square
*
* @return string
*/
public function orientation(): string|false
public function orientation(): string
{
return $this->dimensions()->orientation();
}
/**
* Converts the object to an array
*
* @return array
*/
public function toArray(): array
{
@@ -215,6 +241,8 @@ class Image extends File
/**
* Returns the width of the asset
*
* @return int
*/
public function width(): int
{

View File

@@ -14,8 +14,19 @@ namespace Kirby\Image;
*/
class Location
{
protected float|null $lat = null;
protected float|null $lng = null;
/**
* latitude
*
* @var float|null
*/
protected $lat;
/**
* longitude
*
* @var float|null
*/
protected $lng;
/**
* Constructor
@@ -24,43 +35,44 @@ class Location
*/
public function __construct(array $exif)
{
if (
isset($exif['GPSLatitude']) === true &&
if (isset($exif['GPSLatitude']) === true &&
isset($exif['GPSLatitudeRef']) === true &&
isset($exif['GPSLongitude']) === true &&
isset($exif['GPSLongitudeRef']) === true
) {
$this->lat = $this->gps(
$exif['GPSLatitude'],
$exif['GPSLatitudeRef']
);
$this->lng = $this->gps(
$exif['GPSLongitude'],
$exif['GPSLongitudeRef']
);
$this->lat = $this->gps($exif['GPSLatitude'], $exif['GPSLatitudeRef']);
$this->lng = $this->gps($exif['GPSLongitude'], $exif['GPSLongitudeRef']);
}
}
/**
* Returns the latitude
*
* @return float|null
*/
public function lat(): float|null
public function lat()
{
return $this->lat;
}
/**
* Returns the longitude
*
* @return float|null
*/
public function lng(): float|null
public function lng()
{
return $this->lng;
}
/**
* Converts the gps coordinates
*
* @param string|array $coord
* @param string $hemi
* @return float
*/
protected function gps(array $coord, string $hemi): float
protected function gps($coord, string $hemi): float
{
$degrees = count($coord) > 0 ? $this->num($coord[0]) : 0;
$minutes = count($coord) > 1 ? $this->num($coord[1]) : 0;
@@ -74,6 +86,9 @@ class Location
/**
* Converts coordinates to floats
*
* @param string $part
* @return float
*/
protected function num(string $part): float
{
@@ -88,6 +103,8 @@ class Location
/**
* Converts the object into a nicely readable array
*
* @return array
*/
public function toArray(): array
{
@@ -99,15 +116,18 @@ class Location
/**
* Echos the entire location as lat, lng
*
* @return string
*/
public function __toString(): string
{
return trim($this->lat() . ', ' . $this->lng(), ',');
return trim(trim($this->lat() . ', ' . $this->lng(), ','));
}
/**
* Improved `var_dump` output
* @codeCoverageIgnore
*
* @return array
*/
public function __debugInfo(): array
{

File diff suppressed because it is too large Load Diff