Skip to content

Commit

Permalink
Merge pull request #386 from thephpleague/3.x-getparam
Browse files Browse the repository at this point in the history
Add ManipulatorInterface::getParam().
  • Loading branch information
ADmad authored Jan 29, 2024
2 parents 62784e2 + 38765e6 commit 6b2a329
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 167 deletions.
9 changes: 4 additions & 5 deletions src/Manipulators/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use Intervention\Image\Origin;
use League\Glide\Manipulators\Helpers\Color;

/**
* @property string|null $bg
*/
class Background extends BaseManipulator
{
/**
Expand All @@ -20,11 +17,13 @@ class Background extends BaseManipulator
*/
public function run(ImageInterface $image): ImageInterface
{
if (is_null($this->bg)) {
$bg = (string) $this->getParam('bg');

if ('' === $bg) {
return $image;
}

$color = (new Color($this->bg))->formatted();
$color = (new Color($bg))->formatted();

if ($color) {
$new = $image->driver()->createImage($image->width(), $image->height())
Expand Down
10 changes: 4 additions & 6 deletions src/Manipulators/BaseManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class BaseManipulator implements ManipulatorInterface
/**
* The manipulation params.
*/
public array $params = [];
protected array $params = [];

/**
* Set the manipulation params.
Expand All @@ -27,16 +27,14 @@ public function setParams(array $params)

/**
* Get a specific manipulation param.
*
* @param string $name The manipulation name.
*
* @return mixed The manipulation value.
*/
public function __get($name)
public function getParam(string $name): mixed
{
if (array_key_exists($name, $this->params)) {
return $this->params[$name];
}

return null;
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/Manipulators/Blur.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string $blur
*/
class Blur extends BaseManipulator
{
/**
Expand Down Expand Up @@ -34,14 +31,15 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getBlur(): ?int
{
if (!is_numeric($this->blur)) {
return null;
}
$blur = $this->getParam('blur');

if ($this->blur < 0 or $this->blur > 100) {
if (!is_numeric($blur)
or $blur < 0
or $blur > 100
) {
return null;
}

return (int) $this->blur;
return (int) $blur;
}
}
20 changes: 9 additions & 11 deletions src/Manipulators/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
use League\Glide\Manipulators\Helpers\Color;
use League\Glide\Manipulators\Helpers\Dimension;

/**
* @property string $border
* @property string $dpr
*/
class Border extends BaseManipulator
{
/**
Expand Down Expand Up @@ -52,11 +48,12 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getBorder(ImageInterface $image): ?array
{
if (!$this->border) {
$border = (string) $this->getParam('border');
if ('' === $border) {
return null;
}

$values = explode(',', $this->border);
$values = explode(',', $border);

$width = $this->getWidth($image, $this->getDpr(), $values[0]);
$color = $this->getColor(isset($values[1]) ? $values[1] : 'ffffff');
Expand Down Expand Up @@ -118,15 +115,16 @@ public function getMethod(string $method): string
*/
public function getDpr(): float
{
if (!is_numeric($this->dpr)) {
return 1.0;
}
$dpr = $this->getParam('dpr');

if ($this->dpr < 0 or $this->dpr > 8) {
if (!is_numeric($dpr)
|| $dpr < 0
|| $dpr > 8
) {
return 1.0;
}

return (float) $this->dpr;
return (float) $dpr;
}

/**
Expand Down
15 changes: 7 additions & 8 deletions src/Manipulators/Brightness.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string|null $bri
*/
class Brightness extends BaseManipulator
{
/**
Expand Down Expand Up @@ -34,14 +31,16 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getBrightness(): ?int
{
if (null === $this->bri || !preg_match('/^-*[0-9]+$/', $this->bri)) {
return null;
}
$bri = (string) $this->getParam('bri');

if ($this->bri < -100 or $this->bri > 100) {
if ('' === $bri
or !preg_match('/^-*[0-9]+$/', $bri)
or $bri < -100
or $bri > 100
) {
return null;
}

return (int) $this->bri;
return (int) $bri;
}
}
15 changes: 7 additions & 8 deletions src/Manipulators/Contrast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string|null $con
*/
class Contrast extends BaseManipulator
{
/**
Expand Down Expand Up @@ -34,14 +31,16 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getContrast(): ?int
{
if (null === $this->con || !preg_match('/^-*[0-9]+$/', $this->con)) {
return null;
}
$con = (string) $this->getParam('con');

if ($this->con < -100 or $this->con > 100) {
if ('' === $con
or !preg_match('/^-*[0-9]+$/', $con)
or $con < -100
or $con > 100
) {
return null;
}

return (int) $this->con;
return (int) $con;
}
}
9 changes: 4 additions & 5 deletions src/Manipulators/Crop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string|null $crop
*/
class Crop extends BaseManipulator
{
/**
Expand Down Expand Up @@ -45,11 +42,13 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getCoordinates(ImageInterface $image): ?array
{
if (null === $this->crop) {
$crop = (string) $this->getParam('crop');

if ('' === $crop) {
return null;
}

$coordinates = explode(',', $this->crop);
$coordinates = explode(',', $crop);

if (4 !== count($coordinates)
or (!is_numeric($coordinates[0]))
Expand Down
17 changes: 8 additions & 9 deletions src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string $fm
* @property string $q
*/
class Encode extends BaseManipulator
{
/**
Expand Down Expand Up @@ -60,8 +56,10 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getFormat(ImageInterface $image): string
{
if (array_key_exists($this->fm, static::supportedFormats())) {
return $this->fm;
$fm = (string) $this->getParam('fm');

if ($fm && array_key_exists($fm, static::supportedFormats())) {
return $fm;
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
Expand Down Expand Up @@ -95,13 +93,14 @@ public static function supportedFormats(): array
public function getQuality(): int
{
$default = 90;
$q = $this->getParam('q');

if (!is_numeric($this->q)
or $this->q < 0 or $this->q > 100
if (!is_numeric($q)
or $q < 0 or $q > 100
) {
return $default;
}

return (int) $this->q;
return (int) $q;
}
}
17 changes: 5 additions & 12 deletions src/Manipulators/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string $filt
*/
class Filter extends BaseManipulator
{
/**
Expand All @@ -18,15 +15,11 @@ class Filter extends BaseManipulator
*/
public function run(ImageInterface $image): ImageInterface
{
if ('greyscale' === $this->filt) {
return $this->runGreyscaleFilter($image);
}

if ('sepia' === $this->filt) {
return $this->runSepiaFilter($image);
}

return $image;
return match ($this->getParam('filt')) {
'greyscale' => $this->runGreyscaleFilter($image),
'sepia' => $this->runSepiaFilter($image),
default => $image,
};
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Manipulators/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string $flip
*/
class Flip extends BaseManipulator
{
/**
Expand Down Expand Up @@ -43,8 +40,10 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getFlip(): ?string
{
if (in_array($this->flip, ['h', 'v', 'both'], true)) {
return $this->flip;
$flip = $this->getParam('flip');

if (in_array($flip, ['h', 'v', 'both'], true)) {
return $flip;
}

return null;
Expand Down
15 changes: 7 additions & 8 deletions src/Manipulators/Gamma.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string|null $gam
*/
class Gamma extends BaseManipulator
{
/**
Expand Down Expand Up @@ -34,14 +31,16 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getGamma(): ?float
{
if (null === $this->gam || !preg_match('/^[0-9]\.*[0-9]*$/', $this->gam)) {
return null;
}
$gam = (string) $this->getParam('gam');

if ($this->gam < 0.1 or $this->gam > 9.99) {
if ('' === $gam
|| !preg_match('/^[0-9]\.*[0-9]*$/', $gam)
|| $gam < 0.1
|| $gam > 9.99
) {
return null;
}

return (float) $this->gam;
return (float) $gam;
}
}
5 changes: 5 additions & 0 deletions src/Manipulators/ManipulatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ interface ManipulatorInterface
*/
public function setParams(array $params);

/**
* Get a specific manipulation param.
*/
public function getParam(string $name): mixed;

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

use Intervention\Image\Interfaces\ImageInterface;

/**
* @property string $or
*/
class Orientation extends BaseManipulator
{
/**
Expand Down Expand Up @@ -59,8 +56,10 @@ public function run(ImageInterface $image): ImageInterface
*/
public function getOrientation(): string
{
if (in_array($this->or, ['auto', '0', '90', '180', '270'], true)) {
return $this->or;
$or = $this->getParam('or');

if (in_array($or, ['auto', '0', '90', '180', '270'], true)) {
return $or;
}

return 'auto';
Expand Down
Loading

0 comments on commit 6b2a329

Please sign in to comment.