Skip to content

Commit 7725f47

Browse files
Added optional letter spacing argument to writeText
1 parent 16ed5da commit 7725f47

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/Image.php

+39-5
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,9 @@ public function grayscale(): Image
796796
* @param int $rotation Counterclockwise text rotation in degrees
797797
* @return $this Fluent interface
798798
*/
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
800800
{
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);
802802
return $this;
803803
}
804804

@@ -816,7 +816,7 @@ public function writeText(string $string, string $fontPath, int $fontSize, strin
816816
* @param int $rotation Counterclockwise text rotation in degrees
817817
* @return array Pixels positions of the
818818
*/
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
820820
{
821821
if (!$this->isImageDefined()) {
822822
return [];
@@ -844,7 +844,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
844844
) {
845845
if (
846846
($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
848848
) {
849849
return [];
850850
}
@@ -894,7 +894,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
894894
}
895895
}
896896

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);
898898

899899
if ($posText === false) {
900900
return [];
@@ -927,6 +927,40 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
927927
];
928928
}
929929

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+
930964
/**
931965
* Draw a rectangle.
932966
*

0 commit comments

Comments
 (0)