mirror of
https://github.com/Intervention/image.git
synced 2025-08-20 20:51:20 +02:00
Add Image::toMediaType()
This commit is contained in:
@@ -9,20 +9,38 @@ use Intervention\Image\Interfaces\ImageInterface;
|
||||
|
||||
class AutoEncoder implements EncoderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see EncoderInterface::encode()
|
||||
*/
|
||||
public function encode(ImageInterface $image): EncodedImageInterface
|
||||
{
|
||||
$type = $image->origin()->mediaType();
|
||||
return $image->encode(
|
||||
match ($type) {
|
||||
'image/webp' => new WebpEncoder(),
|
||||
'image/avif' => new AvifEncoder(),
|
||||
'image/jpeg' => new JpegEncoder(),
|
||||
'image/bmp' => new BmpEncoder(),
|
||||
'image/gif' => new GifEncoder(),
|
||||
'image/png' => new PngEncoder(),
|
||||
'image/tiff' => new TiffEncoder(),
|
||||
default => throw new EncoderException('No encoder found for media type (' . $type . ').'),
|
||||
}
|
||||
$this->encoderByMediaType(
|
||||
$image->origin()->mediaType()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return encoder matching to encode given media (mime) type
|
||||
*
|
||||
* @param string $type
|
||||
* @return EncoderInterface
|
||||
* @throws EncoderException
|
||||
*/
|
||||
protected function encoderByMediaType(string $type): EncoderInterface
|
||||
{
|
||||
return match ($type) {
|
||||
'image/webp' => new WebpEncoder(),
|
||||
'image/avif' => new AvifEncoder(),
|
||||
'image/jpeg' => new JpegEncoder(),
|
||||
'image/bmp' => new BmpEncoder(),
|
||||
'image/gif' => new GifEncoder(),
|
||||
'image/png' => new PngEncoder(),
|
||||
'image/tiff' => new TiffEncoder(),
|
||||
default => throw new EncoderException('No encoder found for media type (' . $type . ').'),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
33
src/Encoders/MediaTypeEncoder.php
Normal file
33
src/Encoders/MediaTypeEncoder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Encoders;
|
||||
|
||||
use Intervention\Image\Interfaces\EncodedImageInterface;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
|
||||
class MediaTypeEncoder extends AutoEncoder
|
||||
{
|
||||
/**
|
||||
* Create new encoder instance to encode given media (mime) type
|
||||
*
|
||||
* @param null|string $type
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(protected ?string $type = null)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see EncoderInterface::encode()
|
||||
*/
|
||||
public function encode(ImageInterface $image): EncodedImageInterface
|
||||
{
|
||||
return $image->encode(
|
||||
$this->encoderByMediaType(
|
||||
is_null($this->type) ? $image->origin()->mediaType() : $this->type
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@@ -16,6 +16,7 @@ use Intervention\Image\Encoders\AvifEncoder;
|
||||
use Intervention\Image\Encoders\BmpEncoder;
|
||||
use Intervention\Image\Encoders\GifEncoder;
|
||||
use Intervention\Image\Encoders\JpegEncoder;
|
||||
use Intervention\Image\Encoders\MediaTypeEncoder;
|
||||
use Intervention\Image\Encoders\PngEncoder;
|
||||
use Intervention\Image\Encoders\TiffEncoder;
|
||||
use Intervention\Image\Encoders\WebpEncoder;
|
||||
@@ -744,6 +745,16 @@ final class Image implements ImageInterface, Countable
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see ImageInterface::toMediaType()
|
||||
*/
|
||||
public function toMediaType(?string $type = null): EncodedImageInterface
|
||||
{
|
||||
return $this->encode(new MediaTypeEncoder($type));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
|
@@ -557,6 +557,15 @@ interface ImageInterface extends IteratorAggregate, Countable
|
||||
*/
|
||||
public function drawLine(callable $init): ImageInterface;
|
||||
|
||||
/**
|
||||
* Encode image to given media (mime) type. If no type is given the image
|
||||
* will be encoded to the format of the originally read image.
|
||||
*
|
||||
* @param null|string $type
|
||||
* @return EncodedImageInterface
|
||||
*/
|
||||
public function toMediaType(?string $type = null): EncodedImageInterface;
|
||||
|
||||
/**
|
||||
* Encode image to JPEG format
|
||||
*
|
||||
|
@@ -122,6 +122,17 @@ class ImageTest extends TestCase
|
||||
$this->assertMediaType('image/gif', (string) $result);
|
||||
}
|
||||
|
||||
public function testToMediaType(): void
|
||||
{
|
||||
$result = $this->readTestImage('blue.gif')->toMediaType();
|
||||
$this->assertInstanceOf(EncodedImage::class, $result);
|
||||
$this->assertMediaType('image/gif', (string) $result);
|
||||
|
||||
$result = $this->readTestImage('blue.gif')->toMediaType('image/png');
|
||||
$this->assertInstanceOf(EncodedImage::class, $result);
|
||||
$this->assertMediaType('image/png', (string) $result);
|
||||
}
|
||||
|
||||
public function testWidthHeightSize(): void
|
||||
{
|
||||
$this->assertEquals(3, $this->image->width());
|
||||
|
@@ -121,6 +121,17 @@ class ImageTest extends TestCase
|
||||
$this->assertMediaType('image/gif', (string) $result);
|
||||
}
|
||||
|
||||
public function testToMediaType(): void
|
||||
{
|
||||
$result = $this->readTestImage('blue.gif')->toMediaType();
|
||||
$this->assertInstanceOf(EncodedImage::class, $result);
|
||||
$this->assertMediaType('image/gif', (string) $result);
|
||||
|
||||
$result = $this->readTestImage('blue.gif')->toMediaType('image/png');
|
||||
$this->assertInstanceOf(EncodedImage::class, $result);
|
||||
$this->assertMediaType('image/png', (string) $result);
|
||||
}
|
||||
|
||||
public function testWidthHeightSize(): void
|
||||
{
|
||||
$this->assertEquals(20, $this->image->width());
|
||||
|
Reference in New Issue
Block a user