@@ -810,9 +810,9 @@ public function grayscale(): Image
810810 * @param int $rotation Counterclockwise text rotation in degrees
811811 * @return $this Fluent interface
812812 */
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
814814 {
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 );
816816 return $ this ;
817817 }
818818
@@ -830,7 +830,7 @@ public function writeText(string $string, string $fontPath, int $fontSize, strin
830830 * @param int $rotation Counterclockwise text rotation in degrees
831831 * @return array Pixels positions of the
832832 */
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
834834 {
835835 if (!$ this ->isImageDefined ()) {
836836 return [];
@@ -858,7 +858,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
858858 ) {
859859 if (
860860 ($ 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
862862 ) {
863863 return [];
864864 }
@@ -908,7 +908,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
908908 }
909909 }
910910
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 );
912912
913913 if ($ posText === false ) {
914914 return [];
@@ -941,6 +941,40 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
941941 ];
942942 }
943943
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+
944978 /**
945979 * Draw a rectangle.
946980 *
0 commit comments