From b878020ba89ff5953450d3f776fa0cb089bbe67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20=C4=8Cupi=C4=87?= Date: Mon, 3 Feb 2025 12:56:15 +0100 Subject: [PATCH 1/3] Add transformation handler for flags --- .../config/services/transformations.yaml | 6 ++++ docs/TRANSFORMATIONS.md | 1 + .../TransformationHandler/Flags.php | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php diff --git a/bundle/Resources/config/services/transformations.yaml b/bundle/Resources/config/services/transformations.yaml index 9f40c7e4..b860c0e7 100644 --- a/bundle/Resources/config/services/transformations.yaml +++ b/bundle/Resources/config/services/transformations.yaml @@ -17,6 +17,12 @@ services: tags: - { name: netgen_remote_media.transformation_handler, alias: fit, provider: cloudinary } + netgen_remote_media.handler.cloudinary.flags: + class: Netgen\RemoteMedia\Core\Provider\Cloudinary\TransformationHandler\Flags + public: false + tags: + - { name: netgen_remote_media.transformation_handler, alias: flags, provider: cloudinary } + netgen_remote_media.handler.cloudinary.limit: class: Netgen\RemoteMedia\Core\Provider\Cloudinary\TransformationHandler\Limit public: false diff --git a/docs/TRANSFORMATIONS.md b/docs/TRANSFORMATIONS.md index 11cfe06e..970bdddc 100644 --- a/docs/TRANSFORMATIONS.md +++ b/docs/TRANSFORMATIONS.md @@ -6,6 +6,7 @@ Table of supported transformations from Cloudinary. | 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. | diff --git a/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php b/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php new file mode 100644 index 00000000..1846c82f --- /dev/null +++ b/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php @@ -0,0 +1,31 @@ + $config]; + } +} From 0de60f1302c7e9b66e29e2ea360e2632084dc15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20=C4=8Cupi=C4=87?= Date: Mon, 3 Feb 2025 13:02:50 +0100 Subject: [PATCH 2/3] Filter out unsupported flags --- .../TransformationHandler/Flags.php | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php b/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php index 1846c82f..77a30ed4 100644 --- a/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php +++ b/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php @@ -11,21 +11,66 @@ * * This transformation allows you to set flags which Cloudinary supports, * see: https://cloudinary.com/documentation/transformation_reference#fl_flag. + * If you provide unsupported flags, those will be ignored. */ final class Flags implements HandlerInterface { + private const SUPPORTED_FLAGS = [ + 'alternate', + 'animated', + 'any_format', + 'apng', + 'attachment', + 'awebp', + 'c2pa', + 'clip', + 'clip_evenodd', + 'cutter', + 'draco', + 'force_icc', + 'force_strip', + 'getinfo', + 'group4', + 'hlsv3', + 'ignore_aspect_ratio', + 'ignore_mask_channels', + 'immutable_cache', + 'keep_attribution', + 'keep_dar', + 'keep_iptc', + 'layer_apply', + 'lossy', + 'mono', + 'no_overflow', + 'no_stream', + 'original', + 'png8 / png24 / png32', + 'preserve_transparency', + 'progressive', + 'rasterize', + 'region_relative', + 'relative', + 'replace_image', + 'sanitize', + 'splice', + 'streaming_attachment', + 'strip_profile', + 'text_disallow_overflow', + 'text_no_trim', + 'tiff8_lzw', + 'tiled', + 'truncate_ts', + 'waveform', + ]; + /** - * Takes options from the configuration and returns - * properly configured array of options. + * Takes list of flags from the configuration + * and removes all those that are not supported. */ public function process(array $config = []): array { - $options = []; - - if (count($config) === 0) { - return $options; - } + $flags = array_intersect($config, self::SUPPORTED_FLAGS); - return ['flags' => $config]; + return ['flags' => $flags]; } } From b15e9e731c519d9060d1eb12c6fcc797e2de5275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20=C4=8Cupi=C4=87?= Date: Mon, 3 Feb 2025 16:21:26 +0100 Subject: [PATCH 3/3] Add tests and run CS fixer --- .../TransformationHandler/Flags.php | 2 + .../TransformationHandler/FlagsTest.php | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/lib/Core/Provider/Cloudinary/TransformationHandler/FlagsTest.php diff --git a/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php b/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php index 77a30ed4..064cf5e3 100644 --- a/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php +++ b/lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php @@ -6,6 +6,8 @@ use Netgen\RemoteMedia\Core\Transformation\HandlerInterface; +use function array_intersect; + /** * Class Flags. * diff --git a/tests/lib/Core/Provider/Cloudinary/TransformationHandler/FlagsTest.php b/tests/lib/Core/Provider/Cloudinary/TransformationHandler/FlagsTest.php new file mode 100644 index 00000000..183a556f --- /dev/null +++ b/tests/lib/Core/Provider/Cloudinary/TransformationHandler/FlagsTest.php @@ -0,0 +1,39 @@ +flags = new Flags(); + } + + #[DataProvider('validDataProvider')] + public function test(array $input, array $output): void + { + self::assertSame( + $output, + $this->flags->process($input), + ); + } + + public function validDataProvider(): array + { + return [ + [['rasterize', 'test'], ['flags' => ['rasterize']]], + [['any_format', 'rasterize', 'nonexisting'], ['flags' => ['any_format', 'rasterize']]], + [['rasterize', 'attachment', 'force_strip', 'relative'], ['flags' => ['rasterize', 'attachment', 'force_strip', 'relative']]], + ]; + } +}