2424 */
2525class Resizer
2626{
27- public $ sourceFile = '' ;
28- public $ endFile = '' ;
29- public $ maxWidth = 0 ;
30- public $ maxHeight = 0 ;
31- public $ imageMimetype = '' ;
32- public $ jpgQuality = 90 ;
33- public $ mergeType = 0 ;
34- public $ mergePos = 0 ;
35- public $ degrees = 0 ;
36- public $ error = '' ;
27+ public string $ sourceFile = '' ;
28+ public string $ endFile = '' ;
29+ public int $ maxWidth = 0 ;
30+ public int $ maxHeight = 0 ;
31+ public string $ imageMimetype = '' ;
32+ public int $ jpgQuality = 90 ;
33+ public int $ mergeType = 0 ;
34+ public int $ mergePos = 0 ;
35+ public int $ degrees = 0 ;
36+ public string $ error = '' ;
3737
3838 /**
3939 * resize image if size exceed given width/height
@@ -45,15 +45,24 @@ public function resizeImage()
4545 switch ($ this ->imageMimetype ) {
4646 case 'image/png ' :
4747 $ img = \imagecreatefrompng ($ this ->sourceFile );
48+ if (!$ img ) {
49+ return false ;
50+ }
4851 break ;
4952 case 'image/jpeg ' :
5053 $ img = \imagecreatefromjpeg ($ this ->sourceFile );
5154 if (!$ img ) {
5255 $ img = \imagecreatefromstring (file_get_contents ($ this ->sourceFile ));
5356 }
57+ if (!$ img ) {
58+ return false ;
59+ }
5460 break ;
5561 case 'image/gif ' :
5662 $ img = \imagecreatefromgif ($ this ->sourceFile );
63+ if (!$ img ) {
64+ return false ;
65+ }
5766 break ;
5867 default :
5968 return 'Unsupported format ' ;
@@ -83,12 +92,12 @@ public function resizeImage()
8392 }
8493
8594 // Create a new temporary image.
86- $ tmpimg = \imagecreatetruecolor ($ new_width , $ new_height );
95+ $ tmpimg = \imagecreatetruecolor (( int ) $ new_width , ( int ) $ new_height );
8796 \imagealphablending ($ tmpimg , false );
8897 \imagesavealpha ($ tmpimg , true );
8998
9099 // Copy and resize old image into new image.
91- \imagecopyresampled ($ tmpimg , $ img , 0 , 0 , 0 , 0 , $ new_width , $ new_height , $ width , $ height );
100+ \imagecopyresampled ($ tmpimg , $ img , 0 , 0 , 0 , 0 , ( int ) $ new_width , ( int ) $ new_height , ( int ) $ width , ( int ) $ height );
92101
93102 \unlink ($ this ->endFile );
94103 //compressing the file
@@ -185,8 +194,17 @@ public function resizeAndCrop()
185194
186195 public function mergeImage (): void
187196 {
188- $ dest = \imagecreatefromjpeg ($ this ->endFile );
189- $ src = \imagecreatefromjpeg ($ this ->sourceFile );
197+ switch ($ this ->imageMimetype ) {
198+ case 'image/png ' :
199+ $ dest = \imagecreatefrompng ($ this ->endFile );
200+ $ src = \imagecreatefrompng ($ this ->sourceFile );
201+ break ;
202+ case 'image/jpeg ' :
203+ $ dest = \imagecreatefromjpeg ($ this ->endFile );
204+ $ src = \imagecreatefromjpeg ($ this ->sourceFile );
205+ break ;
206+ // ... etc
207+ }
190208 if (4 == $ this ->mergeType ) {
191209 $ imgWidth = (int )\round ($ this ->maxWidth / 2 - 1 );
192210 $ imgHeight = (int )\round ($ this ->maxHeight / 2 - 1 );
0 commit comments