Skip to content

Commit 9457de4

Browse files
committed
src(Image): improve path handling
Change-Id: Iad56787e29fdc7de20a59785c6376421a96e1794
1 parent 63c2b24 commit 9457de4

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/Image.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@ public function createFromImage(string $image): GdImage|false
361361
parse_str(parse_url($image)['query'] ?? '', $query);
362362
$path = is_array($query['dir']) ? $query['dir']['0'] : $query['dir'];
363363
$image = ImageUtility::getRandomImageFromPath($path);
364-
} elseif (PathUtility::isAbsolutePath(PathUtility::getAbsolutePath($image))) {
365-
$image = PathUtility::getAbsolutePath($image);
364+
}
365+
366+
if (!file_exists($image)) {
367+
$image = PathUtility::resolveFilePath($image);
366368
}
367369

368370
$resource = imagecreatefromstring((string)file_get_contents($image));
@@ -723,11 +725,13 @@ public function applyFrame(GdImage $sourceResource): GdImage
723725
public function applyText(GdImage $sourceResource): GdImage
724726
{
725727
try {
728+
$fontPath = PathUtility::getAbsolutePath($this->fontPath);
729+
$tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf';
730+
$isTempFont = false;
726731
$fontSize = $this->fontSize;
727732
$fontRotation = $this->fontRotation;
728733
$fontLocationX = $this->fontLocationX;
729734
$fontLocationY = $this->fontLocationY;
730-
$fontPath = PathUtility::getAbsolutePath($this->fontPath);
731735
$textLineSpacing = $this->textLineSpacing;
732736
// Convert hex color string to RGB values
733737
$colorComponents = self::getColorComponents($this->fontColor);
@@ -736,17 +740,22 @@ public function applyText(GdImage $sourceResource): GdImage
736740
// Allocate color and set font
737741
$color = intval(imagecolorallocate($sourceResource, $r, $g, $b));
738742

739-
$localFontPath = $fontPath;
740-
$tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf';
741-
if (PathUtility::isUrl($fontPath)) {
742-
$font = file_get_contents($fontPath);
743-
file_put_contents($tempFontPath, $font);
744-
$localFontPath = $tempFontPath;
743+
if (PathUtility::isUrl($this->fontPath)) {
744+
$font = @file_get_contents($this->fontPath);
745+
746+
if ($font === false) {
747+
throw new \Exception('Failed to download font from: ' . $this->fontPath);
748+
}
749+
file_put_contents($tempFontPath, $this->fontPath);
750+
$fontPath = $tempFontPath;
751+
$isTempFont = true;
752+
} else {
753+
$fontPath = PathUtility::resolveFilePath($this->fontPath);
745754
}
746755

747756
// Add first line of text
748757
if (!empty($this->textLine1)) {
749-
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $fontLocationX, $fontLocationY, $color, $localFontPath, $this->textLine1)) {
758+
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $fontLocationX, $fontLocationY, $color, $fontPath, $this->textLine1)) {
750759
throw new \Exception('Could not add first line of text to resource.');
751760
}
752761
}
@@ -755,7 +764,7 @@ public function applyText(GdImage $sourceResource): GdImage
755764
if (!empty($this->textLine2)) {
756765
$line2Y = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationY + $textLineSpacing : $fontLocationY;
757766
$line2X = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationX : $fontLocationX + $textLineSpacing;
758-
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line2X, $line2Y, $color, $localFontPath, $this->textLine2)) {
767+
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line2X, $line2Y, $color, $fontPath, $this->textLine2)) {
759768
throw new \Exception('Could not add second line of text to resource.');
760769
}
761770
}
@@ -764,13 +773,15 @@ public function applyText(GdImage $sourceResource): GdImage
764773
if (!empty($this->textLine3)) {
765774
$line3Y = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationY + $textLineSpacing * 2 : $fontLocationY;
766775
$line3X = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationX : $fontLocationX + $textLineSpacing * 2;
767-
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line3X, $line3Y, $color, $localFontPath, $this->textLine3)) {
776+
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line3X, $line3Y, $color, $fontPath, $this->textLine3)) {
768777
throw new \Exception('Could not add third line of text to resource.');
769778
}
770779
}
771780

772-
if ($localFontPath !== $fontPath) {
773-
unlink($tempFontPath);
781+
if ($isTempFont && file_exists($tempFontPath)) {
782+
if (!unlink($tempFontPath)) {
783+
$this->addErrorData('Failed to delete tmp font: ' . $tempFontPath);
784+
}
774785
}
775786
$this->imageModified = true;
776787
// Return resource with text applied

src/Utility/PathUtility.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,30 @@ public static function fixFilePath(string $path): string
8686
{
8787
return str_replace(['\\', '//'], '/', $path);
8888
}
89+
90+
public static function resolveFilePath(string $filePath): string
91+
{
92+
if (self::isUrl($filePath)) {
93+
return $filePath;
94+
}
95+
96+
if (!self::isAbsolutePath($filePath)) {
97+
$filePath = self::getAbsolutePath($filePath);
98+
}
99+
100+
if (file_exists($filePath)) {
101+
return $filePath;
102+
}
103+
104+
$altPath1 = $_SERVER['DOCUMENT_ROOT'] . $filePath;
105+
$altPath2 = $_SERVER['DOCUMENT_ROOT'] . '/' . $filePath;
106+
107+
if (file_exists($altPath1)) {
108+
return $altPath1;
109+
} elseif (file_exists($altPath2)) {
110+
return $altPath2;
111+
}
112+
113+
throw new Exception('File not found: ' . $filePath);
114+
}
89115
}

0 commit comments

Comments
 (0)