Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/flags transformation #115

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bundle/Resources/config/services/transformations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/TRANSFORMATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
78 changes: 78 additions & 0 deletions lib/Core/Provider/Cloudinary/TransformationHandler/Flags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Netgen\RemoteMedia\Core\Provider\Cloudinary\TransformationHandler;

use Netgen\RemoteMedia\Core\Transformation\HandlerInterface;

use function array_intersect;

/**
* Class Flags.
*
* 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 list of flags from the configuration
* and removes all those that are not supported.
*/
public function process(array $config = []): array
{
$flags = array_intersect($config, self::SUPPORTED_FLAGS);

return ['flags' => $flags];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Netgen\RemoteMedia\Tests\Core\Provider\Cloudinary\TransformationHandler;

use Netgen\RemoteMedia\Core\Provider\Cloudinary\TransformationHandler\Flags;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(Flags::class)]
final class FlagsTest extends TestCase
{
protected Flags $flags;

protected function setUp(): void
{
$this->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']]],
];
}
}