mirror of
https://github.com/Intervention/image.git
synced 2025-08-19 12:11:26 +02:00
EncodedImage class
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ composer.lock
|
||||
vendor/
|
||||
dev/
|
||||
.idea/
|
||||
.phpunit.result.cache
|
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
30
src/EncodedImage.php
Normal file
30
src/EncodedImage.php
Normal 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;
|
||||
}
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user