Skip to content

Commit

Permalink
Add Image::toMediaType()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 17, 2023
1 parent b12f31e commit 04faec2
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/Encoders/AutoEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,38 @@

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 changes: 33 additions & 0 deletions src/Encoders/MediaTypeEncoder.php
Original file line number Diff line number Diff line change
@@ -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
)
);
}
}
11 changes: 11 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -744,6 +745,16 @@ public function drawLine(callable $init): ImageInterface
);
}

/**
* {@inheritdoc}
*
* @see ImageInterface::toMediaType()
*/
public function toMediaType(?string $type = null): EncodedImageInterface
{
return $this->encode(new MediaTypeEncoder($type));
}

/**
* {@inheritdoc}
*
Expand Down
9 changes: 9 additions & 0 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,15 @@ public function drawPolygon(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
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public function testAutoEncode(): void
$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());
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/Imagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public function testAutoEncode(): void
$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());
Expand Down

0 comments on commit 04faec2

Please sign in to comment.