Skip to content

Commit

Permalink
Pass mime type manually to avoid finfo dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 6, 2024
1 parent a09c7c9 commit 8cf0405
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/Drivers/AbstractEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/BmpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
4 changes: 2 additions & 2 deletions src/Drivers/Gd/Encoders/GifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/JpegEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/WebpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/BmpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/GifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/HeicEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/JpegEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/TiffEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/WebpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
10 changes: 3 additions & 7 deletions src/EncodedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -32,7 +28,7 @@ public function __construct(
*/
public function mediaType(): string
{
return mime_content_type($this->pointer);
return $this->mediaType;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/EncodedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}

0 comments on commit 8cf0405

Please sign in to comment.