@@ -465,7 +465,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
465
465
} elseif ($ this ->getWatermarkPosition () == self ::POSITION_CENTER ) {
466
466
$ positionX = $ this ->_imageSrcWidth / 2 - imagesx ($ watermark ) / 2 ;
467
467
$ positionY = $ this ->_imageSrcHeight / 2 - imagesy ($ watermark ) / 2 ;
468
- $ this ->imagecopymergeWithAlphaFix (
468
+ $ this ->copyImageWithAlphaPercentage (
469
469
$ this ->_imageHandler ,
470
470
$ watermark ,
471
471
$ positionX ,
@@ -478,7 +478,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
478
478
);
479
479
} elseif ($ this ->getWatermarkPosition () == self ::POSITION_TOP_RIGHT ) {
480
480
$ positionX = $ this ->_imageSrcWidth - imagesx ($ watermark );
481
- $ this ->imagecopymergeWithAlphaFix (
481
+ $ this ->copyImageWithAlphaPercentage (
482
482
$ this ->_imageHandler ,
483
483
$ watermark ,
484
484
$ positionX ,
@@ -490,7 +490,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
490
490
$ this ->getWatermarkImageOpacity ()
491
491
);
492
492
} elseif ($ this ->getWatermarkPosition () == self ::POSITION_TOP_LEFT ) {
493
- $ this ->imagecopymergeWithAlphaFix (
493
+ $ this ->copyImageWithAlphaPercentage (
494
494
$ this ->_imageHandler ,
495
495
$ watermark ,
496
496
$ positionX ,
@@ -504,7 +504,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
504
504
} elseif ($ this ->getWatermarkPosition () == self ::POSITION_BOTTOM_RIGHT ) {
505
505
$ positionX = $ this ->_imageSrcWidth - imagesx ($ watermark );
506
506
$ positionY = $ this ->_imageSrcHeight - imagesy ($ watermark );
507
- $ this ->imagecopymergeWithAlphaFix (
507
+ $ this ->copyImageWithAlphaPercentage (
508
508
$ this ->_imageHandler ,
509
509
$ watermark ,
510
510
$ positionX ,
@@ -517,7 +517,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
517
517
);
518
518
} elseif ($ this ->getWatermarkPosition () == self ::POSITION_BOTTOM_LEFT ) {
519
519
$ positionY = $ this ->_imageSrcHeight - imagesy ($ watermark );
520
- $ this ->imagecopymergeWithAlphaFix (
520
+ $ this ->copyImageWithAlphaPercentage (
521
521
$ this ->_imageHandler ,
522
522
$ watermark ,
523
523
$ positionX ,
@@ -531,7 +531,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
531
531
}
532
532
533
533
if ($ tile === false && $ merged === false ) {
534
- $ this ->imagecopymergeWithAlphaFix (
534
+ $ this ->copyImageWithAlphaPercentage (
535
535
$ this ->_imageHandler ,
536
536
$ watermark ,
537
537
$ positionX ,
@@ -547,7 +547,7 @@ public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity =
547
547
$ offsetY = $ positionY ;
548
548
while ($ offsetY <= $ this ->_imageSrcHeight + imagesy ($ watermark )) {
549
549
while ($ offsetX <= $ this ->_imageSrcWidth + imagesx ($ watermark )) {
550
- $ this ->imagecopymergeWithAlphaFix (
550
+ $ this ->copyImageWithAlphaPercentage (
551
551
$ this ->_imageHandler ,
552
552
$ watermark ,
553
553
$ offsetX ,
@@ -780,64 +780,71 @@ protected function _createEmptyImage($width, $height)
780
780
}
781
781
782
782
/**
783
- * Fix an issue with the usage of imagecopymerge where the alpha channel is lost
783
+ * Copy source image onto destination image with given alpha percentage
784
784
*
785
- * @param resource $dst_im
786
- * @param resource $src_im
787
- * @param int $dst_x
788
- * @param int $dst_y
789
- * @param int $src_x
790
- * @param int $src_y
791
- * @param int $src_w
792
- * @param int $src_h
793
- * @param int $pct
785
+ * @internal The arguments and functionality is the same as imagecopymerge
786
+ * but with proper handling of alpha transparency
787
+ *
788
+ * @param resource $destinationImage
789
+ * @param resource $sourceImage
790
+ * @param int $destinationX
791
+ * @param int $destinationY
792
+ * @param int $sourceX
793
+ * @param int $sourceY
794
+ * @param int $sourceWidth
795
+ * @param int $sourceHeight
796
+ * @param int $alphaPercentage
794
797
*
795
798
* @return bool
796
799
*/
797
- private function imagecopymergeWithAlphaFix (
798
- $ dst_im ,
799
- $ src_im ,
800
- $ dst_x ,
801
- $ dst_y ,
802
- $ src_x ,
803
- $ src_y ,
804
- $ src_w ,
805
- $ src_h ,
806
- $ pct
800
+ private function copyImageWithAlphaPercentage (
801
+ $ destinationImage ,
802
+ $ sourceImage ,
803
+ $ destinationX ,
804
+ $ destinationY ,
805
+ $ sourceX ,
806
+ $ sourceY ,
807
+ $ sourceWidth ,
808
+ $ sourceHeight ,
809
+ $ alphaPercentage
807
810
) {
808
- if ($ pct >= 100 ) {
809
- return imagecopy ($ dst_im , $ src_im , $ dst_x , $ dst_y , $ src_x , $ src_y , $ src_w , $ src_h );
811
+ if (imageistruecolor ($ destinationImage ) === false || imageistruecolor ($ sourceImage ) === false ) {
812
+ return imagecopymerge ($ destinationImage , $ sourceImage , $ destinationX , $ destinationY , $ sourceX , $ sourceY , $ sourceWidth , $ sourceHeight , $ alphaPercentage );
813
+ }
814
+
815
+ if ($ alphaPercentage >= 100 ) {
816
+ return imagecopy ($ destinationImage , $ sourceImage , $ destinationX , $ destinationY , $ sourceX , $ sourceY , $ sourceWidth , $ sourceHeight );
810
817
}
811
818
812
- if ($ pct < 0 ) {
819
+ if ($ alphaPercentage < 0 ) {
813
820
return false ;
814
821
}
815
822
816
- $ sizeX = imagesx ($ src_im );
817
- $ sizeY = imagesy ($ src_im );
818
- if (false === $ sizeX || false === $ sizeY ) {
823
+ $ sizeX = imagesx ($ sourceImage );
824
+ $ sizeY = imagesy ($ sourceImage );
825
+ if ($ sizeX === false || $ sizeY === false || $ sizeX === 0 || $ sizeY === 0 ) {
819
826
return false ;
820
827
}
821
828
822
- $ tmpImg = imagecreatetruecolor ($ src_w , $ src_h );
823
- if (false === $ tmpImg ) {
829
+ $ tmpImg = imagecreatetruecolor ($ sourceWidth , $ sourceHeight );
830
+ if ($ tmpImg === false ) {
824
831
return false ;
825
832
}
826
833
827
- if (false === imagealphablending ($ tmpImg , false )) {
834
+ if (imagealphablending ($ tmpImg , false ) === false ) {
828
835
return false ;
829
836
}
830
837
831
- if (false === imagecopy ($ tmpImg , $ src_im , 0 , 0 , 0 , 0 , $ sizeX , $ sizeY )) {
838
+ if (imagecopy ($ tmpImg , $ sourceImage , 0 , 0 , 0 , 0 , $ sizeX , $ sizeY ) === false ) {
832
839
return false ;
833
840
}
834
841
835
- $ transparancy = 127 - (($ pct *127 )/100 );
836
- if (false === imagefilter ($ tmpImg , IMG_FILTER_COLORIZE , 0 , 0 , 0 , $ transparancy ) ) {
842
+ $ transparency = 127 - (($ alphaPercentage *127 )/100 );
843
+ if (imagefilter ($ tmpImg , IMG_FILTER_COLORIZE , 0 , 0 , 0 , $ transparency ) === false ) {
837
844
return false ;
838
845
}
839
846
840
- $ result = imagecopy ($ dst_im , $ tmpImg , $ dst_x , $ dst_y , $ src_x , $ src_y , $ src_w , $ src_h );
847
+ $ result = imagecopy ($ destinationImage , $ tmpImg , $ destinationX , $ destinationY , $ sourceX , $ sourceY , $ sourceWidth , $ sourceHeight );
841
848
imagedestroy ($ tmpImg );
842
849
843
850
return $ result ;
0 commit comments