Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #25 #31

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Interfaces/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\ImageInterface;
use SimonHamp\TheOg\Layout\Position;

interface Box
{
public function anchor(): Point;
public function anchor(?Position $position): Point;

public function name(string $name): static;

Expand Down
9 changes: 7 additions & 2 deletions src/Layout/TextBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\TextModifier;
use Intervention\Image\Typography\FontFactory;
use Intervention\Image\Typography\TextBlock;
Expand Down Expand Up @@ -78,7 +79,7 @@ protected function getPrerenderedBox(): Rectangle
return $this->getFinalTextBox($modifier);
}

protected function generateModifier(string $text, Point $position = new Point()): CustomTextModifier
protected function generateModifier(string $text, Point $position = new Point()): SpecializedInterface
{
return CustomTextModifier::buildSpecialized(
new TextModifier(
Expand Down Expand Up @@ -131,7 +132,9 @@ protected function getFinalTextBox(CustomTextModifier &$modifier): Rectangle
$text = $this->text;
$renderedBox = $this->getRenderedBoxForText($text, $modifier);

while (! $this->doesTextFitInBox($renderedBox)) {
$attempts = 0;

while (! $this->doesTextFitInBox($renderedBox) && $attempts < 10) {
if ($renderedBox->width() > $this->box->width()) {
$text = wordwrap($text, intval(floor($this->box->width() / ($modifier->boxSize('M')->width() / 1.8))));
$renderedBox = $this->getRenderedBoxForText($text, $modifier);
Expand All @@ -150,6 +153,8 @@ protected function getFinalTextBox(CustomTextModifier &$modifier): Rectangle
}

$modifier = $this->generateModifier($text, $modifier->position);

$attempts++;
}

return $renderedBox;
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,12 @@ public static function snapshotImages(): iterable
->title('Simone Hampstead'),
'avatar-too-small',
];

yield 'tricky title truncation' => [
(new Image())
->layout(new GitHubBasic)
->title('Adding Unique Field to MySQL Table With Existing Records'),
'tricky-title-truncation',
];
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.