From 8cf0405a9a6347ba02edd0d8d897b561944035a2 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 6 Oct 2024 16:37:26 +0200 Subject: [PATCH] Pass mime type manually to avoid finfo dependency --- src/Drivers/AbstractEncoder.php | 5 +++-- src/Drivers/Gd/Encoders/AvifEncoder.php | 2 +- src/Drivers/Gd/Encoders/BmpEncoder.php | 2 +- src/Drivers/Gd/Encoders/GifEncoder.php | 4 ++-- src/Drivers/Gd/Encoders/JpegEncoder.php | 2 +- src/Drivers/Gd/Encoders/PngEncoder.php | 2 +- src/Drivers/Gd/Encoders/WebpEncoder.php | 2 +- src/Drivers/Imagick/Encoders/AvifEncoder.php | 2 +- src/Drivers/Imagick/Encoders/BmpEncoder.php | 2 +- src/Drivers/Imagick/Encoders/GifEncoder.php | 2 +- src/Drivers/Imagick/Encoders/HeicEncoder.php | 2 +- src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php | 2 +- src/Drivers/Imagick/Encoders/JpegEncoder.php | 2 +- src/Drivers/Imagick/Encoders/PngEncoder.php | 2 +- src/Drivers/Imagick/Encoders/TiffEncoder.php | 2 +- src/Drivers/Imagick/Encoders/WebpEncoder.php | 2 +- src/EncodedImage.php | 10 +++------- tests/BaseTestCase.php | 9 +++++---- tests/Unit/EncodedImageTest.php | 10 +++++----- 19 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/Drivers/AbstractEncoder.php b/src/Drivers/AbstractEncoder.php index a5011584..251eaa1b 100644 --- a/src/Drivers/AbstractEncoder.php +++ b/src/Drivers/AbstractEncoder.php @@ -31,14 +31,15 @@ public function encode(ImageInterface $image): EncodedImageInterface * Build new file pointer, run callback with it and return result as encoded image * * @param callable $callback + * @param null|string $mediaType * @throws RuntimeException * @return EncodedImage */ - protected function createEncodedImage(callable $callback): EncodedImage + protected function createEncodedImage(callable $callback, ?string $mediaType = null): EncodedImage { $pointer = $this->buildFilePointer(); $callback($pointer); - return new EncodedImage($pointer); + return is_string($mediaType) ? new EncodedImage($pointer, $mediaType) : new EncodedImage($pointer); } } diff --git a/src/Drivers/Gd/Encoders/AvifEncoder.php b/src/Drivers/Gd/Encoders/AvifEncoder.php index 1a8b8943..6361e7ae 100644 --- a/src/Drivers/Gd/Encoders/AvifEncoder.php +++ b/src/Drivers/Gd/Encoders/AvifEncoder.php @@ -20,6 +20,6 @@ public function encode(ImageInterface $image): EncodedImage { return $this->createEncodedImage(function ($pointer) use ($image) { imageavif($image->core()->native(), $pointer, $this->quality); - }); + }, 'image/aviv'); } } diff --git a/src/Drivers/Gd/Encoders/BmpEncoder.php b/src/Drivers/Gd/Encoders/BmpEncoder.php index 561c6a28..7e9a52d7 100644 --- a/src/Drivers/Gd/Encoders/BmpEncoder.php +++ b/src/Drivers/Gd/Encoders/BmpEncoder.php @@ -20,6 +20,6 @@ public function encode(ImageInterface $image): EncodedImage { return $this->createEncodedImage(function ($pointer) use ($image) { imagebmp($image->core()->native(), $pointer, false); - }); + }, 'image/bmp'); } } diff --git a/src/Drivers/Gd/Encoders/GifEncoder.php b/src/Drivers/Gd/Encoders/GifEncoder.php index 610e17ef..73223091 100644 --- a/src/Drivers/Gd/Encoders/GifEncoder.php +++ b/src/Drivers/Gd/Encoders/GifEncoder.php @@ -32,7 +32,7 @@ public function encode(ImageInterface $image): EncodedImage return $this->createEncodedImage(function ($pointer) use ($gd) { imageinterlace($gd, $this->interlaced); imagegif($gd, $pointer); - }); + }, 'image/gif'); } /** @@ -56,7 +56,7 @@ protected function encodeAnimated(ImageInterface $image): EncodedImage $builder->setLoops($image->loops()); - return new EncodedImage($builder->encode()); + return new EncodedImage($builder->encode(), 'image/gif'); } catch (Exception $e) { throw new EncoderException($e->getMessage(), $e->getCode(), $e); } diff --git a/src/Drivers/Gd/Encoders/JpegEncoder.php b/src/Drivers/Gd/Encoders/JpegEncoder.php index 028d42c8..b686a9cd 100644 --- a/src/Drivers/Gd/Encoders/JpegEncoder.php +++ b/src/Drivers/Gd/Encoders/JpegEncoder.php @@ -31,6 +31,6 @@ public function encode(ImageInterface $image): EncodedImage return $this->createEncodedImage(function ($pointer) use ($output) { imageinterlace($output, $this->progressive); imagejpeg($output, $pointer, $this->quality); - }); + }, 'image/jpeg'); } } diff --git a/src/Drivers/Gd/Encoders/PngEncoder.php b/src/Drivers/Gd/Encoders/PngEncoder.php index 2d88402d..d6263b63 100644 --- a/src/Drivers/Gd/Encoders/PngEncoder.php +++ b/src/Drivers/Gd/Encoders/PngEncoder.php @@ -28,7 +28,7 @@ public function encode(ImageInterface $image): EncodedImage return $this->createEncodedImage(function ($pointer) use ($output) { imageinterlace($output, $this->interlaced); imagepng($output, $pointer, -1); - }); + }, 'image/png'); } /** diff --git a/src/Drivers/Gd/Encoders/WebpEncoder.php b/src/Drivers/Gd/Encoders/WebpEncoder.php index b0550fe9..d0f7b876 100644 --- a/src/Drivers/Gd/Encoders/WebpEncoder.php +++ b/src/Drivers/Gd/Encoders/WebpEncoder.php @@ -22,6 +22,6 @@ public function encode(ImageInterface $image): EncodedImage return $this->createEncodedImage(function ($pointer) use ($image, $quality) { imagewebp($image->core()->native(), $pointer, $quality); - }); + }, 'image/webp'); } } diff --git a/src/Drivers/Imagick/Encoders/AvifEncoder.php b/src/Drivers/Imagick/Encoders/AvifEncoder.php index 069bc9d6..32b8ef48 100644 --- a/src/Drivers/Imagick/Encoders/AvifEncoder.php +++ b/src/Drivers/Imagick/Encoders/AvifEncoder.php @@ -26,6 +26,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/avif'); } } diff --git a/src/Drivers/Imagick/Encoders/BmpEncoder.php b/src/Drivers/Imagick/Encoders/BmpEncoder.php index 87c1ee6e..eede0f09 100644 --- a/src/Drivers/Imagick/Encoders/BmpEncoder.php +++ b/src/Drivers/Imagick/Encoders/BmpEncoder.php @@ -24,6 +24,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setCompression($compression); $imagick->setImageCompression($compression); - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/bmp'); } } diff --git a/src/Drivers/Imagick/Encoders/GifEncoder.php b/src/Drivers/Imagick/Encoders/GifEncoder.php index f1c50103..f9c98b0e 100644 --- a/src/Drivers/Imagick/Encoders/GifEncoder.php +++ b/src/Drivers/Imagick/Encoders/GifEncoder.php @@ -29,6 +29,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setInterlaceScheme(Imagick::INTERLACE_LINE); } - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/gif'); } } diff --git a/src/Drivers/Imagick/Encoders/HeicEncoder.php b/src/Drivers/Imagick/Encoders/HeicEncoder.php index 5047b699..70e56004 100644 --- a/src/Drivers/Imagick/Encoders/HeicEncoder.php +++ b/src/Drivers/Imagick/Encoders/HeicEncoder.php @@ -22,6 +22,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/heic'); } } diff --git a/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php b/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php index a0d2d43b..5c4bd59e 100644 --- a/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php +++ b/src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php @@ -28,6 +28,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/jp2'); } } diff --git a/src/Drivers/Imagick/Encoders/JpegEncoder.php b/src/Drivers/Imagick/Encoders/JpegEncoder.php index 03528289..72cab7e8 100644 --- a/src/Drivers/Imagick/Encoders/JpegEncoder.php +++ b/src/Drivers/Imagick/Encoders/JpegEncoder.php @@ -45,6 +45,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE); } - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/jpeg'); } } diff --git a/src/Drivers/Imagick/Encoders/PngEncoder.php b/src/Drivers/Imagick/Encoders/PngEncoder.php index 896a6c98..cce49d51 100644 --- a/src/Drivers/Imagick/Encoders/PngEncoder.php +++ b/src/Drivers/Imagick/Encoders/PngEncoder.php @@ -41,6 +41,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $output->setInterlaceScheme(Imagick::INTERLACE_LINE); } - return new EncodedImage($output->getImagesBlob()); + return new EncodedImage($output->getImagesBlob(), 'image/png'); } } diff --git a/src/Drivers/Imagick/Encoders/TiffEncoder.php b/src/Drivers/Imagick/Encoders/TiffEncoder.php index 6b660420..a03e0ae3 100644 --- a/src/Drivers/Imagick/Encoders/TiffEncoder.php +++ b/src/Drivers/Imagick/Encoders/TiffEncoder.php @@ -24,6 +24,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setCompressionQuality($this->quality); $imagick->setImageCompressionQuality($this->quality); - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/tiff'); } } diff --git a/src/Drivers/Imagick/Encoders/WebpEncoder.php b/src/Drivers/Imagick/Encoders/WebpEncoder.php index caec9b2c..96fd54cc 100644 --- a/src/Drivers/Imagick/Encoders/WebpEncoder.php +++ b/src/Drivers/Imagick/Encoders/WebpEncoder.php @@ -32,6 +32,6 @@ public function encode(ImageInterface $image): EncodedImageInterface $imagick->setImageCompression($compression); $imagick->setImageCompressionQuality($this->quality); - return new EncodedImage($imagick->getImagesBlob()); + return new EncodedImage($imagick->getImagesBlob(), 'image/webp'); } } diff --git a/src/EncodedImage.php b/src/EncodedImage.php index 8c4a4d31..a4c81fbf 100644 --- a/src/EncodedImage.php +++ b/src/EncodedImage.php @@ -12,16 +12,12 @@ class EncodedImage extends File implements EncodedImageInterface * Create new instance * * @param string|resource $data - * @param string $mediaType Deprecated parameter, will be removed + * @param string $mediaType */ public function __construct( mixed $data, - protected string $mediaType = 'application/octet-stream' // deprecated + protected string $mediaType = 'application/octet-stream' ) { - if ($mediaType !== 'application/octet-stream') { - trigger_error('Parameter $mediaType for class' . self::class . ' is deprecated.', E_USER_DEPRECATED); - } - parent::__construct($data); } @@ -32,7 +28,7 @@ public function __construct( */ public function mediaType(): string { - return mime_content_type($this->pointer); + return $this->mediaType; } /** diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index b9b21b26..7a083c40 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -105,10 +105,11 @@ protected function assertTransparency(ColorInterface $color) protected function assertMediaType(string|array $allowed, string|EncodedImage $input): void { - $detected = match (true) { - is_string($input) => (new EncodedImage($input))->mimetype(), - default => $input->mimetype(), - }; + $pointer = fopen('php://temp', 'rw'); + fputs($pointer, (string) $input); + rewind($pointer); + $detected = mime_content_type($pointer); + fclose($pointer); $allowed = is_string($allowed) ? [$allowed] : $allowed; $this->assertTrue(in_array($detected, $allowed)); diff --git a/tests/Unit/EncodedImageTest.php b/tests/Unit/EncodedImageTest.php index 2441edf6..9224d46b 100644 --- a/tests/Unit/EncodedImageTest.php +++ b/tests/Unit/EncodedImageTest.php @@ -31,7 +31,7 @@ public function testSave(): void public function testToDataUri(): void { $image = new EncodedImage('foo'); - $this->assertEquals('data:text/plain;base64,Zm9v', $image->toDataUri()); + $this->assertEquals('data:application/octet-stream;base64,Zm9v', $image->toDataUri()); } public function testToString(): void @@ -43,18 +43,18 @@ public function testToString(): void public function testMediaType(): void { $image = new EncodedImage('foo'); - $this->assertEquals('text/plain', $image->mediaType()); + $this->assertEquals('application/octet-stream', $image->mediaType()); - $image = new EncodedImage($this->getTestResourceData()); + $image = new EncodedImage($this->getTestResourceData(), 'image/jpeg'); $this->assertEquals('image/jpeg', $image->mediaType()); } public function testMimetype(): void { $image = new EncodedImage('foo'); - $this->assertEquals('text/plain', $image->mimetype()); + $this->assertEquals('application/octet-stream', $image->mimetype()); - $image = new EncodedImage($this->getTestResourceData()); + $image = new EncodedImage($this->getTestResourceData(), 'image/jpeg'); $this->assertEquals('image/jpeg', $image->mimetype()); } }