From c2036f2a5fc68abd0fbf656f1fa3dcdf60d8aeb9 Mon Sep 17 00:00:00 2001 From: Flo Kosiol Date: Mon, 29 Nov 2021 21:55:01 +0100 Subject: [PATCH] support multiple mime types #64 --- src/Focus/GdLib.php | 60 ++++++++++++++++++++++-- src/Focus/ImageMagick.php | 96 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 148 insertions(+), 8 deletions(-) diff --git a/src/Focus/GdLib.php b/src/Focus/GdLib.php index 0a2b472..fdebb9f 100644 --- a/src/Focus/GdLib.php +++ b/src/Focus/GdLib.php @@ -2,18 +2,24 @@ namespace Flokosiol\Focus; -ini_set('memory_limit', '512M'); - use claviska\SimpleImage; -use Kirby\Image\Image; -use Kirby\Image\Dimensions; +use Kirby\Filesystem\Mime; use Kirby\Image\Darkroom; +use Kirby\Image\Image; 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); + $mime = $this->mime($options); // original image dimension for focus cropping $originalImage = new Image($file); @@ -30,11 +36,19 @@ public function process(string $file, array $options = []): array $image = $this->blur($image, $options); $image = $this->grayscale($image, $options); - $image->toFile($file, null, $options['quality']); + $image->toFile($file, $mime, $options['quality']); 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 +58,13 @@ protected function autoOrient(SimpleImage $image, $options) 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) { @@ -60,6 +81,13 @@ protected function resize(SimpleImage $image, array $options) } + /** + * 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) { @@ -69,6 +97,13 @@ protected function blur(SimpleImage $image, array $options) 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) { @@ -77,4 +112,19 @@ protected function grayscale(SimpleImage $image, array $options) 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']); + } } \ No newline at end of file diff --git a/src/Focus/ImageMagick.php b/src/Focus/ImageMagick.php index 87eac9b..0ca8226 100644 --- a/src/Focus/ImageMagick.php +++ b/src/Focus/ImageMagick.php @@ -3,13 +3,20 @@ namespace Flokosiol\Focus; use Exception; -use Kirby\Image\Image; -use Kirby\Image\Dimensions; +use Kirby\Filesystem\F; use Kirby\Image\Darkroom; -use Kirby\Toolkit\F; +use Kirby\Image\Image; 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) { @@ -17,6 +24,13 @@ protected function autoOrient(string $file, array $options) } } + /** + * Applies the blur settings + * + * @param string $file + * @param array $options + * @return string + */ protected function blur(string $file, array $options) { if ($options['blur'] !== false) { @@ -24,6 +38,13 @@ protected function blur(string $file, array $options) } } + /** + * Keep animated gifs + * + * @param string $file + * @param array $options + * @return string + */ protected function coalesce(string $file, array $options) { if (F::extension($file) === 'gif') { @@ -31,11 +52,23 @@ protected function coalesce(string $file, array $options) } } + /** + * 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() + [ @@ -44,6 +77,13 @@ protected function defaults(): array ]; } + /** + * 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) { @@ -51,6 +91,14 @@ protected function grayscale(string $file, array $options) } } + /** + * 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) { @@ -58,6 +106,15 @@ protected function interlace(string $file, array $options) } } + /** + * 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); @@ -96,11 +153,26 @@ public function process(string $file, array $options = []): array 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 @@ -135,11 +207,29 @@ protected function resize(string $file, array $options): string 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 { + if ($options['format'] !== null) { + $file = basename($file) . '.' . $options['format']; + } 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';