@@ -136,7 +136,7 @@ public function resetCanvas(int $width, int $height): Image
136136
137137 \imagealphablending ($ this ->image , false );
138138 \imagesavealpha ($ this ->image , true );
139- \imagecolortransparent ($ this ->image , \imagecolorallocate ($ this ->image , 0 , 0 , 0 ));
139+ \imagefill ($ this ->image , 0 , 0 , \imagecolorallocatealpha ($ this ->image , 0 , 0 , 0 , 127 ));
140140
141141 $ this ->width = $ width ;
142142 $ this ->height = $ height ;
@@ -478,14 +478,9 @@ public function resize(int $width, int $height): Image
478478 return $ this ;
479479 }
480480
481- if (
482- ($ image = \imagecreatetruecolor ($ width , $ height )) !== false &&
483- \imagealphablending ($ image , false ) !== false &&
484- \imagesavealpha ($ image , true ) !== false &&
485- ($ transparent = $ this ->colorAllocate ('#000000FF ' )) !== false &&
486- \imagefill ($ image , 0 , 0 , $ transparent ) !== false &&
487- \imagecopyresampled ($ image , $ this ->image , 0 , 0 , 0 , 0 , $ width , $ height , $ this ->width , $ this ->height ) !== false
488- ) {
481+ $ image = Image::newCanvas ($ width , $ height )->getImage ();
482+
483+ if (\imagecopyresampled ($ image , $ this ->image , 0 , 0 , 0 , 0 , $ width , $ height , $ this ->width , $ this ->height ) !== false ) {
489484 $ this ->image = $ image ;
490485 $ this ->width = $ width ;
491486 $ this ->height = $ height ;
@@ -512,7 +507,6 @@ public function downscaleAndCrop(int $width, int $height, $posX = Image::ALIGN_C
512507 $ height = $ this ->height ;
513508 }
514509
515-
516510 $ finalWidth = \round ($ this ->width * $ height / $ this ->height );
517511 $ finalHeight = $ height ;
518512
@@ -724,6 +718,10 @@ public function alphaMask(Image $mask): Image
724718
725719 if ($ alpha != 127 ) {
726720 $ rgb = \imagecolorat ($ this ->image , $ j , $ i );
721+ $ alpha = 127 - \ceil ((127 - (($ rgb >> 24 ) & 0x7F )) * (127 - $ alpha ) / 127 );
722+ }
723+
724+ if ($ alpha != 127 ) {
727725 $ red = ($ rgb >> 16 ) & 0xFF ;
728726 $ green = ($ rgb >> 8 ) & 0xFF ;
729727 $ blue = $ rgb & 0xFF ;
@@ -746,6 +744,25 @@ public function alphaMask(Image $mask): Image
746744 return $ this ;
747745 }
748746
747+ /**
748+ * change the image opacity
749+ *
750+ * @param float $opacity Opacity (0 to 1)
751+ * @return $this Fluent interface
752+ */
753+ public function setOpacity (float $ opacity ): Image
754+ {
755+ if (!$ this ->isImageDefined ()) {
756+ return $ this ;
757+ }
758+
759+ \imagealphablending ($ this ->image , false );
760+ \imagesavealpha ($ this ->image , true );
761+ \imagefilter ($ this ->image , IMG_FILTER_COLORIZE , 0 , 0 , 0 , 127 * (1 - $ opacity ));
762+
763+ return $ this ;
764+ }
765+
749766 //===============================================================================================================================
750767 //=========================================================POST PROD=============================================================
751768 //===============================================================================================================================
@@ -1204,18 +1221,17 @@ public function displayGIF()
12041221 /**
12051222 * Get image raw data
12061223 *
1207- * @param string $nameFunction Image function to be called
1208- * @param int $quality JPG quality : 0 to 100
1224+ * @param callable $imgFunction Image function to be called
12091225 * @return string Data
12101226 */
1211- private function getData (string $ nameFunction , int $ quality = - 1 ): string
1227+ private function getData (callable $ imgFunction ): string
12121228 {
12131229 if (!$ this ->isImageDefined ()) {
12141230 return '' ;
12151231 }
12161232
12171233 \ob_start ();
1218- $ nameFunction ( $ this -> image , null , $ quality , - 1 );
1234+ $ imgFunction ( );
12191235 $ image_data = \ob_get_contents ();
12201236 \ob_end_clean ();
12211237
@@ -1229,7 +1245,7 @@ private function getData(string $nameFunction, int $quality = -1): string
12291245 */
12301246 public function getDataPNG (): string
12311247 {
1232- return $ this ->getData (' imagepng ' );
1248+ return $ this ->getData (function () { $ this -> displayPNG ();} );
12331249 }
12341250
12351251 /**
@@ -1240,7 +1256,7 @@ public function getDataPNG(): string
12401256 */
12411257 public function getDataJPG (int $ quality = -1 ): string
12421258 {
1243- return $ this ->getData (' imagejpeg ' , $ quality );
1259+ return $ this ->getData (function () use ( $ quality) { $ this -> displayJPG ( $ quality );} );
12441260 }
12451261
12461262 /**
@@ -1250,7 +1266,7 @@ public function getDataJPG(int $quality = -1): string
12501266 */
12511267 public function getDataGIF (): string
12521268 {
1253- return $ this ->getData (' imagegif ' );
1269+ return $ this ->getData (function () { $ this -> displayGIF ();} );
12541270 }
12551271
12561272 /**
0 commit comments