@@ -404,7 +404,7 @@ public function rotate($angle)
404
404
*/
405
405
public function watermark ($ imagePath , $ positionX = 0 , $ positionY = 0 , $ opacity = 30 , $ tile = false )
406
406
{
407
- list ($ watermarkSrcWidth , $ watermarkSrcHeight , $ watermarkFileType , ) = $ this ->_getImageOptions ($ imagePath );
407
+ list ($ watermarkSrcWidth , $ watermarkSrcHeight , $ watermarkFileType ,) = $ this ->_getImageOptions ($ imagePath );
408
408
$ this ->_getFileAttributes ();
409
409
$ watermark = call_user_func (
410
410
$ this ->_getCallback ('create ' , $ watermarkFileType , 'Unsupported watermark image format. ' ),
@@ -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
- imagecopymerge (
468
+ $ this -> imagecopymergeWithAlphaFix (
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
- imagecopymerge (
481
+ $ this -> imagecopymergeWithAlphaFix (
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
- imagecopymerge (
493
+ $ this -> imagecopymergeWithAlphaFix (
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
- imagecopymerge (
507
+ $ this -> imagecopymergeWithAlphaFix (
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
- imagecopymerge (
520
+ $ this -> imagecopymergeWithAlphaFix (
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
- imagecopymerge (
534
+ $ this -> imagecopymergeWithAlphaFix (
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
- imagecopymerge (
550
+ $ this -> imagecopymergeWithAlphaFix (
551
551
$ this ->_imageHandler ,
552
552
$ watermark ,
553
553
$ offsetX ,
@@ -778,4 +778,68 @@ protected function _createEmptyImage($width, $height)
778
778
$ this ->imageDestroy ();
779
779
$ this ->_imageHandler = $ image ;
780
780
}
781
+
782
+ /**
783
+ * Fix an issue with the usage of imagecopymerge where the alpha channel is lost
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
794
+ *
795
+ * @return bool
796
+ */
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
807
+ ) {
808
+ if ($ pct >= 100 ) {
809
+ return imagecopy ($ dst_im , $ src_im , $ dst_x , $ dst_y , $ src_x , $ src_y , $ src_w , $ src_h );
810
+ }
811
+
812
+ if ($ pct < 0 ) {
813
+ return false ;
814
+ }
815
+
816
+ $ sizeX = imagesx ($ src_im );
817
+ $ sizeY = imagesy ($ src_im );
818
+ if (false === $ sizeX || false === $ sizeY ) {
819
+ return false ;
820
+ }
821
+
822
+ $ tmpImg = imagecreatetruecolor ($ src_w , $ src_h );
823
+ if (false === $ tmpImg ) {
824
+ return false ;
825
+ }
826
+
827
+ if (false === imagealphablending ($ tmpImg , false )) {
828
+ return false ;
829
+ }
830
+
831
+ if (false === imagecopy ($ tmpImg , $ src_im , 0 , 0 , 0 , 0 , $ sizeX , $ sizeY )) {
832
+ return false ;
833
+ }
834
+
835
+ $ transparancy = 127 - (($ pct *127 )/100 );
836
+ if (false === imagefilter ($ tmpImg , IMG_FILTER_COLORIZE , 0 , 0 , 0 , $ transparancy )) {
837
+ return false ;
838
+ }
839
+
840
+ $ result = imagecopy ($ dst_im , $ tmpImg , $ dst_x , $ dst_y , $ src_x , $ src_y , $ src_w , $ src_h );
841
+ imagedestroy ($ tmpImg );
842
+
843
+ return $ result ;
844
+ }
781
845
}
0 commit comments