Skip to content

Commit

Permalink
Merge pull request #4 from roelofjan-elsinga/master
Browse files Browse the repository at this point in the history
Added optional letter spacing argument to writeText
  • Loading branch information
DantSu authored Nov 9, 2022
2 parents 1188b95 + be3aa36 commit 9a6f48c
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,9 @@ public function grayscale(): Image
* @param int $rotation Counterclockwise text rotation in degrees
* @return $this Fluent interface
*/
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
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
{
$this->writeTextAndGetBoundingBox($string, $fontPath, $fontSize, $color, $posX, $posY, $anchorX, $anchorY, $rotation);
$this->writeTextAndGetBoundingBox($string, $fontPath, $fontSize, $color, $posX, $posY, $anchorX, $anchorY, $rotation, $letter_spacing);
return $this;
}

Expand All @@ -830,7 +830,7 @@ public function writeText(string $string, string $fontPath, int $fontSize, strin
* @param int $rotation Counterclockwise text rotation in degrees
* @return array Pixels positions of the
*/
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
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
{
if (!$this->isImageDefined()) {
return [];
Expand Down Expand Up @@ -858,7 +858,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
) {
if (
($newImg = \imagecreatetruecolor(1, 1)) === false ||
($posText = \imagettftext($newImg, $fontSize, $rotation, 0, 0, $color, $fontPath, $string)) === false
($posText = $this->imagettftextWithSpacing($newImg, $fontSize, $rotation, 0, 0, $color, $fontPath, $string, $letter_spacing)) === false
) {
return [];
}
Expand Down Expand Up @@ -908,7 +908,7 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
}
}

$posText = \imagettftext($this->image, $fontSize, $rotation, $posX, $posY, $color, $fontPath, $string);
$posText = $this->imagettftextWithSpacing($this->image, $fontSize, $rotation, $posX, $posY, $color, $fontPath, $string, $letter_spacing);

if ($posText === false) {
return [];
Expand Down Expand Up @@ -941,6 +941,40 @@ public function writeTextAndGetBoundingBox(string $string, string $fontPath, int
];
}

/**
* @param $image
* @param $size
* @param $angle
* @param $x
* @param $y
* @param $color
* @param $font
* @param $text
* @param int $spacing
* @return array
*/
private function imagettftextWithSpacing($image, float $size, float $angle, float $x, float $y, int $color, string $font, string $text, int $spacing = 0)
{
if ($spacing == 0)
{
return \imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
}
else
{
$temp_x = $x;
$temp_y = $y;
$posText = [];
for ($i = 0; $i < \mb_strlen($text); ++$i)
{
$posText = \imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $text[$i]);
$bbox = \imagettfbbox($size, 0, $font, $text[$i]);
$temp_x += \cos(\deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
$temp_y -= \sin(\deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
}
return $posText;
}
}

/**
* Draw a rectangle.
*
Expand Down

0 comments on commit 9a6f48c

Please sign in to comment.