3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
7
+ declare (strict_types=1 );
8
+
6
9
namespace Magento \Framework \Image \Adapter ;
7
10
8
11
/**
@@ -59,6 +62,9 @@ protected function _reset()
59
62
*/
60
63
public function open ($ filename )
61
64
{
65
+ if (filesize ($ filename ) === 0 ) {
66
+ throw new \InvalidArgumentException ("Wrong file size: ' {$ filename }'. " );
67
+ }
62
68
$ this ->_fileName = $ filename ;
63
69
$ this ->_reset ();
64
70
$ this ->getMimeType ();
@@ -225,18 +231,19 @@ public function getImage()
225
231
* @param null|int $fileType
226
232
* @param string $unsupportedText
227
233
* @return string
228
- * @throws \Exception
234
+ * @throws \InvalidArgumentException
235
+ * @throws \BadFunctionCallException
229
236
*/
230
237
private function _getCallback ($ callbackType , $ fileType = null , $ unsupportedText = 'Unsupported image format. ' )
231
238
{
232
239
if (null === $ fileType ) {
233
240
$ fileType = $ this ->_fileType ;
234
241
}
235
242
if (empty (self ::$ _callbacks [$ fileType ])) {
236
- throw new \Exception ($ unsupportedText );
243
+ throw new \InvalidArgumentException ($ unsupportedText );
237
244
}
238
245
if (empty (self ::$ _callbacks [$ fileType ][$ callbackType ])) {
239
- throw new \Exception ('Callback not found. ' );
246
+ throw new \BadFunctionCallException ('Callback not found. ' );
240
247
}
241
248
return self ::$ _callbacks [$ fileType ][$ callbackType ];
242
249
}
@@ -248,7 +255,7 @@ private function _getCallback($callbackType, $fileType = null, $unsupportedText
248
255
*
249
256
* @param resource &$imageResourceTo
250
257
* @return int
251
- * @throws \Exception
258
+ * @throws \InvalidArgumentException
252
259
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
253
260
*/
254
261
private function _fillBackgroundColor (&$ imageResourceTo )
@@ -261,17 +268,17 @@ private function _fillBackgroundColor(&$imageResourceTo)
261
268
// fill truecolor png with alpha transparency
262
269
if ($ isAlpha ) {
263
270
if (!imagealphablending ($ imageResourceTo , false )) {
264
- throw new \Exception ('Failed to set alpha blending for PNG image. ' );
271
+ throw new \InvalidArgumentException ('Failed to set alpha blending for PNG image. ' );
265
272
}
266
273
$ transparentAlphaColor = imagecolorallocatealpha ($ imageResourceTo , 0 , 0 , 0 , 127 );
267
274
if (false === $ transparentAlphaColor ) {
268
- throw new \Exception ('Failed to allocate alpha transparency for PNG image. ' );
275
+ throw new \InvalidArgumentException ('Failed to allocate alpha transparency for PNG image. ' );
269
276
}
270
277
if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentAlphaColor )) {
271
- throw new \Exception ('Failed to fill PNG image with alpha transparency. ' );
278
+ throw new \InvalidArgumentException ('Failed to fill PNG image with alpha transparency. ' );
272
279
}
273
280
if (!imagesavealpha ($ imageResourceTo , true )) {
274
- throw new \Exception ('Failed to save alpha transparency into PNG image. ' );
281
+ throw new \InvalidArgumentException ('Failed to save alpha transparency into PNG image. ' );
275
282
}
276
283
277
284
return $ transparentAlphaColor ;
@@ -283,22 +290,22 @@ private function _fillBackgroundColor(&$imageResourceTo)
283
290
$ transparentColor = imagecolorallocate ($ imageResourceTo , $ r , $ g , $ b );
284
291
}
285
292
if (false === $ transparentColor ) {
286
- throw new \Exception ('Failed to allocate transparent color for image. ' );
293
+ throw new \InvalidArgumentException ('Failed to allocate transparent color for image. ' );
287
294
}
288
295
if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentColor )) {
289
- throw new \Exception ('Failed to fill image with transparency. ' );
296
+ throw new \InvalidArgumentException ('Failed to fill image with transparency. ' );
290
297
}
291
298
imagecolortransparent ($ imageResourceTo , $ transparentColor );
292
299
return $ transparentColor ;
293
300
}
294
301
} catch (\Exception $ e ) {
295
- // fallback to default background color
302
+ throw new \ DomainException ( ' Failed to fill image. ' );
296
303
}
297
304
}
298
305
list ($ r , $ g , $ b ) = $ this ->_backgroundColor ;
299
306
$ color = imagecolorallocate ($ imageResourceTo , $ r , $ g , $ b );
300
307
if (!imagefill ($ imageResourceTo , 0 , 0 , $ color )) {
301
- throw new \Exception ("Failed to fill image background with color {$ r } {$ g } {$ b }. " );
308
+ throw new \InvalidArgumentException ("Failed to fill image background with color {$ r } {$ g } {$ b }. " );
302
309
}
303
310
304
311
return $ color ;
@@ -637,13 +644,13 @@ public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
637
644
* Checks required dependencies
638
645
*
639
646
* @return void
640
- * @throws \Exception If some of dependencies are missing
647
+ * @throws \RuntimeException If some of dependencies are missing
641
648
*/
642
649
public function checkDependencies ()
643
650
{
644
651
foreach ($ this ->_requiredExtensions as $ value ) {
645
652
if (!extension_loaded ($ value )) {
646
- throw new \Exception ("Required PHP extension ' {$ value }' was not loaded. " );
653
+ throw new \RuntimeException ("Required PHP extension ' {$ value }' was not loaded. " );
647
654
}
648
655
}
649
656
}
@@ -755,7 +762,7 @@ protected function _createImageFromText($text)
755
762
* @param string $text
756
763
* @param string $font
757
764
* @return void
758
- * @throws \Exception
765
+ * @throws \InvalidArgumentException
759
766
*/
760
767
protected function _createImageFromTtfText ($ text , $ font )
761
768
{
@@ -777,7 +784,7 @@ protected function _createImageFromTtfText($text, $font)
777
784
$ text
778
785
);
779
786
if ($ result === false ) {
780
- throw new \Exception ('Unable to create TTF text ' );
787
+ throw new \InvalidArgumentException ('Unable to create TTF text ' );
781
788
}
782
789
}
783
790
0 commit comments