Skip to content

Commit

Permalink
Define API params in order to be able to filter
Browse files Browse the repository at this point in the history
  • Loading branch information
deluxetom committed Dec 17, 2024
1 parent e8cead3 commit 436f688
Show file tree
Hide file tree
Showing 22 changed files with 270 additions and 123 deletions.
43 changes: 43 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', // presets
'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,32 @@ 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
{
$params = [];

array_walk($this->manipulators, function (ManipulatorInterface $manipulator) use (&$params) {
foreach ($manipulator->getApiParams() as $param) {
$params[] = $param;
}
});

return $this->apiParams = array_values(array_unique(array_merge(self::GLOBAL_API_PARAMS, $params)));
}

/**
* 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
7 changes: 7 additions & 0 deletions src/Manipulators/BaseManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function getParam(string $name): mixed
: null;
}

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

/**
* Perform the image manipulation.
*
Expand Down
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.
*
* @rerun 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 436f688

Please sign in to comment.