|
13 | 13 |
|
14 | 14 |
|
15 | 15 | /** |
16 | | - * Basic manipulation with images. |
| 16 | + * Basic manipulation with images. Supported types are JPEG, PNG, GIF, WEBP and BMP. |
17 | 17 | * |
18 | 18 | * <code> |
19 | 19 | * $image = Image::fromFile('nette.jpg'); |
@@ -144,43 +144,43 @@ public static function rgb(int $red, int $green, int $blue, int $transparency = |
144 | 144 |
|
145 | 145 |
|
146 | 146 | /** |
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. |
148 | 148 | * @throws Nette\NotSupportedException if gd extension is not loaded |
149 | 149 | * @throws UnknownImageFileException if file not found or file type is not known |
150 | 150 | * @return static |
151 | 151 | */ |
152 | | - public static function fromFile(string $file, int &$detectedFormat = null) |
| 152 | + public static function fromFile(string $file, int &$type = null) |
153 | 153 | { |
154 | 154 | if (!extension_loaded('gd')) { |
155 | 155 | throw new Nette\NotSupportedException('PHP extension GD is not loaded.'); |
156 | 156 | } |
157 | 157 |
|
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) { |
161 | 160 | throw new UnknownImageFileException(is_file($file) ? "Unknown type of file '$file'." : "File '$file' not found."); |
162 | 161 | } |
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 { |
164 | 165 | throw new ImageException($message); |
165 | 166 | })); |
166 | 167 | } |
167 | 168 |
|
168 | 169 |
|
169 | 170 | /** |
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. |
171 | 172 | * @return static |
172 | 173 | * @throws Nette\NotSupportedException if gd extension is not loaded |
173 | 174 | * @throws ImageException |
174 | 175 | */ |
175 | | - public static function fromString(string $s, int &$detectedFormat = null) |
| 176 | + public static function fromString(string $s, int &$type = null) |
176 | 177 | { |
177 | 178 | if (!extension_loaded('gd')) { |
178 | 179 | throw new Nette\NotSupportedException('PHP extension GD is not loaded.'); |
179 | 180 | } |
180 | 181 |
|
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) { |
184 | 184 | throw new UnknownImageFileException('Unknown type of image.'); |
185 | 185 | } |
186 | 186 |
|
@@ -217,6 +217,26 @@ public static function fromBlank(int $width, int $height, array $color = null) |
217 | 217 | } |
218 | 218 |
|
219 | 219 |
|
| 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 | + |
220 | 240 | /** |
221 | 241 | * Returns the file extension for the given `Image::XXX` constant. |
222 | 242 | */ |
|
0 commit comments