@@ -810,9 +810,9 @@ public function grayscale(): Image
810
810
* @param int $rotation Counterclockwise text rotation in degrees
811
811
* @return $this Fluent interface
812
812
*/
813
- public function writeText (string $ string , string $ fontPath , int $ fontSize , string $ color = '#ffffff ' , $ posX = 0 , $ posY = 0 , string $ anchorX = Image::ALIGN_CENTER , string $ anchorY = Image::ALIGN_MIDDLE , int $ rotation = 0 ): Image
813
+ public function writeText (string $ string , string $ fontPath , int $ fontSize , string $ color = '#ffffff ' , $ posX = 0 , $ posY = 0 , string $ anchorX = Image::ALIGN_CENTER , string $ anchorY = Image::ALIGN_MIDDLE , int $ rotation = 0 , int $ letter_spacing = 0 ): Image
814
814
{
815
- $ this ->writeTextAndGetBoundingBox ($ string , $ fontPath , $ fontSize , $ color , $ posX , $ posY , $ anchorX , $ anchorY , $ rotation );
815
+ $ this ->writeTextAndGetBoundingBox ($ string , $ fontPath , $ fontSize , $ color , $ posX , $ posY , $ anchorX , $ anchorY , $ rotation, $ letter_spacing );
816
816
return $ this ;
817
817
}
818
818
@@ -830,7 +830,7 @@ public function writeText(string $string, string $fontPath, int $fontSize, strin
830
830
* @param int $rotation Counterclockwise text rotation in degrees
831
831
* @return array Pixels positions of the
832
832
*/
833
- public function writeTextAndGetBoundingBox (string $ string , string $ fontPath , int $ fontSize , string $ color = '#ffffff ' , $ posX = 0 , $ posY = 0 , string $ anchorX = Image::ALIGN_CENTER , string $ anchorY = Image::ALIGN_MIDDLE , int $ rotation = 0 ): array
833
+ public function writeTextAndGetBoundingBox (string $ string , string $ fontPath , int $ fontSize , string $ color = '#ffffff ' , $ posX = 0 , $ posY = 0 , string $ anchorX = Image::ALIGN_CENTER , string $ anchorY = Image::ALIGN_MIDDLE , int $ rotation = 0 , int $ letter_spacing = 0 ): array
834
834
{
835
835
if (!$ this ->isImageDefined ()) {
836
836
return [];
@@ -858,7 +858,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
858
858
) {
859
859
if (
860
860
($ newImg = \imagecreatetruecolor (1 , 1 )) === false ||
861
- ($ posText = \imagettftext ($ newImg , $ fontSize , $ rotation , 0 , 0 , $ color , $ fontPath , $ string )) === false
861
+ ($ posText = $ this -> imagettftextWithSpacing ($ newImg , $ fontSize , $ rotation , 0 , 0 , $ color , $ fontPath , $ string, $ letter_spacing )) === false
862
862
) {
863
863
return [];
864
864
}
@@ -908,7 +908,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
908
908
}
909
909
}
910
910
911
- $ posText = \imagettftext ($ this ->image , $ fontSize , $ rotation , $ posX , $ posY , $ color , $ fontPath , $ string );
911
+ $ posText = $ this -> imagettftextWithSpacing ($ this ->image , $ fontSize , $ rotation , $ posX , $ posY , $ color , $ fontPath , $ string, $ letter_spacing );
912
912
913
913
if ($ posText === false ) {
914
914
return [];
@@ -941,6 +941,40 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
941
941
];
942
942
}
943
943
944
+ /**
945
+ * @param $image
946
+ * @param $size
947
+ * @param $angle
948
+ * @param $x
949
+ * @param $y
950
+ * @param $color
951
+ * @param $font
952
+ * @param $text
953
+ * @param int $spacing
954
+ * @return array
955
+ */
956
+ private function imagettftextWithSpacing ($ image , float $ size , float $ angle , float $ x , float $ y , int $ color , string $ font , string $ text , int $ spacing = 0 )
957
+ {
958
+ if ($ spacing == 0 )
959
+ {
960
+ return \imagettftext ($ image , $ size , $ angle , $ x , $ y , $ color , $ font , $ text );
961
+ }
962
+ else
963
+ {
964
+ $ temp_x = $ x ;
965
+ $ temp_y = $ y ;
966
+ $ posText = [];
967
+ for ($ i = 0 ; $ i < \mb_strlen ($ text ); ++$ i )
968
+ {
969
+ $ posText = \imagettftext ($ image , $ size , $ angle , $ temp_x , $ temp_y , $ color , $ font , $ text [$ i ]);
970
+ $ bbox = \imagettfbbox ($ size , 0 , $ font , $ text [$ i ]);
971
+ $ temp_x += \cos (\deg2rad ($ angle )) * ($ spacing + ($ bbox [2 ] - $ bbox [0 ]));
972
+ $ temp_y -= \sin (\deg2rad ($ angle )) * ($ spacing + ($ bbox [2 ] - $ bbox [0 ]));
973
+ }
974
+ return $ posText ;
975
+ }
976
+ }
977
+
944
978
/**
945
979
* Draw a rectangle.
946
980
*
0 commit comments