From 531d5aa72afa964c63a217e6fe79db946921a03f Mon Sep 17 00:00:00 2001 From: Petar Jakopec Date: Fri, 8 Aug 2025 09:12:31 +0200 Subject: [PATCH 1/3] NGSTACK-986 add support for gravity transformation --- .../config/services/transformations.yaml | 6 +++ .../TransformationHandler/Gravity.php | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 lib/Core/Provider/Cloudinary/TransformationHandler/Gravity.php diff --git a/bundle/Resources/config/services/transformations.yaml b/bundle/Resources/config/services/transformations.yaml index b860c0e7..a624e5f3 100644 --- a/bundle/Resources/config/services/transformations.yaml +++ b/bundle/Resources/config/services/transformations.yaml @@ -23,6 +23,12 @@ services: tags: - { name: netgen_remote_media.transformation_handler, alias: flags, provider: cloudinary } + netgen_remote_media.handler.cloudinary.gravity: + class: Netgen\RemoteMedia\Core\Provider\Cloudinary\TransformationHandler\Gravity + public: false + tags: + - { name: netgen_remote_media.transformation_handler, alias: gravity, provider: cloudinary } + netgen_remote_media.handler.cloudinary.limit: class: Netgen\RemoteMedia\Core\Provider\Cloudinary\TransformationHandler\Limit public: false diff --git a/lib/Core/Provider/Cloudinary/TransformationHandler/Gravity.php b/lib/Core/Provider/Cloudinary/TransformationHandler/Gravity.php new file mode 100644 index 00000000..75028a25 --- /dev/null +++ b/lib/Core/Provider/Cloudinary/TransformationHandler/Gravity.php @@ -0,0 +1,37 @@ += 2 && $config[0] === 'auto' => ['gravity' => $config[0] . ':' . $config[1]], + count($config) === 1 => ['gravity' => $config[0]], + default => throw new TransformationHandlerFailedException(self::class), + }; + } +} From 521810fc97a665768298140c537a1b789d2c3bd8 Mon Sep 17 00:00:00 2001 From: Petar Jakopec Date: Fri, 8 Aug 2025 09:13:38 +0200 Subject: [PATCH 2/3] NGSTACK-986 add tests for gravity --- .../TransformationHandler/GravityTest.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/lib/Core/Provider/Cloudinary/TransformationHandler/GravityTest.php diff --git a/tests/lib/Core/Provider/Cloudinary/TransformationHandler/GravityTest.php b/tests/lib/Core/Provider/Cloudinary/TransformationHandler/GravityTest.php new file mode 100644 index 00000000..1d1d3185 --- /dev/null +++ b/tests/lib/Core/Provider/Cloudinary/TransformationHandler/GravityTest.php @@ -0,0 +1,72 @@ +gravity = new Gravity(); + } + + public function testGravityCompassPosition(): void + { + self::assertSame( + ['gravity' => 'south'], + $this->gravity->process(['south']), + ); + } + + public function testGravitySpecialPosition(): void + { + self::assertSame( + ['gravity' => 'face'], + $this->gravity->process(['face']), + ); + } + + public function testGravityObject(): void + { + self::assertSame( + ['gravity' => 'microwave'], + $this->gravity->process(['microwave']), + ); + } + + public function testGravityWithDefaultAuto(): void + { + self::assertSame( + [ + 'gravity' => 'auto', + ], + $this->gravity->process(['auto']), + ); + } + + public function testGravityWithAutoType(): void + { + self::assertSame( + [ + 'gravity' => 'auto:subject', + ], + $this->gravity->process(['auto', 'subject']), + ); + } + + public function testWithoutConfig(): void + { + $this->expectException(TransformationHandlerFailedException::class); + + $this->gravity->process(); + } +} From 360d332559ed2024279912184eb7ef97dffe91d8 Mon Sep 17 00:00:00 2001 From: Petar Jakopec Date: Fri, 8 Aug 2025 09:15:31 +0200 Subject: [PATCH 3/3] NGSTACK-986 update transformation docs --- docs/TRANSFORMATIONS.md | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/docs/TRANSFORMATIONS.md b/docs/TRANSFORMATIONS.md index 970bdddc..93e3eec3 100644 --- a/docs/TRANSFORMATIONS.md +++ b/docs/TRANSFORMATIONS.md @@ -1,25 +1,26 @@ Table of supported transformations from Cloudinary. -| Transformation name | Transformation alias | Description | -|:--------------------:|:--------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| -| Crop | crop | Crops the image if it has been cropped in the administration interface | -| Effect | effect | Applies the effect to change the visual appearance | -| Fill | fill | Exact given width and height while retaining the original aspect ratio, using only part of the image that fills the given dimensions if necessary | -| Fit | fit | The image is resized so that it takes up as much space as possible within a bounding box defined by the given width and height parameters. The original aspect ratio is retained and all of the original image is visible. | -| Flags | flags | This transformation allows you to set flags which Cloudinary supports, see: https://cloudinary.com/documentation/transformation_reference#fl_flag. -| Format | format | Defines in format should the media be delivered. | -| Lfill | lfill | Same as the fill mode but only if the original image is larger than the given limit (width and height). This mode doesn't scale up the image if your requested dimensions are bigger than the original image's | -| Limit | limit | Same as the fit mode but only if the original image is larger than the given limit (width and height), in which case the image is scaled down. This mode doesn't scale up the image if your requested dimensions are larger than the original image's. | -| Lpad | lpad | Same as the pad mode but only if the original image is larger than the given limit (width and height), in which case the image is scaled down to fill the given width and height while retaining the original aspect ratio. If the proportions of the original image do not match the given width and height, padding is added to the image to reach the required size. | -| Mfit | mfit | Same as the fit mode but only if the original image is smaller than the given minimum (width and height), in which case the image is scaled up. All of the original image is visible. | -| Mpad | mpad | Same as the pad mode but only if the original image is smaller than the given minimum (width and height), in which case the image is scaled up to fill the given width and height while retaining the original aspect ratio and with all of the original image visible. You can also specify the color of the background in the case that padding is added. | -| Named transformation | transformation | A named transformation is a set of image transformations that has been given a custom name for easy reference. | -| Opacity | opacity | Adjust the opacity of an image using the opacity transformation. Specify a value between 0 and 100, representing the percentage of transparency, where 100 means completely opaque and 0 is completely transparent. | -| Pad | pad | Resize the image to fill the given width and height while retaining the original aspect ratio and with all of the original image visible. If the proportions of the original image do not match the given width and height, padding is added to the image to reach the required size. You can also specify the color of the background in the case that padding is added. | -| Quality | quality | Set compression level to apply to an image as a value between 1 (smallest file size possible) and 100 (best visual quality). Automatic quality selection is also available | -| Resize | resize | Change the size of the image | -| Scale | scale | Change the size of the image exactly to the given width and height without necessarily retaining the original aspect ratio: all original image parts are visible but might be stretched or shrunk. | - | Watermark Text | watermark_text | Adds watermark text to an image, video or document. The text can be either specified through configuration (as a default) or through administration interface (it's stored in the location, same as crop settings. Supported config: `text`, `font_family`, `font_size`, `align` (left, right, top, bottom), `x`, `y`, `angle` (eg. 45 or -90), `opacity`, `density`, `color`. | +| Transformation name | Transformation alias | Description | +|:--------------------:|:--------------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| +| Crop | crop | Crops the image if it has been cropped in the administration interface | +| Effect | effect | Applies the effect to change the visual appearance | +| Fill | fill | Exact given width and height while retaining the original aspect ratio, using only part of the image that fills the given dimensions if necessary | +| Fit | fit | The image is resized so that it takes up as much space as possible within a bounding box defined by the given width and height parameters. The original aspect ratio is retained and all of the original image is visible. | +| Flags | flags | This transformation allows you to set flags which Cloudinary supports, see: https://cloudinary.com/documentation/transformation_reference#fl_flag. +| Format | format | Defines in format should the media be delivered. | +| Gravity | gravity | Determines which part of an asset to focus on, and thus which part of the asset to keep, when any part of the asset is cropped. For overlays, this setting determines where to place the overlay. | +| Lfill | lfill | Same as the fill mode but only if the original image is larger than the given limit (width and height). This mode doesn't scale up the image if your requested dimensions are bigger than the original image's | +| Limit | limit | Same as the fit mode but only if the original image is larger than the given limit (width and height), in which case the image is scaled down. This mode doesn't scale up the image if your requested dimensions are larger than the original image's. | +| Lpad | lpad | Same as the pad mode but only if the original image is larger than the given limit (width and height), in which case the image is scaled down to fill the given width and height while retaining the original aspect ratio. If the proportions of the original image do not match the given width and height, padding is added to the image to reach the required size. | +| Mfit | mfit | Same as the fit mode but only if the original image is smaller than the given minimum (width and height), in which case the image is scaled up. All of the original image is visible. | +| Mpad | mpad | Same as the pad mode but only if the original image is smaller than the given minimum (width and height), in which case the image is scaled up to fill the given width and height while retaining the original aspect ratio and with all of the original image visible. You can also specify the color of the background in the case that padding is added. | +| Named transformation | transformation | A named transformation is a set of image transformations that has been given a custom name for easy reference. | +| Opacity | opacity | Adjust the opacity of an image using the opacity transformation. Specify a value between 0 and 100, representing the percentage of transparency, where 100 means completely opaque and 0 is completely transparent. | +| Pad | pad | Resize the image to fill the given width and height while retaining the original aspect ratio and with all of the original image visible. If the proportions of the original image do not match the given width and height, padding is added to the image to reach the required size. You can also specify the color of the background in the case that padding is added. | +| Quality | quality | Set compression level to apply to an image as a value between 1 (smallest file size possible) and 100 (best visual quality). Automatic quality selection is also available | +| Resize | resize | Change the size of the image | +| Scale | scale | Change the size of the image exactly to the given width and height without necessarily retaining the original aspect ratio: all original image parts are visible but might be stretched or shrunk. | + | Watermark Text | watermark_text | Adds watermark text to an image, video or document. The text can be either specified through configuration (as a default) or through administration interface (it's stored in the location, same as crop settings. Supported config: `text`, `font_family`, `font_size`, `align` (left, right, top, bottom), `x`, `y`, `angle` (eg. 45 or -90), `opacity`, `density`, `color`. | For additional details on each of the transformation, consult the Cloudinary [documentation](http://cloudinary.com/documentation/image_transformations)