Skip to content

Commit bb5c632

Browse files
committed
Image: added detectTypeFromFile() and detectTypeFromString()
1 parent 36f531a commit bb5c632

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/Utils/Image.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
/**
16-
* Basic manipulation with images.
16+
* Basic manipulation with images. Supported types are JPEG, PNG, GIF, WEBP and BMP.
1717
*
1818
* <code>
1919
* $image = Image::fromFile('nette.jpg');
@@ -144,43 +144,43 @@ public static function rgb(int $red, int $green, int $blue, int $transparency =
144144

145145

146146
/**
147-
* Reads an image from a file and returns its type in $detectedFormat. Supported types are JPEG, PNG, GIF, WEBP and BMP.
147+
* Reads an image from a file and returns its type in $type.
148148
* @throws Nette\NotSupportedException if gd extension is not loaded
149149
* @throws UnknownImageFileException if file not found or file type is not known
150150
* @return static
151151
*/
152-
public static function fromFile(string $file, int &$detectedFormat = null)
152+
public static function fromFile(string $file, int &$type = null)
153153
{
154154
if (!extension_loaded('gd')) {
155155
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
156156
}
157157

158-
$detectedFormat = @getimagesize($file)[2]; // @ - files smaller than 12 bytes causes read error
159-
if (!isset(self::FORMATS[$detectedFormat])) {
160-
$detectedFormat = null;
158+
$type = self::detectTypeFromFile($file);
159+
if (!$type) {
161160
throw new UnknownImageFileException(is_file($file) ? "Unknown type of file '$file'." : "File '$file' not found.");
162161
}
163-
return new static(Callback::invokeSafe('imagecreatefrom' . image_type_to_extension($detectedFormat, false), [$file], function (string $message): void {
162+
163+
$method = 'imagecreatefrom' . self::FORMATS[$type];
164+
return new static(Callback::invokeSafe($method, [$file], function (string $message): void {
164165
throw new ImageException($message);
165166
}));
166167
}
167168

168169

169170
/**
170-
* Reads an image from a string and returns its type in $detectedFormat. Supported types are JPEG, PNG, GIF, WEBP and BMP.
171+
* Reads an image from a string and returns its type in $type.
171172
* @return static
172173
* @throws Nette\NotSupportedException if gd extension is not loaded
173174
* @throws ImageException
174175
*/
175-
public static function fromString(string $s, int &$detectedFormat = null)
176+
public static function fromString(string $s, int &$type = null)
176177
{
177178
if (!extension_loaded('gd')) {
178179
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
179180
}
180181

181-
$detectedFormat = @getimagesizefromstring($s)[2]; // @ - strings smaller than 12 bytes causes read error
182-
if (!isset(self::FORMATS[$detectedFormat])) {
183-
$detectedFormat = null;
182+
$type = self::detectTypeFromString($s);
183+
if (!$type) {
184184
throw new UnknownImageFileException('Unknown type of image.');
185185
}
186186

@@ -217,6 +217,26 @@ public static function fromBlank(int $width, int $height, array $color = null)
217217
}
218218

219219

220+
/**
221+
* Returns the type of image from file.
222+
*/
223+
public static function detectTypeFromFile(string $file): ?int
224+
{
225+
$type = @getimagesize($file)[2]; // @ - files smaller than 12 bytes causes read error
226+
return isset(self::FORMATS[$type]) ? $type : null;
227+
}
228+
229+
230+
/**
231+
* Returns the type of image from string.
232+
*/
233+
public static function detectTypeFromString(string $s): ?int
234+
{
235+
$type = @getimagesizefromstring($s)[2]; // @ - strings smaller than 12 bytes causes read error
236+
return isset(self::FORMATS[$type]) ? $type : null;
237+
}
238+
239+
220240
/**
221241
* Returns the file extension for the given `Image::XXX` constant.
222242
*/

tests/Utils/Image.factories.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ test('', function () {
1919
Assert::same(176, $image->getWidth());
2020
Assert::same(104, $image->getHeight());
2121
Assert::same(Image::GIF, $format);
22+
23+
Assert::same(Image::GIF, Image::detectTypeFromFile(__DIR__ . '/fixtures.images/logo.gif'));
2224
});
2325

2426

@@ -38,6 +40,9 @@ Assert::exception(function () {
3840
}, Nette\Utils\UnknownImageFileException::class, "File 'fixtures.images/missing.png' not found.");
3941

4042

43+
Assert::null(Image::detectTypeFromFile('fixtures.images/missing.png'));
44+
45+
4146
Assert::exception(function () {
4247
Image::fromFile(__DIR__ . '/fixtures.images/logo.tiff');
4348
}, Nette\Utils\UnknownImageFileException::class, "Unknown type of file '%a%fixtures.images/logo.tiff'.");
@@ -60,9 +65,14 @@ test('', function () {
6065
Assert::same(1, $image->getWidth());
6166
Assert::same(1, $image->getHeight());
6267
Assert::same(Image::GIF, $format);
68+
69+
Assert::same(Image::GIF, Image::detectTypeFromString(Image::EMPTY_GIF));
6370
});
6471

6572

6673
Assert::exception(function () {
6774
Image::fromString('abcdefg');
6875
}, Nette\Utils\UnknownImageFileException::class);
76+
77+
78+
Assert::null(Image::detectTypeFromString('x'));

0 commit comments

Comments
 (0)