From 8405300cc7e3663cebc57e3d54d9c7aa94ce053b Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 21 Oct 2021 15:07:56 +0200 Subject: [PATCH] EncodedImage class --- .gitignore | 1 + src/Drivers/Abstract/AbstractImage.php | 30 ++++++-------------------- src/EncodedImage.php | 30 ++++++++++++++++++++++++++ src/Interfaces/ImageInterface.php | 3 +++ 4 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 src/EncodedImage.php diff --git a/.gitignore b/.gitignore index daaed11d..799dd941 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock vendor/ dev/ .idea/ +.phpunit.result.cache \ No newline at end of file diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index 780b734b..4c48a781 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -3,6 +3,7 @@ namespace Intervention\Image\Drivers\Abstract; use Intervention\Image\Collection; +use Intervention\Image\EncodedImage; use Intervention\Image\Exceptions\NotWritableException; use Intervention\Image\Geometry\Size; use Intervention\Image\Interfaces\EncoderInterface; @@ -47,35 +48,18 @@ abstract class AbstractImage return $modifier->apply($this); } - public function encode(EncoderInterface $encoder, ?string $path = null): string + public function encode(EncoderInterface $encoder): EncodedImage { - $encoded = $encoder->encode($this); - - if ($path) { - $saved = @file_put_contents($path, $encoded); - if ($saved === false) { - throw new NotWritableException( - "Can't write image data to path ({$path})." - ); - } - } - - return $encoded; + return new EncodedImage($encoder->encode($this)); } - public function toJpeg(?int $quality = null, ?string $path = null): string + public function toJpeg(?int $quality = null): string { - return $this->encode( - $this->resolveDriverClass('Encoders\JpegEncoder', $quality), - $path - ); + return $this->encode($this->resolveDriverClass('Encoders\JpegEncoder', $quality)); } - public function toGif(?string $path = null): string + public function toGif(): string { - return $this->encode( - $this->resolveDriverClass('Encoders\GifEncoder'), - $path - ); + return $this->encode($this->resolveDriverClass('Encoders\GifEncoder')); } } diff --git a/src/EncodedImage.php b/src/EncodedImage.php new file mode 100644 index 00000000..b7691860 --- /dev/null +++ b/src/EncodedImage.php @@ -0,0 +1,30 @@ +data = $data; + } + + public function save(string $filepath): void + { + $saved = @file_put_contents($filepath, (string) $this); + if ($saved === false) { + throw new NotWritableException( + "Can't write image data to path ({$filepath})." + ); + } + } + + public function __toString(): string + { + return $this->data; + } +} diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index ed54858a..d4bca589 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -2,6 +2,8 @@ namespace Intervention\Image\Interfaces; +use Intervention\Image\EncodedImage; + interface ImageInterface { public function size(): SizeInterface; @@ -9,4 +11,5 @@ interface ImageInterface public function height(): int; public function isAnimated(): bool; public function greyscale(): ImageInterface; + public function encode(EncoderInterface $encoder): EncodedImage }