1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-21 05:01:20 +02:00

Add Image::toMediaType()

This commit is contained in:
Oliver Vogel
2023-12-17 17:08:10 +01:00
parent b12f31e9b2
commit 04faec22a6
6 changed files with 104 additions and 11 deletions

View File

@@ -9,20 +9,38 @@ use Intervention\Image\Interfaces\ImageInterface;
class AutoEncoder implements EncoderInterface class AutoEncoder implements EncoderInterface
{ {
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface public function encode(ImageInterface $image): EncodedImageInterface
{ {
$type = $image->origin()->mediaType();
return $image->encode( return $image->encode(
match ($type) { $this->encoderByMediaType(
'image/webp' => new WebpEncoder(), $image->origin()->mediaType()
'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 . ').'),
}
); );
} }
/**
* 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 . ').'),
};
}
} }

View 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
)
);
}
}

View File

@@ -16,6 +16,7 @@ use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\Encoders\BmpEncoder; use Intervention\Image\Encoders\BmpEncoder;
use Intervention\Image\Encoders\GifEncoder; use Intervention\Image\Encoders\GifEncoder;
use Intervention\Image\Encoders\JpegEncoder; use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Encoders\MediaTypeEncoder;
use Intervention\Image\Encoders\PngEncoder; use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Encoders\TiffEncoder; use Intervention\Image\Encoders\TiffEncoder;
use Intervention\Image\Encoders\WebpEncoder; 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} * {@inheritdoc}
* *

View File

@@ -557,6 +557,15 @@ interface ImageInterface extends IteratorAggregate, Countable
*/ */
public function drawLine(callable $init): ImageInterface; 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 * Encode image to JPEG format
* *

View File

@@ -122,6 +122,17 @@ class ImageTest extends TestCase
$this->assertMediaType('image/gif', (string) $result); $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 public function testWidthHeightSize(): void
{ {
$this->assertEquals(3, $this->image->width()); $this->assertEquals(3, $this->image->width());

View File

@@ -121,6 +121,17 @@ class ImageTest extends TestCase
$this->assertMediaType('image/gif', (string) $result); $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 public function testWidthHeightSize(): void
{ {
$this->assertEquals(20, $this->image->width()); $this->assertEquals(20, $this->image->width());