Skip to content

Commit 5f6d27d

Browse files
committed
Fix issue with GD and relative font paths
This patch builds full path to a font file with GD driver to make sure to pass absolute path to imagettftext(). This is because of issues with different GD versions behaving differently when passing relative paths to imagettftext()
1 parent ea1e6d4 commit 5f6d27d

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/Drivers/Gd/Modifiers/TextModifier.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Intervention\Image\Drivers\Gd\Modifiers;
66

77
use Intervention\Image\Exceptions\ColorException;
8+
use Intervention\Image\Exceptions\FontException;
89
use Intervention\Image\Exceptions\RuntimeException;
910
use Intervention\Image\Interfaces\ImageInterface;
1011
use Intervention\Image\Interfaces\SpecializedInterface;
@@ -26,32 +27,40 @@ public function apply(ImageInterface $image): ImageInterface
2627
$textColor = $this->gdTextColor($image);
2728
$strokeColor = $this->gdStrokeColor($image);
2829

30+
// build full path to font file to make sure to pass absolute path to imageftbbox()
31+
// because of issues with different GD version behaving differently when passing
32+
// relative paths to imagettftext()
33+
$fontPath = $this->font->hasFilename() ? realpath($this->font->filename()) : false;
34+
if ($this->font->hasFilename() && $fontPath === false) {
35+
throw new FontException('Font file ' . $this->font->filename() . ' does not exist.');
36+
}
37+
2938
foreach ($image as $frame) {
3039
imagealphablending($frame->native(), true);
3140
if ($this->font->hasFilename()) {
3241
foreach ($lines as $line) {
3342
foreach ($this->strokeOffsets($this->font) as $offset) {
3443
imagettftext(
35-
$frame->native(),
36-
$fontProcessor->nativeFontSize($this->font),
37-
$this->font->angle() * -1,
38-
$line->position()->x() + $offset->x(),
39-
$line->position()->y() + $offset->y(),
40-
$strokeColor,
41-
$this->font->filename(),
42-
(string) $line
44+
image: $frame->native(),
45+
size: $fontProcessor->nativeFontSize($this->font),
46+
angle: $this->font->angle() * -1,
47+
x: $line->position()->x() + $offset->x(),
48+
y: $line->position()->y() + $offset->y(),
49+
color: $strokeColor,
50+
font_filename: $fontPath,
51+
text: (string) $line
4352
);
4453
}
4554

4655
imagettftext(
47-
$frame->native(),
48-
$fontProcessor->nativeFontSize($this->font),
49-
$this->font->angle() * -1,
50-
$line->position()->x(),
51-
$line->position()->y(),
52-
$textColor,
53-
$this->font->filename(),
54-
(string) $line
56+
image: $frame->native(),
57+
size: $fontProcessor->nativeFontSize($this->font),
58+
angle: $this->font->angle() * -1,
59+
x: $line->position()->x(),
60+
y: $line->position()->y(),
61+
color: $textColor,
62+
font_filename: $fontPath,
63+
text: (string) $line
5564
);
5665
}
5766
} else {

0 commit comments

Comments
 (0)