Skip to content
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
18 changes: 9 additions & 9 deletions api/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,32 +259,32 @@

if ($newConfig['picture']['take_frame'] && $newConfig['picture']['frame'] === '') {
$newConfig['picture']['take_frame'] = false;
$logger->debug('set picture.frame empty', [$newConfig['picture']['frame']]);
$logger->debug('Picture frame empty. Disabled picture frame.');
}

if ($newConfig['collage']['take_frame'] && $newConfig['collage']['frame'] === '') {
$newConfig['collage']['take_frame'] = false;
$logger->debug('collage.frame empty', [$newConfig['collage']['frame']]);
$logger->debug('Collage frame empty. Disabled collage frame.');
}

if ($newConfig['print']['print_frame'] && $newConfig['print']['frame'] === '') {
$newConfig['print']['print_frame'] = false;
$logger->debug('print.frame empty', [$newConfig['print']['frame']]);
$logger->debug('Print frame empty. Disabled frame on print.');
}

if ($newConfig['textonpicture']['enabled'] && ($newConfig['textonpicture']['font'] === '' || !file_exists(PathUtility::getAbsolutePath($newConfig['textonpicture']['font'])))) {
if ($newConfig['textonpicture']['enabled'] && $newConfig['textonpicture']['font'] === '') {
$newConfig['textonpicture']['enabled'] = false;
$logger->debug('Picture font does not exist or is empty. Disabled text on picture. Note: Must be an absoloute path.', [$newConfig['textonpicture']['font']]);
$logger->debug('Picture font is empty. Disabled text on picture.');
}

if ($newConfig['textoncollage']['enabled'] && ($newConfig['textoncollage']['font'] === '' || !file_exists(PathUtility::getAbsolutePath($newConfig['textoncollage']['font'])))) {
if ($newConfig['textoncollage']['enabled'] && $newConfig['textoncollage']['font'] === '') {
$newConfig['textoncollage']['enabled'] = false;
$logger->debug('Collage font does not exist or is empty. Disabled text on picture. Note: Must be an absoloute path.', [$newConfig['textoncollage']['font']]);
$logger->debug('Collage font is empty. Disabled text on picture.');
}

if ($newConfig['textonprint']['enabled'] && ($newConfig['textonprint']['font'] === '' || !file_exists(PathUtility::getAbsolutePath($newConfig['textonprint']['font'])))) {
if ($newConfig['textonprint']['enabled'] && $newConfig['textonprint']['font'] === '') {
$newConfig['textonprint']['enabled'] = false;
$logger->debug('Print font does not exist or is empty. Disabled text on print. Note: Must be an absoloute path.', [$newConfig['textonprint']['font']]);
$logger->debug('Print font is empty. Disabled text on print.');
}

if ($newConfig['logo']['enabled']) {
Expand Down
39 changes: 25 additions & 14 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,10 @@ public function createFromImage(string $image): GdImage|false
parse_str(parse_url($image)['query'] ?? '', $query);
$path = is_array($query['dir']) ? $query['dir']['0'] : $query['dir'];
$image = ImageUtility::getRandomImageFromPath($path);
} elseif (PathUtility::isAbsolutePath(PathUtility::getAbsolutePath($image))) {
$image = PathUtility::getAbsolutePath($image);
}

if (!file_exists($image)) {
$image = PathUtility::resolveFilePath($image);
}

$resource = imagecreatefromstring((string)file_get_contents($image));
Expand Down Expand Up @@ -723,11 +725,13 @@ public function applyFrame(GdImage $sourceResource): GdImage
public function applyText(GdImage $sourceResource): GdImage
{
try {
$fontPath = PathUtility::getAbsolutePath($this->fontPath);
$tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf';
$isTempFont = false;
$fontSize = $this->fontSize;
$fontRotation = $this->fontRotation;
$fontLocationX = $this->fontLocationX;
$fontLocationY = $this->fontLocationY;
$fontPath = PathUtility::getAbsolutePath($this->fontPath);
$textLineSpacing = $this->textLineSpacing;
// Convert hex color string to RGB values
$colorComponents = self::getColorComponents($this->fontColor);
Expand All @@ -736,17 +740,22 @@ public function applyText(GdImage $sourceResource): GdImage
// Allocate color and set font
$color = intval(imagecolorallocate($sourceResource, $r, $g, $b));

$localFontPath = $fontPath;
$tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf';
if (PathUtility::isUrl($fontPath)) {
$font = file_get_contents($fontPath);
file_put_contents($tempFontPath, $font);
$localFontPath = $tempFontPath;
if (PathUtility::isUrl($this->fontPath)) {
$font = @file_get_contents($this->fontPath);

if ($font === false) {
throw new \Exception('Failed to download font from: ' . $this->fontPath);
}
file_put_contents($tempFontPath, $this->fontPath);
$fontPath = $tempFontPath;
$isTempFont = true;
} else {
$fontPath = PathUtility::resolveFilePath($this->fontPath);
}

// Add first line of text
if (!empty($this->textLine1)) {
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $fontLocationX, $fontLocationY, $color, $localFontPath, $this->textLine1)) {
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $fontLocationX, $fontLocationY, $color, $fontPath, $this->textLine1)) {
throw new \Exception('Could not add first line of text to resource.');
}
}
Expand All @@ -755,7 +764,7 @@ public function applyText(GdImage $sourceResource): GdImage
if (!empty($this->textLine2)) {
$line2Y = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationY + $textLineSpacing : $fontLocationY;
$line2X = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationX : $fontLocationX + $textLineSpacing;
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line2X, $line2Y, $color, $localFontPath, $this->textLine2)) {
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line2X, $line2Y, $color, $fontPath, $this->textLine2)) {
throw new \Exception('Could not add second line of text to resource.');
}
}
Expand All @@ -764,13 +773,15 @@ public function applyText(GdImage $sourceResource): GdImage
if (!empty($this->textLine3)) {
$line3Y = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationY + $textLineSpacing * 2 : $fontLocationY;
$line3X = $fontRotation < 45 && $fontRotation > -45 ? $fontLocationX : $fontLocationX + $textLineSpacing * 2;
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line3X, $line3Y, $color, $localFontPath, $this->textLine3)) {
if (!imagettftext($sourceResource, $fontSize, $fontRotation, $line3X, $line3Y, $color, $fontPath, $this->textLine3)) {
throw new \Exception('Could not add third line of text to resource.');
}
}

if ($localFontPath !== $fontPath) {
unlink($tempFontPath);
if ($isTempFont && file_exists($tempFontPath)) {
if (!unlink($tempFontPath)) {
$this->addErrorData('Failed to delete tmp font: ' . $tempFontPath);
}
}
$this->imageModified = true;
// Return resource with text applied
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/FontUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function getFontPreviewImage(
],
array $attributes = [],
): string {
$absoluteFontPath = PathUtility::getAbsolutePath($fontPath);
$absoluteFontPath = PathUtility::resolveFilePath($fontPath);
$lineHeight = (int) round($fontSize * 1.5);

$image = imagecreatetruecolor($width, $height);
Expand Down
23 changes: 23 additions & 0 deletions src/Utility/PathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,27 @@ public static function fixFilePath(string $path): string
{
return str_replace(['\\', '//'], '/', $path);
}

public static function resolveFilePath(string $filePath): string
{
if (self::isUrl($filePath)) {
return $filePath;
}

$absolutePath = self::isAbsolutePath($filePath) ? $filePath : self::getAbsolutePath($filePath);
if (file_exists($absolutePath)) {
return $absolutePath;
}

$altPath1 = $_SERVER['DOCUMENT_ROOT'] . $filePath;
$altPath2 = $_SERVER['DOCUMENT_ROOT'] . '/' . $filePath;

if (file_exists($altPath1)) {
return $altPath1;
} elseif (file_exists($altPath2)) {
return $altPath2;
}

throw new \Exception('File not found: ' . $filePath);
}
}
Loading