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:
parent
a09c7c9c33
commit
8cf0405a9a
@ -31,14 +31,15 @@ abstract class AbstractEncoder implements EncoderInterface
|
|||||||
* Build new file pointer, run callback with it and return result as encoded image
|
* Build new file pointer, run callback with it and return result as encoded image
|
||||||
*
|
*
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
|
* @param null|string $mediaType
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* @return EncodedImage
|
* @return EncodedImage
|
||||||
*/
|
*/
|
||||||
protected function createEncodedImage(callable $callback): EncodedImage
|
protected function createEncodedImage(callable $callback, ?string $mediaType = null): EncodedImage
|
||||||
{
|
{
|
||||||
$pointer = $this->buildFilePointer();
|
$pointer = $this->buildFilePointer();
|
||||||
$callback($pointer);
|
$callback($pointer);
|
||||||
|
|
||||||
return new EncodedImage($pointer);
|
return is_string($mediaType) ? new EncodedImage($pointer, $mediaType) : new EncodedImage($pointer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,6 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
|
|||||||
{
|
{
|
||||||
return $this->createEncodedImage(function ($pointer) use ($image) {
|
return $this->createEncodedImage(function ($pointer) use ($image) {
|
||||||
imageavif($image->core()->native(), $pointer, $this->quality);
|
imageavif($image->core()->native(), $pointer, $this->quality);
|
||||||
});
|
}, 'image/aviv');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,6 @@ class BmpEncoder extends GenericBmpEncoder implements SpecializedInterface
|
|||||||
{
|
{
|
||||||
return $this->createEncodedImage(function ($pointer) use ($image) {
|
return $this->createEncodedImage(function ($pointer) use ($image) {
|
||||||
imagebmp($image->core()->native(), $pointer, false);
|
imagebmp($image->core()->native(), $pointer, false);
|
||||||
});
|
}, 'image/bmp');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface
|
|||||||
return $this->createEncodedImage(function ($pointer) use ($gd) {
|
return $this->createEncodedImage(function ($pointer) use ($gd) {
|
||||||
imageinterlace($gd, $this->interlaced);
|
imageinterlace($gd, $this->interlaced);
|
||||||
imagegif($gd, $pointer);
|
imagegif($gd, $pointer);
|
||||||
});
|
}, 'image/gif');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,7 +56,7 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface
|
|||||||
|
|
||||||
$builder->setLoops($image->loops());
|
$builder->setLoops($image->loops());
|
||||||
|
|
||||||
return new EncodedImage($builder->encode());
|
return new EncodedImage($builder->encode(), 'image/gif');
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new EncoderException($e->getMessage(), $e->getCode(), $e);
|
throw new EncoderException($e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,6 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
|
|||||||
return $this->createEncodedImage(function ($pointer) use ($output) {
|
return $this->createEncodedImage(function ($pointer) use ($output) {
|
||||||
imageinterlace($output, $this->progressive);
|
imageinterlace($output, $this->progressive);
|
||||||
imagejpeg($output, $pointer, $this->quality);
|
imagejpeg($output, $pointer, $this->quality);
|
||||||
});
|
}, 'image/jpeg');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
|
|||||||
return $this->createEncodedImage(function ($pointer) use ($output) {
|
return $this->createEncodedImage(function ($pointer) use ($output) {
|
||||||
imageinterlace($output, $this->interlaced);
|
imageinterlace($output, $this->interlaced);
|
||||||
imagepng($output, $pointer, -1);
|
imagepng($output, $pointer, -1);
|
||||||
});
|
}, 'image/png');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,6 +22,6 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
|
|||||||
|
|
||||||
return $this->createEncodedImage(function ($pointer) use ($image, $quality) {
|
return $this->createEncodedImage(function ($pointer) use ($image, $quality) {
|
||||||
imagewebp($image->core()->native(), $pointer, $quality);
|
imagewebp($image->core()->native(), $pointer, $quality);
|
||||||
});
|
}, 'image/webp');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,6 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
|
|||||||
$imagick->setCompressionQuality($this->quality);
|
$imagick->setCompressionQuality($this->quality);
|
||||||
$imagick->setImageCompressionQuality($this->quality);
|
$imagick->setImageCompressionQuality($this->quality);
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/avif');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,6 @@ class BmpEncoder extends GenericBmpEncoder implements SpecializedInterface
|
|||||||
$imagick->setCompression($compression);
|
$imagick->setCompression($compression);
|
||||||
$imagick->setImageCompression($compression);
|
$imagick->setImageCompression($compression);
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/bmp');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,6 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface
|
|||||||
$imagick->setInterlaceScheme(Imagick::INTERLACE_LINE);
|
$imagick->setInterlaceScheme(Imagick::INTERLACE_LINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/gif');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,6 @@ class HeicEncoder extends GenericHeicEncoder implements SpecializedInterface
|
|||||||
$imagick->setCompressionQuality($this->quality);
|
$imagick->setCompressionQuality($this->quality);
|
||||||
$imagick->setImageCompressionQuality($this->quality);
|
$imagick->setImageCompressionQuality($this->quality);
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/heic');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,6 @@ class Jpeg2000Encoder extends GenericJpeg2000Encoder implements SpecializedInter
|
|||||||
$imagick->setCompressionQuality($this->quality);
|
$imagick->setCompressionQuality($this->quality);
|
||||||
$imagick->setImageCompressionQuality($this->quality);
|
$imagick->setImageCompressionQuality($this->quality);
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/jp2');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,6 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
|
|||||||
$imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE);
|
$imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/jpeg');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,6 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
|
|||||||
$output->setInterlaceScheme(Imagick::INTERLACE_LINE);
|
$output->setInterlaceScheme(Imagick::INTERLACE_LINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EncodedImage($output->getImagesBlob());
|
return new EncodedImage($output->getImagesBlob(), 'image/png');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,6 @@ class TiffEncoder extends GenericTiffEncoder implements SpecializedInterface
|
|||||||
$imagick->setCompressionQuality($this->quality);
|
$imagick->setCompressionQuality($this->quality);
|
||||||
$imagick->setImageCompressionQuality($this->quality);
|
$imagick->setImageCompressionQuality($this->quality);
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/tiff');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,6 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
|
|||||||
$imagick->setImageCompression($compression);
|
$imagick->setImageCompression($compression);
|
||||||
$imagick->setImageCompressionQuality($this->quality);
|
$imagick->setImageCompressionQuality($this->quality);
|
||||||
|
|
||||||
return new EncodedImage($imagick->getImagesBlob());
|
return new EncodedImage($imagick->getImagesBlob(), 'image/webp');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,16 +12,12 @@ class EncodedImage extends File implements EncodedImageInterface
|
|||||||
* Create new instance
|
* Create new instance
|
||||||
*
|
*
|
||||||
* @param string|resource $data
|
* @param string|resource $data
|
||||||
* @param string $mediaType Deprecated parameter, will be removed
|
* @param string $mediaType
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
mixed $data,
|
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);
|
parent::__construct($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +28,7 @@ class EncodedImage extends File implements EncodedImageInterface
|
|||||||
*/
|
*/
|
||||||
public function mediaType(): string
|
public function mediaType(): string
|
||||||
{
|
{
|
||||||
return mime_content_type($this->pointer);
|
return $this->mediaType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,10 +105,11 @@ abstract class BaseTestCase extends MockeryTestCase
|
|||||||
|
|
||||||
protected function assertMediaType(string|array $allowed, string|EncodedImage $input): void
|
protected function assertMediaType(string|array $allowed, string|EncodedImage $input): void
|
||||||
{
|
{
|
||||||
$detected = match (true) {
|
$pointer = fopen('php://temp', 'rw');
|
||||||
is_string($input) => (new EncodedImage($input))->mimetype(),
|
fputs($pointer, (string) $input);
|
||||||
default => $input->mimetype(),
|
rewind($pointer);
|
||||||
};
|
$detected = mime_content_type($pointer);
|
||||||
|
fclose($pointer);
|
||||||
|
|
||||||
$allowed = is_string($allowed) ? [$allowed] : $allowed;
|
$allowed = is_string($allowed) ? [$allowed] : $allowed;
|
||||||
$this->assertTrue(in_array($detected, $allowed));
|
$this->assertTrue(in_array($detected, $allowed));
|
||||||
|
@ -31,7 +31,7 @@ final class EncodedImageTest extends BaseTestCase
|
|||||||
public function testToDataUri(): void
|
public function testToDataUri(): void
|
||||||
{
|
{
|
||||||
$image = new EncodedImage('foo');
|
$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
|
public function testToString(): void
|
||||||
@ -43,18 +43,18 @@ final class EncodedImageTest extends BaseTestCase
|
|||||||
public function testMediaType(): void
|
public function testMediaType(): void
|
||||||
{
|
{
|
||||||
$image = new EncodedImage('foo');
|
$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());
|
$this->assertEquals('image/jpeg', $image->mediaType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMimetype(): void
|
public function testMimetype(): void
|
||||||
{
|
{
|
||||||
$image = new EncodedImage('foo');
|
$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());
|
$this->assertEquals('image/jpeg', $image->mimetype());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user