1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-16 19:58:14 +01:00

Pass mime type manually to avoid finfo dependency

This commit is contained in:
Oliver Vogel 2024-10-06 16:37:26 +02:00
parent a09c7c9c33
commit 8cf0405a9a
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB
19 changed files with 32 additions and 34 deletions

View File

@ -31,14 +31,15 @@ abstract class AbstractEncoder implements EncoderInterface
* 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);
}
}

View File

@ -20,6 +20,6 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
{
return $this->createEncodedImage(function ($pointer) use ($image) {
imageavif($image->core()->native(), $pointer, $this->quality);
});
}, 'image/aviv');
}
}

View File

@ -20,6 +20,6 @@ class BmpEncoder extends GenericBmpEncoder implements SpecializedInterface
{
return $this->createEncodedImage(function ($pointer) use ($image) {
imagebmp($image->core()->native(), $pointer, false);
});
}, 'image/bmp');
}
}

View File

@ -32,7 +32,7 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface
return $this->createEncodedImage(function ($pointer) use ($gd) {
imageinterlace($gd, $this->interlaced);
imagegif($gd, $pointer);
});
}, 'image/gif');
}
/**
@ -56,7 +56,7 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface
$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);
}

View File

@ -31,6 +31,6 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
return $this->createEncodedImage(function ($pointer) use ($output) {
imageinterlace($output, $this->progressive);
imagejpeg($output, $pointer, $this->quality);
});
}, 'image/jpeg');
}
}

View File

@ -28,7 +28,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
return $this->createEncodedImage(function ($pointer) use ($output) {
imageinterlace($output, $this->interlaced);
imagepng($output, $pointer, -1);
});
}, 'image/png');
}
/**

View File

@ -22,6 +22,6 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
return $this->createEncodedImage(function ($pointer) use ($image, $quality) {
imagewebp($image->core()->native(), $pointer, $quality);
});
}, 'image/webp');
}
}

View File

@ -26,6 +26,6 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
$imagick->setCompressionQuality($this->quality);
$imagick->setImageCompressionQuality($this->quality);
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/avif');
}
}

View File

@ -24,6 +24,6 @@ class BmpEncoder extends GenericBmpEncoder implements SpecializedInterface
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/bmp');
}
}

View File

@ -29,6 +29,6 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface
$imagick->setInterlaceScheme(Imagick::INTERLACE_LINE);
}
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/gif');
}
}

View File

@ -22,6 +22,6 @@ class HeicEncoder extends GenericHeicEncoder implements SpecializedInterface
$imagick->setCompressionQuality($this->quality);
$imagick->setImageCompressionQuality($this->quality);
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/heic');
}
}

View File

@ -28,6 +28,6 @@ class Jpeg2000Encoder extends GenericJpeg2000Encoder implements SpecializedInter
$imagick->setCompressionQuality($this->quality);
$imagick->setImageCompressionQuality($this->quality);
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/jp2');
}
}

View File

@ -45,6 +45,6 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
$imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE);
}
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/jpeg');
}
}

View File

@ -41,6 +41,6 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
$output->setInterlaceScheme(Imagick::INTERLACE_LINE);
}
return new EncodedImage($output->getImagesBlob());
return new EncodedImage($output->getImagesBlob(), 'image/png');
}
}

View File

@ -24,6 +24,6 @@ class TiffEncoder extends GenericTiffEncoder implements SpecializedInterface
$imagick->setCompressionQuality($this->quality);
$imagick->setImageCompressionQuality($this->quality);
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/tiff');
}
}

View File

@ -32,6 +32,6 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
$imagick->setImageCompression($compression);
$imagick->setImageCompressionQuality($this->quality);
return new EncodedImage($imagick->getImagesBlob());
return new EncodedImage($imagick->getImagesBlob(), 'image/webp');
}
}

View File

@ -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 @@ class EncodedImage extends File implements EncodedImageInterface
*/
public function mediaType(): string
{
return mime_content_type($this->pointer);
return $this->mediaType;
}
/**

View File

@ -105,10 +105,11 @@ abstract class BaseTestCase extends MockeryTestCase
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));

View File

@ -31,7 +31,7 @@ final class EncodedImageTest extends BaseTestCase
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 @@ final class EncodedImageTest extends BaseTestCase
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());
}
}