1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-20 04:31:24 +02:00

Add mediaType methods for every mimetype method

This commit is contained in:
Oliver Vogel
2023-12-17 16:48:09 +01:00
parent f4cd289e91
commit c594ed1dd0
5 changed files with 40 additions and 9 deletions

View File

@@ -10,22 +10,32 @@ class EncodedImage extends File implements EncodedImageInterface
* Create new instance * Create new instance
* *
* @param string $data * @param string $data
* @param string $mimetype * @param string $mediaType
*/ */
public function __construct( public function __construct(
protected string $data, protected string $data,
protected string $mimetype = 'application/octet-stream' protected string $mediaType = 'application/octet-stream'
) { ) {
} }
/** /**
* Return mime type of encoed image data * Return media (mime) type of encoed image data
*
* @return string
*/
public function mediaType(): string
{
return $this->mediaType;
}
/**
* Alias of self::mediaType(
* *
* @return string * @return string
*/ */
public function mimetype(): string public function mimetype(): string
{ {
return $this->mimetype; return $this->mediaType();
} }
/** /**
@@ -35,6 +45,6 @@ class EncodedImage extends File implements EncodedImageInterface
*/ */
public function toDataUri(): string public function toDataUri(): string
{ {
return sprintf('data:%s;base64,%s', $this->mimetype, base64_encode($this->data)); return sprintf('data:%s;base64,%s', $this->mediaType, base64_encode($this->data));
} }
} }

View File

@@ -11,7 +11,7 @@ class AutoEncoder implements EncoderInterface
{ {
public function encode(ImageInterface $image): EncodedImageInterface public function encode(ImageInterface $image): EncodedImageInterface
{ {
$type = $image->origin()->mimetype(); $type = $image->origin()->mediaType();
return $image->encode( return $image->encode(
match ($type) { match ($type) {
'image/webp' => new WebpEncoder(), 'image/webp' => new WebpEncoder(),

View File

@@ -9,6 +9,13 @@ interface EncodedImageInterface extends FileInterface
* *
* @return string * @return string
*/ */
public function mediaType(): string;
/**
* Alias of self::mediaType()
*
* @return string
*/
public function mimetype(): string; public function mimetype(): string;
/** /**

View File

@@ -7,16 +7,21 @@ class Origin
/** /**
* Create new origin instance * Create new origin instance
* *
* @param string $mimetype * @param string $mediaType
* @return void * @return void
*/ */
public function __construct( public function __construct(
protected string $mimetype = 'application/octet-stream' protected string $mediaType = 'application/octet-stream'
) { ) {
} }
public function mediaType(): string
{
return $this->mediaType;
}
public function mimetype(): string public function mimetype(): string
{ {
return $this->mimetype; return $this->mediaType();
} }
} }

View File

@@ -38,6 +38,15 @@ class EncodedImageTest extends TestCase
$this->assertEquals('foo', (string) $image); $this->assertEquals('foo', (string) $image);
} }
public function testMediaType(): void
{
$image = new EncodedImage('foo');
$this->assertEquals('application/octet-stream', $image->mediaType());
$image = new EncodedImage('foo', 'image/jpeg');
$this->assertEquals('image/jpeg', $image->mediaType());
}
public function testMimetype(): void public function testMimetype(): void
{ {
$image = new EncodedImage('foo'); $image = new EncodedImage('foo');