Skip to content

Commit

Permalink
Merge pull request #413 from deluxetom/3.x-api-params
Browse files Browse the repository at this point in the history
Define API params in order to be able to filter correctly
  • Loading branch information
ADmad authored Dec 18, 2024
2 parents cfdfbd9 + 71685e0 commit 3cb6c58
Show file tree
Hide file tree
Showing 23 changed files with 264 additions and 135 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
tests:
runs-on: ubuntu-24.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
uses: codecov/codecov-action@v3

coding-style:
runs-on: ubuntu-24.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

Expand All @@ -61,7 +61,7 @@ jobs:
vendor/bin/php-cs-fixer fix --dry-run --diff --allow-risky=yes
static-analysis:
runs-on: ubuntu-24.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

Expand Down
41 changes: 41 additions & 0 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

class Api implements ApiInterface
{
public const GLOBAL_API_PARAMS = [
'p', // preset
'q', // quality
'fm', // format
's', // signature
];

/**
* Intervention image manager.
*/
Expand All @@ -23,6 +30,13 @@ class Api implements ApiInterface
*/
protected array $manipulators;

/**
* API parameters.
*
* @var list<string>
*/
protected array $apiParams;

/**
* Create API instance.
*
Expand All @@ -33,6 +47,7 @@ public function __construct(ImageManager $imageManager, array $manipulators)
{
$this->setImageManager($imageManager);
$this->setManipulators($manipulators);
$this->setApiParams();
}

/**
Expand Down Expand Up @@ -116,4 +131,30 @@ public function encode(ImageInterface $image, array $params): string

return $encoded->toString();
}

/**
* Sets the API parameters for all manipulators.
*
* @return list<string>
*/
public function setApiParams(): array
{
$this->apiParams = self::GLOBAL_API_PARAMS;

foreach ($this->manipulators as $manipulator) {
$this->apiParams = array_merge($this->apiParams, $manipulator->getApiParams());
}

return $this->apiParams = array_values(array_unique($this->apiParams));
}

/**
* Retun the list of API params.
*
* @return list<string>
*/
public function getApiParams(): array
{
return $this->apiParams;
}
}
7 changes: 7 additions & 0 deletions src/Api/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ interface ApiInterface
* @return string Manipulated image binary data.
*/
public function run(string $source, array $params): string;

/**
* Collection of API parameters.
*
* @return list<string>
*/
public function getApiParams(): array;
}
5 changes: 5 additions & 0 deletions src/Manipulators/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class Background extends BaseManipulator
{
public function getApiParams(): array
{
return ['bg'];
}

/**
* Perform background image manipulation.
*
Expand Down
9 changes: 0 additions & 9 deletions src/Manipulators/BaseManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace League\Glide\Manipulators;

use Intervention\Image\Interfaces\ImageInterface;

abstract class BaseManipulator implements ManipulatorInterface
{
/**
Expand Down Expand Up @@ -36,11 +34,4 @@ public function getParam(string $name): mixed
? $this->params[$name]
: null;
}

/**
* Perform the image manipulation.
*
* @return ImageInterface The manipulated image.
*/
abstract public function run(ImageInterface $image): ImageInterface;
}
5 changes: 5 additions & 0 deletions src/Manipulators/Blur.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Blur extends BaseManipulator
{
public function getApiParams(): array
{
return ['blur'];
}

/**
* Perform blur image manipulation.
*
Expand Down
9 changes: 7 additions & 2 deletions src/Manipulators/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

class Border extends BaseManipulator
{
public function getApiParams(): array
{
return ['border'];
}

/**
* Perform border image manipulation.
*
Expand Down Expand Up @@ -50,8 +55,8 @@ public function getBorder(ImageInterface $image): ?array
$values = explode(',', $border);

$width = $this->getWidth($image, $this->getDpr(), $values[0]);
$color = $this->getColor(isset($values[1]) ? $values[1] : 'ffffff');
$method = $this->getMethod(isset($values[2]) ? $values[2] : 'overlay');
$color = $this->getColor($values[1] ?? 'ffffff');
$method = $this->getMethod($values[2] ?? 'overlay');

if (null !== $width) {
return [$width, $color, $method];
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Brightness.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Brightness extends BaseManipulator
{
public function getApiParams(): array
{
return ['bri'];
}

/**
* Perform brightness image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Contrast.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Contrast extends BaseManipulator
{
public function getApiParams(): array
{
return ['con'];
}

/**
* Perform contrast image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Crop.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Crop extends BaseManipulator
{
public function getApiParams(): array
{
return ['crop'];
}

/**
* Perform crop image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Filter extends BaseManipulator
{
public function getApiParams(): array
{
return ['filt'];
}

/**
* Perform filter image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Flip extends BaseManipulator
{
public function getApiParams(): array
{
return ['flip'];
}

/**
* Perform flip image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Gamma.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Gamma extends BaseManipulator
{
public function getApiParams(): array
{
return ['gam'];
}

/**
* Perform gamma image manipulation.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Manipulators/ManipulatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public function setParams(array $params): static;
*/
public function getParam(string $name): mixed;

/**
* Get the names of the manipulator API parameters.
*
* @return list<string>
*/
public function getApiParams(): array;

/**
* Perform the image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Orientation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Orientation extends BaseManipulator
{
public function getApiParams(): array
{
return ['or'];
}

/**
* Perform orientation image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Pixelate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Pixelate extends BaseManipulator
{
public function getApiParams(): array
{
return ['pixel'];
}

/**
* Perform pixelate image manipulation.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Sharpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Sharpen extends BaseManipulator
{
public function getApiParams(): array
{
return ['sharp'];
}

/**
* Perform sharpen image manipulation.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Manipulators/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function __construct(?int $maxImageSize = null)
$this->maxImageSize = $maxImageSize;
}

public function getApiParams(): array
{
return ['w', 'f', 'fit', 'dpr'];
}

/**
* Set the maximum image size.
*
Expand Down Expand Up @@ -427,7 +432,7 @@ public function getCrop(): array
}

if (preg_match('/^crop-([\d]{1,3})-([\d]{1,3})(?:-([\d]{1,3}(?:\.\d+)?))*$/', $fit, $matches)) {
$matches[3] = isset($matches[3]) ? $matches[3] : 1;
$matches[3] = $matches[3] ?? 1;

if ($matches[1] > 100 || $matches[2] > 100 || $matches[3] > 100) {
return [50, 50, 1.0];
Expand Down
5 changes: 5 additions & 0 deletions src/Manipulators/Watermark.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function __construct(?FilesystemOperator $watermarks = null, string $wate
$this->setWatermarksPathPrefix($watermarksPathPrefix);
}

public function getApiParams(): array
{
return ['mark', 'markw', 'markh', 'markx', 'marky', 'markpad', 'markfit', 'markpos', 'markalpha', 'dpr'];
}

/**
* Set the watermarks file system.
*
Expand Down
11 changes: 5 additions & 6 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ public function getCachePath(string $path, array $params = []): string
}

if ($this->cacheWithFileExtensions) {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
$ext = isset($params['fm']) ? $params['fm'] : pathinfo($path, PATHINFO_EXTENSION);
$ext = $params['fm'] ?? pathinfo($path, PATHINFO_EXTENSION);
$ext = 'pjpg' === $ext ? 'jpg' : $ext;
$cachedPath .= '.'.$ext;
}
Expand Down Expand Up @@ -481,23 +480,23 @@ public function getPresets(): array
/**
* Get all image manipulations params, including defaults and presets.
*
* @param array $params Image manipulation params.
* @param array<string, string|int> $params Image manipulation params.
*
* @return array All image manipulation params.
* @return array<string, string|int> All image manipulation params.
*/
public function getAllParams(array $params): array
{
$all = $this->defaults;

if (isset($params['p'])) {
foreach (explode(',', $params['p']) as $preset) {
foreach (explode(',', (string) $params['p']) as $preset) {
if (isset($this->presets[$preset])) {
$all = array_merge($all, $this->presets[$preset]);
}
}
}

return array_merge($all, $params);
return array_filter(array_merge($all, $params), fn ($key) => in_array($key, $this->api->getApiParams(), true), ARRAY_FILTER_USE_KEY);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public function testGetManipulators()
$this->assertEquals([], $this->api->getManipulators());
}

public function testGetApiParams(): void
{
$manipulator1 = \Mockery::mock(ManipulatorInterface::class, function ($mock) {
$mock->shouldReceive('getApiParams')->andReturn(['foo', 'bar']);
});
$manipulator2 = \Mockery::mock(ManipulatorInterface::class, function ($mock) {
$mock->shouldReceive('getApiParams')->andReturn(['foo', 'baz']);
});

$api = new Api(ImageManager::gd(), [$manipulator1, $manipulator2]);
$this->assertEquals(array_merge(Api::GLOBAL_API_PARAMS, ['foo', 'bar', 'baz']), $api->getApiParams());
}

public function testRun()
{
$image = \Mockery::mock(ImageInterface::class, function ($mock) {
Expand All @@ -77,6 +90,7 @@ public function testRun()
$manipulator = \Mockery::mock(ManipulatorInterface::class, function ($mock) use ($image) {
$mock->shouldReceive('setParams')->with([]);
$mock->shouldReceive('run')->andReturn($image);
$mock->shouldReceive('getApiParams')->andReturn(['p', 'q', 'fm', 's']);
});

$api = new Api($manager, [$manipulator]);
Expand Down
Loading

0 comments on commit 3cb6c58

Please sign in to comment.