@@ -796,9 +796,9 @@ public function grayscale(): Image
796
796
* @param int $rotation Counterclockwise text rotation in degrees
797
797
* @return $this Fluent interface
798
798
*/
799
- 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
799
+ 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
800
800
{
801
- $ this ->writeTextAndGetBoundingBox ($ string , $ fontPath , $ fontSize , $ color , $ posX , $ posY , $ anchorX , $ anchorY , $ rotation );
801
+ $ this ->writeTextAndGetBoundingBox ($ string , $ fontPath , $ fontSize , $ color , $ posX , $ posY , $ anchorX , $ anchorY , $ rotation, $ letter_spacing );
802
802
return $ this ;
803
803
}
804
804
@@ -816,7 +816,7 @@ public function writeText(string $string, string $fontPath, int $fontSize, strin
816
816
* @param int $rotation Counterclockwise text rotation in degrees
817
817
* @return array Pixels positions of the
818
818
*/
819
- 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
819
+ 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
820
820
{
821
821
if (!$ this ->isImageDefined ()) {
822
822
return [];
@@ -844,7 +844,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
844
844
) {
845
845
if (
846
846
($ newImg = \imagecreatetruecolor (1 , 1 )) === false ||
847
- ($ posText = \imagettftext ($ newImg , $ fontSize , $ rotation , 0 , 0 , $ color , $ fontPath , $ string )) === false
847
+ ($ posText = $ this -> imagettftextWithSpacing ($ newImg , $ fontSize , $ rotation , 0 , 0 , $ color , $ fontPath , $ string, $ letter_spacing )) === false
848
848
) {
849
849
return [];
850
850
}
@@ -894,7 +894,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
894
894
}
895
895
}
896
896
897
- $ posText = \imagettftext ($ this ->image , $ fontSize , $ rotation , $ posX , $ posY , $ color , $ fontPath , $ string );
897
+ $ posText = $ this -> imagettftextWithSpacing ($ this ->image , $ fontSize , $ rotation , $ posX , $ posY , $ color , $ fontPath , $ string, $ letter_spacing );
898
898
899
899
if ($ posText === false ) {
900
900
return [];
@@ -927,6 +927,40 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
927
927
];
928
928
}
929
929
930
+ /**
931
+ * @param $image
932
+ * @param $size
933
+ * @param $angle
934
+ * @param $x
935
+ * @param $y
936
+ * @param $color
937
+ * @param $font
938
+ * @param $text
939
+ * @param int $spacing
940
+ * @return array|false
941
+ */
942
+ private function imagettftextWithSpacing ($ image , $ size , $ angle , $ x , $ y , $ color , $ font , $ text , int $ spacing = 0 )
943
+ {
944
+ if ($ spacing == 0 )
945
+ {
946
+ return imagettftext ($ image , $ size , $ angle , $ x , $ y , $ color , $ font , $ text );
947
+ }
948
+ else
949
+ {
950
+ $ temp_x = $ x ;
951
+ $ temp_y = $ y ;
952
+ $ posText = false ;
953
+ for ($ i = 0 ; $ i < strlen ($ text ); $ i ++)
954
+ {
955
+ $ posText = imagettftext ($ image , $ size , $ angle , $ temp_x , $ temp_y , $ color , $ font , $ text [$ i ]);
956
+ $ bbox = imagettfbbox ($ size , 0 , $ font , $ text [$ i ]);
957
+ $ temp_x += cos (deg2rad ($ angle )) * ($ spacing + ($ bbox [2 ] - $ bbox [0 ]));
958
+ $ temp_y -= sin (deg2rad ($ angle )) * ($ spacing + ($ bbox [2 ] - $ bbox [0 ]));
959
+ }
960
+ return $ posText ;
961
+ }
962
+ }
963
+
930
964
/**
931
965
* Draw a rectangle.
932
966
*
0 commit comments