@@ -31,62 +31,43 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
31
31
$ newHeight = $ height ;
32
32
}
33
33
34
- $ watermark = imagecreatefrompng ($ wmImageSource );
35
-
36
- $ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
34
+ $ image = '' ;
37
35
if ($ infoImg ['mime ' ] == 'image/jpeg ' ) {
38
36
$ image = imagecreatefromjpeg ($ sourceURL );
39
- // Set the margins for the watermark and get the height/width of the watermark image
40
- $ positionRight = $ positionX ;
41
- $ positionBottom = $ positionY ;
42
- $ sx = imagesx ($ watermark );
43
- $ sy = imagesy ($ watermark );
44
- // width to calculate positioning of the watermark.
45
- imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
46
-
47
- imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
48
37
} elseif ($ infoImg ['mime ' ] == 'image/jpg ' ) {
49
38
$ image = imagecreatefromjpeg ($ sourceURL );
50
- // Set the margins for the watermark and get the height/width of the watermark image
51
- $ positionRight = $ positionX ;
52
- $ positionBottom = $ positionY ;
53
- $ sx = imagesx ($ watermark );
54
- $ sy = imagesy ($ watermark );
55
- // width to calculate positioning of the watermark.
56
- imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
57
-
58
- imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
59
39
} elseif ($ infoImg ['mime ' ] == 'image/png ' ) {
60
40
$ image = imagecreatefrompng ($ sourceURL );
61
- // Set the margins for the watermark and get the height/width of the watermark image
62
- $ positionRight = $ positionX ;
63
- $ positionBottom = $ positionY ;
64
- $ sx = imagesx ($ watermark );
65
- $ sy = imagesy ($ watermark );
66
- // width to calculate positioning of the watermark.
67
- imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
68
-
69
- imagealphablending ($ image , false );
70
- imagesavealpha ($ image , true );
71
-
72
- imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
73
41
} elseif ($ infoImg ['mime ' ] == 'image/gif ' ) {
74
42
$ image = imagecreatefromgif ($ sourceURL );
43
+ }
44
+
45
+
46
+ $ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
47
+
48
+ if (!empty ($ wmImageSource )) {
49
+ $ watermark = imagecreatefrompng ($ wmImageSource );
75
50
// Set the margins for the watermark and get the height/width of the watermark image
76
51
$ positionRight = $ positionX ;
77
52
$ positionBottom = $ positionY ;
78
53
$ sx = imagesx ($ watermark );
79
54
$ sy = imagesy ($ watermark );
80
55
// width to calculate positioning of the watermark.
81
56
imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
57
+ }
58
+ imagealphablending ($ imgResource , false );
59
+ imagesavealpha ($ imgResource , true );
82
60
83
- imagealphablending ( $ image , false );
84
- imagesavealpha ( $ image, true );
61
+ imagecopyresampled ( $ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
62
+ if ( $ infoImg [ ' mime ' ] == ' image/png ' || $ infoImg [ ' mime ' ] == ' image/gif ' ) {
85
63
86
- imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
64
+ $ newQuality = ($ quality / 10 ) - 1 ;
65
+ imagealphablending ($ imgResource , false );
66
+ imagesavealpha ($ imgResource , true );
67
+ $ RET = imagepng ($ imgResource , $ destinationURL , $ newQuality ); //For png quality range is 0-9
68
+ } else {
69
+ $ RET = imagejpeg ($ imgResource , $ destinationURL , $ quality );
87
70
}
88
-
89
- $ RET = imagejpeg ($ imgResource , $ destinationURL , $ quality );
90
71
imagedestroy ($ image );
91
72
return $ RET ;
92
73
}
0 commit comments