mirror of
https://github.com/Intervention/image.git
synced 2025-08-28 08:09:54 +02:00
Refactor GD TextWriter
This commit is contained in:
@@ -17,8 +17,48 @@ abstract class AbstractTextWriter implements ModifierInterface
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTextBlock(): TextBlock
|
protected function getFont(): FontInterface
|
||||||
{
|
{
|
||||||
return new TextBlock($this->text);
|
return $this->font;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getPosition(): Point
|
||||||
|
{
|
||||||
|
return $this->position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build TextBlock object from text string and align every line
|
||||||
|
* according to text writers font object and position.
|
||||||
|
*
|
||||||
|
* @return TextBlock
|
||||||
|
*/
|
||||||
|
public function getAlignedTextBlock(): TextBlock
|
||||||
|
{
|
||||||
|
$lines = new TextBlock($this->text);
|
||||||
|
$position = $this->getPosition();
|
||||||
|
$font = $this->getFont();
|
||||||
|
|
||||||
|
$boundingBox = $lines->getBoundingBox($font, $position);
|
||||||
|
$pivot = $boundingBox->last();
|
||||||
|
|
||||||
|
$leading = $font->leadingInPixels();
|
||||||
|
$blockWidth = $lines->longestLine()->widthInFont($font);
|
||||||
|
|
||||||
|
$x = $pivot->getX();
|
||||||
|
$y = $font->hasFilename() ? $pivot->getY() + $font->capHeight() : $pivot->getY();
|
||||||
|
$x_adjustment = 0;
|
||||||
|
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
$x_adjustment = $font->getAlign() == 'left' ? 0 : $blockWidth - $line->widthInFont($font);
|
||||||
|
$x_adjustment = $font->getAlign() == 'right' ? intval(round($x_adjustment)) : $x_adjustment;
|
||||||
|
$x_adjustment = $font->getAlign() == 'center' ? intval(round($x_adjustment / 2)) : $x_adjustment;
|
||||||
|
$position = new Point($x + $x_adjustment, $y);
|
||||||
|
$position->rotate($font->getAngle(), $pivot);
|
||||||
|
$line->setPosition($position);
|
||||||
|
$y += $leading;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $lines;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,16 +5,14 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
|
|||||||
use Intervention\Image\Drivers\Abstract\AbstractTextWriter;
|
use Intervention\Image\Drivers\Abstract\AbstractTextWriter;
|
||||||
use Intervention\Image\Drivers\Gd\Font;
|
use Intervention\Image\Drivers\Gd\Font;
|
||||||
use Intervention\Image\Exceptions\FontException;
|
use Intervention\Image\Exceptions\FontException;
|
||||||
|
use Intervention\Image\Interfaces\FontInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
|
||||||
class TextWriter extends AbstractTextWriter
|
class TextWriter extends AbstractTextWriter
|
||||||
{
|
{
|
||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
$lines = $this->getTextBlock();
|
$lines = $this->getAlignedTextBlock();
|
||||||
$boundingBox = $lines->getBoundingBox($this->getFont(), $this->position);
|
|
||||||
$lines->alignByFont($this->getFont(), $boundingBox->last());
|
|
||||||
|
|
||||||
foreach ($image as $frame) {
|
foreach ($image as $frame) {
|
||||||
if ($this->font->hasFilename()) {
|
if ($this->font->hasFilename()) {
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
@@ -29,9 +27,6 @@ class TextWriter extends AbstractTextWriter
|
|||||||
$line
|
$line
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// debug
|
|
||||||
imagepolygon($frame->getCore(), $boundingBox->toArray(), 0);
|
|
||||||
} else {
|
} else {
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
imagestring(
|
imagestring(
|
||||||
@@ -42,7 +37,6 @@ class TextWriter extends AbstractTextWriter
|
|||||||
$line,
|
$line,
|
||||||
$this->font->getColor()->toInt()
|
$this->font->getColor()->toInt()
|
||||||
);
|
);
|
||||||
imagepolygon($frame->getCore(), $boundingBox->toArray(), 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,7 +44,7 @@ class TextWriter extends AbstractTextWriter
|
|||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getFont(): Font
|
protected function getFont(): FontInterface
|
||||||
{
|
{
|
||||||
if (!is_a($this->font, Font::class)) {
|
if (!is_a($this->font, Font::class)) {
|
||||||
throw new FontException('Font is not compatible to current driver.');
|
throw new FontException('Font is not compatible to current driver.');
|
||||||
|
@@ -17,37 +17,6 @@ class TextBlock extends Collection
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set position of each line in text block
|
|
||||||
* according to given font settings.
|
|
||||||
*
|
|
||||||
* @param FontInterface $font
|
|
||||||
* @param Point $pivot
|
|
||||||
* @return TextBlock
|
|
||||||
*/
|
|
||||||
public function alignByFont(FontInterface $font, Point $pivot = null): self
|
|
||||||
{
|
|
||||||
$pivot = $pivot ? $pivot : new Point();
|
|
||||||
|
|
||||||
$leading = $font->leadingInPixels();
|
|
||||||
$x = $pivot->getX();
|
|
||||||
$y = $font->hasFilename() ? $pivot->getY() + $font->capHeight() : $pivot->getY();
|
|
||||||
|
|
||||||
$x_adjustment = 0;
|
|
||||||
$total_width = $this->longestLine()->widthInFont($font);
|
|
||||||
foreach ($this as $line) {
|
|
||||||
$x_adjustment = $font->getAlign() == 'left' ? 0 : $total_width - $line->widthInFont($font);
|
|
||||||
$x_adjustment = $font->getAlign() == 'right' ? intval(round($x_adjustment)) : $x_adjustment;
|
|
||||||
$x_adjustment = $font->getAlign() == 'center' ? intval(round($x_adjustment / 2)) : $x_adjustment;
|
|
||||||
$position = new Point($x + $x_adjustment, $y);
|
|
||||||
$position->rotate($font->getAngle(), $pivot);
|
|
||||||
$line->setPosition($position);
|
|
||||||
$y += $leading;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getBoundingBox(FontInterface $font, Point $pivot = null): Polygon
|
public function getBoundingBox(FontInterface $font, Point $pivot = null): Polygon
|
||||||
{
|
{
|
||||||
$pivot = $pivot ? $pivot : new Point();
|
$pivot = $pivot ? $pivot : new Point();
|
||||||
|
Reference in New Issue
Block a user