Skip to content

Commit 2c98def

Browse files
committed
Catch ValueError and throw own exception when MIME-type is not supported
1 parent c5cf7ec commit 2c98def

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Drivers/Gd/Decoders/AbstractDecoder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66

77
use Intervention\Image\Drivers\SpecializableDecoder;
88
use Intervention\Image\Exceptions\DecoderException;
9+
use Intervention\Image\Exceptions\NotSupportedException;
910
use Intervention\Image\Interfaces\SpecializedInterface;
1011
use Intervention\Image\MediaType;
12+
use ValueError;
1113

1214
abstract class AbstractDecoder extends SpecializableDecoder implements SpecializedInterface
1315
{
1416
/**
1517
* Return media (mime) type of the file at given file path
1618
*
1719
* @throws DecoderException
20+
* @throws NotSupportedException
1821
*/
1922
protected function getMediaTypeByFilePath(string $filepath): MediaType
2023
{
@@ -24,13 +27,18 @@ protected function getMediaTypeByFilePath(string $filepath): MediaType
2427
throw new DecoderException('Unable to detect media (MIME) from data in file path.');
2528
}
2629

27-
return MediaType::from($info['mime']);
30+
try {
31+
return MediaType::from($info['mime']);
32+
} catch (ValueError) {
33+
throw new NotSupportedException('Unsupported media type (MIME) ' . $info['mime'] . '.');
34+
}
2835
}
2936

3037
/**
3138
* Return media (mime) type of the given image data
3239
*
3340
* @throws DecoderException
41+
* @throws NotSupportedException
3442
*/
3543
protected function getMediaTypeByBinary(string $data): MediaType
3644
{
@@ -40,6 +48,10 @@ protected function getMediaTypeByBinary(string $data): MediaType
4048
throw new DecoderException('Unable to detect media (MIME) from binary data.');
4149
}
4250

43-
return MediaType::from($info['mime']);
51+
try {
52+
return MediaType::from($info['mime']);
53+
} catch (ValueError) {
54+
throw new NotSupportedException('Unsupported media type (MIME) ' . $info['mime'] . '.');
55+
}
4456
}
4557
}

0 commit comments

Comments
 (0)