diff --git a/src/Manipulators/Background.php b/src/Manipulators/Background.php index 97e5a87..055f36d 100644 --- a/src/Manipulators/Background.php +++ b/src/Manipulators/Background.php @@ -6,9 +6,6 @@ use Intervention\Image\Origin; use League\Glide\Manipulators\Helpers\Color; -/** - * @property string|null $bg - */ class Background extends BaseManipulator { /** @@ -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()) diff --git a/src/Manipulators/BaseManipulator.php b/src/Manipulators/BaseManipulator.php index 5394d22..d26ccd1 100644 --- a/src/Manipulators/BaseManipulator.php +++ b/src/Manipulators/BaseManipulator.php @@ -9,7 +9,7 @@ abstract class BaseManipulator implements ManipulatorInterface /** * The manipulation params. */ - public array $params = []; + protected array $params = []; /** * Set the manipulation params. @@ -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; } /** diff --git a/src/Manipulators/Blur.php b/src/Manipulators/Blur.php index cfc3ae9..c5ecccd 100644 --- a/src/Manipulators/Blur.php +++ b/src/Manipulators/Blur.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $blur - */ class Blur extends BaseManipulator { /** @@ -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; } } diff --git a/src/Manipulators/Border.php b/src/Manipulators/Border.php index 9171c57..3f8ffa4 100644 --- a/src/Manipulators/Border.php +++ b/src/Manipulators/Border.php @@ -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 { /** @@ -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'); @@ -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; } /** diff --git a/src/Manipulators/Brightness.php b/src/Manipulators/Brightness.php index a6617b2..6051e85 100644 --- a/src/Manipulators/Brightness.php +++ b/src/Manipulators/Brightness.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string|null $bri - */ class Brightness extends BaseManipulator { /** @@ -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; } } diff --git a/src/Manipulators/Contrast.php b/src/Manipulators/Contrast.php index 3aa5124..71418c7 100644 --- a/src/Manipulators/Contrast.php +++ b/src/Manipulators/Contrast.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string|null $con - */ class Contrast extends BaseManipulator { /** @@ -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; } } diff --git a/src/Manipulators/Crop.php b/src/Manipulators/Crop.php index 12ebcbe..1e85bd8 100644 --- a/src/Manipulators/Crop.php +++ b/src/Manipulators/Crop.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string|null $crop - */ class Crop extends BaseManipulator { /** @@ -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])) diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index f951446..8ccf03b 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -7,10 +7,6 @@ use Intervention\Image\ImageManager; use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $fm - * @property string $q - */ class Encode extends BaseManipulator { /** @@ -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 */ @@ -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; } } diff --git a/src/Manipulators/Filter.php b/src/Manipulators/Filter.php index ebcc83f..0f06398 100644 --- a/src/Manipulators/Filter.php +++ b/src/Manipulators/Filter.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $filt - */ class Filter extends BaseManipulator { /** @@ -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, + }; } /** diff --git a/src/Manipulators/Flip.php b/src/Manipulators/Flip.php index 9ba5be6..9eedea2 100644 --- a/src/Manipulators/Flip.php +++ b/src/Manipulators/Flip.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $flip - */ class Flip extends BaseManipulator { /** @@ -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; diff --git a/src/Manipulators/Gamma.php b/src/Manipulators/Gamma.php index 46bf75a..b0fe3ab 100644 --- a/src/Manipulators/Gamma.php +++ b/src/Manipulators/Gamma.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string|null $gam - */ class Gamma extends BaseManipulator { /** @@ -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; } } diff --git a/src/Manipulators/ManipulatorInterface.php b/src/Manipulators/ManipulatorInterface.php index 4c3f0d8..d04c266 100644 --- a/src/Manipulators/ManipulatorInterface.php +++ b/src/Manipulators/ManipulatorInterface.php @@ -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. * diff --git a/src/Manipulators/Orientation.php b/src/Manipulators/Orientation.php index 3c2b50f..a2fecf3 100644 --- a/src/Manipulators/Orientation.php +++ b/src/Manipulators/Orientation.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $or - */ class Orientation extends BaseManipulator { /** @@ -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'; diff --git a/src/Manipulators/Pixelate.php b/src/Manipulators/Pixelate.php index fa4ccea..2fc3cf5 100644 --- a/src/Manipulators/Pixelate.php +++ b/src/Manipulators/Pixelate.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $pixel - */ class Pixelate extends BaseManipulator { /** @@ -34,14 +31,15 @@ public function run(ImageInterface $image): ImageInterface */ public function getPixelate(): ?int { - if (!is_numeric($this->pixel)) { - return null; - } + $pixel = $this->getParam('pixel'); - if ($this->pixel < 0 or $this->pixel > 1000) { + if (!is_numeric($pixel) + || $pixel < 0 + || $pixel > 1000 + ) { return null; } - return (int) $this->pixel; + return (int) $pixel; } } diff --git a/src/Manipulators/Sharpen.php b/src/Manipulators/Sharpen.php index e48f80f..6d8e89b 100644 --- a/src/Manipulators/Sharpen.php +++ b/src/Manipulators/Sharpen.php @@ -4,9 +4,6 @@ use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $sharp - */ class Sharpen extends BaseManipulator { /** @@ -34,14 +31,15 @@ public function run(ImageInterface $image): ImageInterface */ public function getSharpen(): ?int { - if (!is_numeric($this->sharp)) { - return null; - } + $sharp = $this->getParam('sharp'); - if ($this->sharp < 0 or $this->sharp > 100) { + if (!is_numeric($sharp) + || $sharp < 0 + || $sharp > 100 + ) { return null; } - return (int) $this->sharp; + return (int) $sharp; } } diff --git a/src/Manipulators/Size.php b/src/Manipulators/Size.php index 8f1a7e9..f9f700c 100644 --- a/src/Manipulators/Size.php +++ b/src/Manipulators/Size.php @@ -5,12 +5,6 @@ use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\ImageInterface; -/** - * @property string $dpr - * @property string|null $fit - * @property string $h - * @property string $w - */ class Size extends BaseManipulator { /** @@ -80,15 +74,9 @@ public function run(ImageInterface $image): ImageInterface */ public function getWidth(): ?int { - if (!is_numeric($this->w)) { - return null; - } - - if ($this->w <= 0) { - return null; - } + $w = (int) $this->getParam('w'); - return (int) $this->w; + return $w <= 0 ? null : $w; } /** @@ -98,15 +86,9 @@ public function getWidth(): ?int */ public function getHeight(): ?int { - if (!is_numeric($this->h)) { - return null; - } + $h = (int) $this->getParam('h'); - if ($this->h <= 0) { - return null; - } - - return (int) $this->h; + return $h <= 0 ? null : $h; } /** @@ -116,15 +98,13 @@ public function getHeight(): ?int */ public function getFit(): string { - if (null === $this->fit) { - return 'contain'; - } + $fit = (string) $this->getParam('fit'); - if (in_array($this->fit, ['contain', 'fill', 'max', 'stretch', 'fill-max'], true)) { - return $this->fit; + if (in_array($fit, ['contain', 'fill', 'max', 'stretch', 'fill-max'], true)) { + return $fit; } - if (preg_match('/^(crop)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right|-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/', $this->fit)) { + if (preg_match('/^(crop)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right|-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/', $fit)) { return 'crop'; } @@ -138,15 +118,17 @@ public function getFit(): string */ public function getDpr(): float { - if (!is_numeric($this->dpr)) { + $dpr = $this->getParam('dpr'); + + if (!is_numeric($dpr)) { return 1.0; } - if ($this->dpr < 0 or $this->dpr > 8) { + if ($dpr < 0 or $dpr > 8) { return 1.0; } - return (float) $this->dpr; + return (float) $dpr; } /** @@ -432,15 +414,17 @@ public function getCrop(): array 'crop-bottom-right' => [100, 100, 1.0], ]; - if (null === $this->fit) { + $fit = (string) $this->getParam('fit'); + + if ('' === $fit) { return [50, 50, 1.0]; } - if (array_key_exists($this->fit, $cropMethods)) { - return $cropMethods[$this->fit]; + if (array_key_exists($fit, $cropMethods)) { + return $cropMethods[$fit]; } - if (preg_match('/^crop-([\d]{1,3})-([\d]{1,3})(?:-([\d]{1,3}(?:\.\d+)?))*$/', $this->fit, $matches)) { + if (preg_match('/^crop-([\d]{1,3})-([\d]{1,3})(?:-([\d]{1,3}(?:\.\d+)?))*$/', $fit, $matches)) { $matches[3] = isset($matches[3]) ? $matches[3] : 1; if ($matches[1] > 100 or $matches[2] > 100 or $matches[3] > 100) { diff --git a/src/Manipulators/Watermark.php b/src/Manipulators/Watermark.php index d739635..603159a 100644 --- a/src/Manipulators/Watermark.php +++ b/src/Manipulators/Watermark.php @@ -8,18 +8,6 @@ use League\Glide\Filesystem\FilesystemException; use League\Glide\Manipulators\Helpers\Dimension; -/** - * @property string $dpr - * @property string $mark - * @property string $markfit - * @property string $markh - * @property string $markpad - * @property string $markpos - * @property string $markw - * @property string $markx - * @property string $marky - * @property string $markalpha - */ class Watermark extends BaseManipulator { /** @@ -133,9 +121,9 @@ public function getImage(ImageInterface $image): ?ImageInterface return null; } - $path = $this->mark; + $path = (string) $this->getParam('mark'); - if (!$path) { + if ('' === $path) { return null; } @@ -171,8 +159,10 @@ public function getImage(ImageInterface $image): ?ImageInterface */ public function getDimension(ImageInterface $image, string $field): ?float { - if ($this->{$field}) { - return (new Dimension($image, $this->getDpr()))->get($this->{$field}); + $dim = $this->getParam($field); + + if ($dim) { + return (new Dimension($image, $this->getDpr()))->get((string) $dim); } return null; @@ -185,15 +175,17 @@ public function getDimension(ImageInterface $image, string $field): ?float */ public function getDpr(): float { - if (!is_numeric($this->dpr)) { + $dpr = $this->getParam('dpr'); + + if (!is_numeric($dpr)) { return 1.0; } - if ($this->dpr < 0 or $this->dpr > 8) { + if ($dpr < 0 or $dpr > 8) { return 1.0; } - return (float) $this->dpr; + return (float) $dpr; } /** @@ -219,8 +211,10 @@ public function getFit(): ?string 'crop-bottom-right', ]; - if (in_array($this->markfit, $fitMethods, true)) { - return $this->markfit; + $markfit = $this->getParam('markfit'); + + if (in_array($markfit, $fitMethods, true)) { + return $markfit; } return null; @@ -245,8 +239,10 @@ public function getPosition(): string 'bottom-right', ]; - if (in_array($this->markpos, $positions, true)) { - return $this->markpos; + $markpos = $this->getParam('markpos'); + + if (in_array($markpos, $positions, true)) { + return $markpos; } return 'bottom-right'; @@ -259,14 +255,16 @@ public function getPosition(): string */ public function getAlpha(): int { - if (!is_numeric($this->markalpha)) { + $markalpha = $this->getParam('markalpha'); + + if (!is_numeric($markalpha)) { return 100; } - if ($this->markalpha < 0 or $this->markalpha > 100) { + if ($markalpha < 0 or $markalpha > 100) { return 100; } - return (int) $this->markalpha; + return (int) $markalpha; } }