diff --git a/src/Api/Api.php b/src/Api/Api.php index 495c2c0..9df1f24 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -3,7 +3,7 @@ namespace League\Glide\Api; use Intervention\Image\ImageManager; -use League\Glide\Manipulators\Encode; +use Intervention\Image\Interfaces\ImageInterface; use League\Glide\Manipulators\ManipulatorInterface; class Api implements ApiInterface @@ -92,13 +92,23 @@ public function run(string $source, array $params): string foreach ($this->manipulators as $manipulator) { $manipulator->setParams($params); - /** @var \Intervention\Image\Interfaces\ImageInterface */ $image = $manipulator->run($image); } - $encoder = new Encode(); - $encoder->setParams($params); - /** @var \Intervention\Image\Interfaces\EncodedImageInterface */ + return $this->encode($image, $params); + } + + /** + * Perform image encoding to a given format. + * + * @param ImageInterface $image Image object + * @param array $params the manipulator params + * + * @return string Manipulated image binary data + */ + public function encode(ImageInterface $image, array $params): string + { + $encoder = new Encode($params); $encoded = $encoder->run($image); return $encoded->toString(); diff --git a/src/Manipulators/Encode.php b/src/Api/Encode.php similarity index 79% rename from src/Manipulators/Encode.php rename to src/Api/Encode.php index 0313131..8721503 100644 --- a/src/Manipulators/Encode.php +++ b/src/Api/Encode.php @@ -1,12 +1,41 @@ params = $params; + } + + /** + * Get a specific manipulation param. + */ + public function getParam(string $name): mixed + { + return array_key_exists($name, $this->params) + ? $this->params[$name] + : null; + } + /** * Perform output image manipulation. * @@ -42,6 +71,7 @@ public function run(ImageInterface $image): EncodedImageInterface $encoderOptions['interlaced'] = $shouldInterlace; break; default: + throw new Exception("Invalid format provided: {$format}"); } return $image->encodeByExtension(...$encoderOptions);