@@ -31,62 +31,43 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
3131 $ newHeight = $ height ;
3232 }
3333
34- $ watermark = imagecreatefrompng ($ wmImageSource );
35-
36- $ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
34+ $ image = '' ;
3735 if ($ infoImg ['mime ' ] == 'image/jpeg ' ) {
3836 $ 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 );
4837 } elseif ($ infoImg ['mime ' ] == 'image/jpg ' ) {
4938 $ 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 );
5939 } elseif ($ infoImg ['mime ' ] == 'image/png ' ) {
6040 $ 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 );
7341 } elseif ($ infoImg ['mime ' ] == 'image/gif ' ) {
7442 $ image = imagecreatefromgif ($ sourceURL );
43+ }
44+
45+
46+ $ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
47+
48+ if (!empty ($ wmImageSource )) {
49+ $ watermark = imagecreatefrompng ($ wmImageSource );
7550 // Set the margins for the watermark and get the height/width of the watermark image
7651 $ positionRight = $ positionX ;
7752 $ positionBottom = $ positionY ;
7853 $ sx = imagesx ($ watermark );
7954 $ sy = imagesy ($ watermark );
8055 // width to calculate positioning of the watermark.
8156 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 );
8260
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 ' ) {
8563
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 );
8770 }
88-
89- $ RET = imagejpeg ($ imgResource , $ destinationURL , $ quality );
9071 imagedestroy ($ image );
9172 return $ RET ;
9273 }
0 commit comments