1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 12:11:26 +02:00

EncodedImage class

This commit is contained in:
Oliver Vogel
2021-10-21 15:07:56 +02:00
parent 95e72b877b
commit 8405300cc7
4 changed files with 41 additions and 23 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ composer.lock
vendor/ vendor/
dev/ dev/
.idea/ .idea/
.phpunit.result.cache

View File

@@ -3,6 +3,7 @@
namespace Intervention\Image\Drivers\Abstract; namespace Intervention\Image\Drivers\Abstract;
use Intervention\Image\Collection; use Intervention\Image\Collection;
use Intervention\Image\EncodedImage;
use Intervention\Image\Exceptions\NotWritableException; use Intervention\Image\Exceptions\NotWritableException;
use Intervention\Image\Geometry\Size; use Intervention\Image\Geometry\Size;
use Intervention\Image\Interfaces\EncoderInterface; use Intervention\Image\Interfaces\EncoderInterface;
@@ -47,35 +48,18 @@ abstract class AbstractImage
return $modifier->apply($this); return $modifier->apply($this);
} }
public function encode(EncoderInterface $encoder, ?string $path = null): string public function encode(EncoderInterface $encoder): EncodedImage
{ {
$encoded = $encoder->encode($this); return new EncodedImage($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;
} }
public function toJpeg(?int $quality = null, ?string $path = null): string public function toJpeg(?int $quality = null): string
{ {
return $this->encode( return $this->encode($this->resolveDriverClass('Encoders\JpegEncoder', $quality));
$this->resolveDriverClass('Encoders\JpegEncoder', $quality),
$path
);
} }
public function toGif(?string $path = null): string public function toGif(): string
{ {
return $this->encode( return $this->encode($this->resolveDriverClass('Encoders\GifEncoder'));
$this->resolveDriverClass('Encoders\GifEncoder'),
$path
);
} }
} }

30
src/EncodedImage.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
namespace Intervention\Image;
use Intervention\Image\Exceptions\NotWritableException;
class EncodedImage
{
protected $data;
public function __construct(string $data)
{
$this->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;
}
}

View File

@@ -2,6 +2,8 @@
namespace Intervention\Image\Interfaces; namespace Intervention\Image\Interfaces;
use Intervention\Image\EncodedImage;
interface ImageInterface interface ImageInterface
{ {
public function size(): SizeInterface; public function size(): SizeInterface;
@@ -9,4 +11,5 @@ interface ImageInterface
public function height(): int; public function height(): int;
public function isAnimated(): bool; public function isAnimated(): bool;
public function greyscale(): ImageInterface; public function greyscale(): ImageInterface;
public function encode(EncoderInterface $encoder): EncodedImage
} }