From 320936c0ddea812ca64fda8817586905d4d6bacb Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 13:19:46 +0200 Subject: [PATCH 01/17] Update TransformableImage.php --- src/TransformableImage.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index f4c9239..89d96b9 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -97,6 +97,8 @@ public function driver(string $driver) */ public function croppable($param = true) { + throw new \Exception("Test exception"); + if (is_numeric($param)) { $this->cropAspectRatio = $param; $param = true; From ac8c821ea8fad067fe8c28f063d52866a450ebc4 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 14:06:51 +0200 Subject: [PATCH 02/17] Remove test code --- src/TransformableImage.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 89d96b9..f4c9239 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -97,8 +97,6 @@ public function driver(string $driver) */ public function croppable($param = true) { - throw new \Exception("Test exception"); - if (is_numeric($param)) { $this->cropAspectRatio = $param; $param = true; From ca0fe53c54ea4189a65e9aadd4701ec221b85442 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 14:19:33 +0200 Subject: [PATCH 03/17] Update TransformableImage.php --- src/TransformableImage.php | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index f4c9239..3e8dbe6 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -59,6 +59,13 @@ trait TransformableImage */ private $quality = 90; + /** + * Indicates if the image is croppable. + * + * @var bool + */ + private $webp = false; + /** * The Intervention Image instance. * @@ -163,6 +170,26 @@ public function quality(int $quality) return $this; } + /** + * Specify if the underlying image should be orientated. + * Rotate the image to the orientation specified in Exif data, if any. Especially useful for smartphones. + * This method requires the exif extension to be enabled in your php settings. + * + * @throws \Exception + * + * @return $this + */ + public function webp() + { + // if (!extension_loaded('exif')) { + // throw new \Exception('The PHP exif extension must be enabled to use the autoOrientate method.'); + // } + + $this->webp = true; + + return $this; + } + /** * Transform the uploaded file. * @@ -191,6 +218,10 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData) $this->resizeImage(); } + if($this->webp) { + $this->covertToWebp(); + } + $this->image->save($uploadedFile->getPathName(), $this->quality, $uploadedFile->getClientOriginalExtension()); $this->image->destroy(); } @@ -229,4 +260,13 @@ private function orientateImage() { $this->image->orientate(); } + + /** + * Encodes the image to a webp format. + * + * @return void + */ + private function covertToWebp() { + $this->image->encode('webp'); + } } From 3b934409a205369abfda028f7fcbb76dca9616cd Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 14:29:15 +0200 Subject: [PATCH 04/17] Update TransformableImage.php --- src/TransformableImage.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 3e8dbe6..f9209ef 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -181,10 +181,6 @@ public function quality(int $quality) */ public function webp() { - // if (!extension_loaded('exif')) { - // throw new \Exception('The PHP exif extension must be enabled to use the autoOrientate method.'); - // } - $this->webp = true; return $this; @@ -219,7 +215,7 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData) } if($this->webp) { - $this->covertToWebp(); + $this->convertToWebp(); } $this->image->save($uploadedFile->getPathName(), $this->quality, $uploadedFile->getClientOriginalExtension()); @@ -266,7 +262,7 @@ private function orientateImage() * * @return void */ - private function covertToWebp() { + private function convertToWebp() { $this->image->encode('webp'); } } From 2e271660a01454913528f772fc95bbd57d777b7b Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 14:38:43 +0200 Subject: [PATCH 05/17] Update TransformableImage.php --- src/TransformableImage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index f9209ef..99d4f27 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -218,7 +218,7 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData) $this->convertToWebp(); } - $this->image->save($uploadedFile->getPathName(), $this->quality, $uploadedFile->getClientOriginalExtension()); + $this->image->save($uploadedFile->getPathName(), $this->quality, $this->webp === true ? 'webp' : $uploadedFile->getClientOriginalExtension()); $this->image->destroy(); } From cb43c7dd2bc1a40880498ee1e339a0e2ee1d85ac Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 14:51:44 +0200 Subject: [PATCH 06/17] Added: readme, comment and code style fixes --- README.md | 14 +++++++++++--- src/TransformableImage.php | 11 ++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ca386f7..788186c 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ AdvancedImage::make('Photo')->resize(600, 400), AdvancedImage::make('Photo')->resize(null, 300), ``` -*Note: this method uses [Intervention Image `resize()`](https://image.intervention.io/v2/api/resize) with the upsize and aspect ratio constraints.* +_Note: this method uses [Intervention Image `resize()`](https://image.intervention.io/v2/api/resize) with the upsize and aspect ratio constraints._ ### `autoOrientate()` @@ -118,7 +118,15 @@ This can be mandatory in some cases for the cropper to work properly. AdvancedImage::make('Photo')->autoOrientate(), ``` -*Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details.* +### `webp()` + +Specify if the underlying image should be encoded and formatted to the webp format. + +```php +AdvancedImage::make('Photo')->webp(), +``` + +_Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details._ ### `quality(int $quality)` @@ -130,4 +138,4 @@ This only applies to JPG format as PNG compression is lossless. The value must r AdvancedImage::make('Photo')->resize(600, 400)->quality(95), ``` -*Note: the quality will be passed to the [Intervention Image `save()`](https://image.intervention.io/v2/api/save) method.* +_Note: the quality will be passed to the [Intervention Image `save()`](https://image.intervention.io/v2/api/save) method._ diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 99d4f27..d84ae5c 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -171,11 +171,7 @@ public function quality(int $quality) } /** - * Specify if the underlying image should be orientated. - * Rotate the image to the orientation specified in Exif data, if any. Especially useful for smartphones. - * This method requires the exif extension to be enabled in your php settings. - * - * @throws \Exception + * Specify if the underlying image should be encoded to the webp format. * * @return $this */ @@ -214,7 +210,7 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData) $this->resizeImage(); } - if($this->webp) { + if ($this->webp) { $this->convertToWebp(); } @@ -262,7 +258,8 @@ private function orientateImage() * * @return void */ - private function convertToWebp() { + private function convertToWebp() + { $this->image->encode('webp'); } } From ce72e1ff8d26841ec946c3672a0335fd91bb8335 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 15:10:02 +0200 Subject: [PATCH 07/17] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 788186c..d3ef66c 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,8 @@ This can be mandatory in some cases for the cropper to work properly. AdvancedImage::make('Photo')->autoOrientate(), ``` +_Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details._ + ### `webp()` Specify if the underlying image should be encoded and formatted to the webp format. @@ -126,7 +128,7 @@ Specify if the underlying image should be encoded and formatted to the webp form AdvancedImage::make('Photo')->webp(), ``` -_Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details._ +_Note: For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support._ ### `quality(int $quality)` From 262b7897a88a98813ed706f11dbc452539a7a93b Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 17:01:41 +0200 Subject: [PATCH 08/17] Update TransformableImage.php --- src/TransformableImage.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index d84ae5c..b280e40 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -64,7 +64,9 @@ trait TransformableImage * * @var bool */ - private $webp = false; + private $outputFormat; + + private $allowedExtensions = ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url']; /** * The Intervention Image instance. @@ -170,15 +172,14 @@ public function quality(int $quality) return $this; } - /** - * Specify if the underlying image should be encoded to the webp format. - * - * @return $this - */ - public function webp() + public function convert($format) { - $this->webp = true; - + if (!in_array($format, $this->$allowedExtensions)) { + throw new \Exception("Unsupported output format: $format"); + } + + $this->outputFormat = $format; + return $this; } @@ -210,11 +211,11 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData) $this->resizeImage(); } - if ($this->webp) { - $this->convertToWebp(); + if ($this->outputFormat) { + $this->convertImage($this->outputFormat); } - $this->image->save($uploadedFile->getPathName(), $this->quality, $this->webp === true ? 'webp' : $uploadedFile->getClientOriginalExtension()); + $this->image->save($uploadedFile->getPathName(), $this->quality, $this->outputFormat ? $this->outputFormat : $uploadedFile->getClientOriginalExtension()); $this->image->destroy(); } @@ -254,12 +255,12 @@ private function orientateImage() } /** - * Encodes the image to a webp format. + * Encodes the image to the given format. * * @return void */ - private function convertToWebp() + private function convertImage($format) { - $this->image->encode('webp'); + $this->image->encode($format); } } From e82aaddf1b82fc7522aafd098f13172a5241416b Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:05:26 +0200 Subject: [PATCH 09/17] readme and comments --- README.md | 9 ++++++--- src/TransformableImage.php | 10 +++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d3ef66c..3a04519 100644 --- a/README.md +++ b/README.md @@ -122,13 +122,16 @@ _Note: PHP must be compiled in with `--enable-exif` to use this method. Windows ### `webp()` -Specify if the underlying image should be encoded and formatted to the webp format. +Specify the desired output image format. ```php -AdvancedImage::make('Photo')->webp(), +AdvancedImage::make('Photo')->convert('webp'), ``` -_Note: For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support._ +\_Note: + +1. To see all the supported formats that can be used to convert your files see [Intervention Image `encode()`]:(https://image.intervention.io/v2/api/encode +2. For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support.\_ ### `quality(int $quality)` diff --git a/src/TransformableImage.php b/src/TransformableImage.php index b280e40..4e25ef6 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -172,9 +172,17 @@ public function quality(int $quality) return $this; } + /** + * Specify the image format. + * This only applies to formats that are supported by Intervention. + * + * @throws \Exception + * + * @return $this + */ public function convert($format) { - if (!in_array($format, $this->$allowedExtensions)) { + if (!in_array($format, $this->allowedExtensions)) { throw new \Exception("Unsupported output format: $format"); } From c55718e612712c0c2cd2c89fefd2fbc99255f4f3 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:08:09 +0200 Subject: [PATCH 10/17] fix code style --- src/TransformableImage.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 4e25ef6..3b1c2d2 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -173,19 +173,19 @@ public function quality(int $quality) } /** - * Specify the image format. - * This only applies to formats that are supported by Intervention. + * Specify the desired output image format. + * This method sets the output format to be used by Intervention. * * @throws \Exception * * @return $this */ - public function convert($format) + public function convert($format) { if (!in_array($format, $this->allowedExtensions)) { throw new \Exception("Unsupported output format: $format"); } - + $this->outputFormat = $format; return $this; From ce45fed6d790b92662eace3c08788fc3051b8a35 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:09:27 +0200 Subject: [PATCH 11/17] fix code style error? --- src/TransformableImage.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 3b1c2d2..3937edf 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -186,8 +186,10 @@ public function convert($format) throw new \Exception("Unsupported output format: $format"); } + $this->outputFormat = $format; - + + return $this; } From 2534a453aa0818b90485470fb73f99859605b784 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:10:57 +0200 Subject: [PATCH 12/17] fix code style?!?!??! --- src/TransformableImage.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 3937edf..70c8f44 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -186,10 +186,8 @@ public function convert($format) throw new \Exception("Unsupported output format: $format"); } - $this->outputFormat = $format; - - + return $this; } From 68a32981460d25118b89ba300e797426c51b9fdf Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:12:48 +0200 Subject: [PATCH 13/17] fix styleci --- src/TransformableImage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index 70c8f44..e5da808 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -185,9 +185,9 @@ public function convert($format) if (!in_array($format, $this->allowedExtensions)) { throw new \Exception("Unsupported output format: $format"); } - + $this->outputFormat = $format; - + return $this; } From ee15a7b3eb38ab38005ca49bc31e636ff3da2998 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:14:29 +0200 Subject: [PATCH 14/17] Added comment to allowedExtensions --- src/TransformableImage.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index e5da808..bf0e32d 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -66,6 +66,11 @@ trait TransformableImage */ private $outputFormat; + /** + * Indicates the allowed extensions for the output format. + * + * @var array + */ private $allowedExtensions = ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url']; /** From ecd68711eb10cafe6205daf7db05cae36f1b5b8e Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:20:39 +0200 Subject: [PATCH 15/17] Update README.md --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3a04519..cd98b22 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ AdvancedImage::make('Photo')->autoOrientate(), _Note: PHP must be compiled in with `--enable-exif` to use this method. Windows users must also have the mbstring extension enabled. See [the Intervention Image documentation](https://image.intervention.io/v2/api/orientate) for more details._ -### `webp()` +### `convert(string $format)` Specify the desired output image format. @@ -128,10 +128,8 @@ Specify the desired output image format. AdvancedImage::make('Photo')->convert('webp'), ``` -\_Note: - -1. To see all the supported formats that can be used to convert your files see [Intervention Image `encode()`]:(https://image.intervention.io/v2/api/encode -2. For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support.\_ +_Note 1: To see all the supported formats that can be used to convert your files see [Intervention Image `encode()`]:(https://image.intervention.io/v2/api/encode_ +_Note 2: For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support._ ### `quality(int $quality)` From 74c153505a0baa24814332f10b4fb65f1c8ed431 Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Fri, 18 Aug 2023 18:26:51 +0200 Subject: [PATCH 16/17] fix readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd98b22..fac02da 100644 --- a/README.md +++ b/README.md @@ -128,8 +128,10 @@ Specify the desired output image format. AdvancedImage::make('Photo')->convert('webp'), ``` -_Note 1: To see all the supported formats that can be used to convert your files see [Intervention Image `encode()`]:(https://image.intervention.io/v2/api/encode_ -_Note 2: For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support._ +_Note: _ + +- _To see all the supported formats that can be used to convert your files see [Intervention Image `encode()`]:(https://image.intervention.io/v2/api/encode._ +- _For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support._ ### `quality(int $quality)` From c898e0201c52a443143916443a53eba1fcbe9f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Tessier?= Date: Mon, 28 Aug 2023 20:36:50 +0200 Subject: [PATCH 17/17] Refactor TransformableImage for new convert method --- src/TransformableImage.php | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/TransformableImage.php b/src/TransformableImage.php index bf0e32d..96c0004 100644 --- a/src/TransformableImage.php +++ b/src/TransformableImage.php @@ -60,19 +60,12 @@ trait TransformableImage private $quality = 90; /** - * Indicates if the image is croppable. + * The format of the resulting image. * - * @var bool + * @var string */ private $outputFormat; - /** - * Indicates the allowed extensions for the output format. - * - * @var array - */ - private $allowedExtensions = ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url']; - /** * The Intervention Image instance. * @@ -185,9 +178,12 @@ public function quality(int $quality) * * @return $this */ - public function convert($format) + public function convert(string $format) { - if (!in_array($format, $this->allowedExtensions)) { + /** + * @See https://image.intervention.io/v2/api/encode + */ + if (!in_array($format, ['jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'data-url'])) { throw new \Exception("Unsupported output format: $format"); } @@ -228,7 +224,7 @@ public function transformImage(UploadedFile $uploadedFile, ?object $cropperData) $this->convertImage($this->outputFormat); } - $this->image->save($uploadedFile->getPathName(), $this->quality, $this->outputFormat ? $this->outputFormat : $uploadedFile->getClientOriginalExtension()); + $this->image->save($uploadedFile->getPathName(), $this->quality, $this->outputFormat ?? $uploadedFile->getClientOriginalExtension()); $this->image->destroy(); } @@ -268,12 +264,12 @@ private function orientateImage() } /** - * Encodes the image to the given format. + * Encode the image to the given format. * * @return void */ - private function convertImage($format) + private function convertImage(string $format) { - $this->image->encode($format); + $this->image->encode($format, $this->quality); } }