From 3a287e00c7c82b5c508d3aa76a3965385d338bde Mon Sep 17 00:00:00 2001 From: Andreas Skomski Date: Tue, 13 Jan 2026 08:55:00 +0100 Subject: [PATCH] WIP: add helper to apply text config Change-Id: I439faa2f58fb7f11c8731e68593826255fbad60a --- api/applyEffects.php | 15 ++------------- api/chromakeying/save.php | 19 ++++--------------- api/print.php | 20 ++++---------------- src/Collage.php | 1 + src/Image.php | 33 +++++++++++++++++++++++++++++++++ test/photo.php | 19 ++++--------------- 6 files changed, 48 insertions(+), 59 deletions(-) diff --git a/api/applyEffects.php b/api/applyEffects.php index 25530c386..92ee76429 100644 --- a/api/applyEffects.php +++ b/api/applyEffects.php @@ -255,25 +255,14 @@ } } - if ($config['textonpicture']['enabled'] && (!$vars['isCollage'] && !$vars['isChroma'] || $vars['editSingleCollage'])) { + if (!$vars['isCollage'] && !$vars['isChroma'] || $vars['editSingleCollage']) { // calculate and apply text on picture if image got downscaled before $scale = 1.0; if (isset($originalWidth) && isset($originalHeight)) { $currentWidth = imagesx($imageResource); $scale = $currentWidth / $originalWidth; } - - // Cast after scaling to avoid implicit float-to-int deprecation warnings in PHP 8.4 - $imageHandler->fontSize = (int) round($config['textonpicture']['font_size'] * $scale); - $imageHandler->textLineSpacing = (int) round($config['textonpicture']['linespace'] * $scale); - $imageHandler->fontLocationX = (int) round($config['textonpicture']['locationx'] * $scale); - $imageHandler->fontLocationY = (int) round($config['textonpicture']['locationy'] * $scale); - $imageHandler->fontRotation = $config['textonpicture']['rotation']; - $imageHandler->fontColor = $config['textonpicture']['font_color']; - $imageHandler->fontPath = $config['textonpicture']['font']; - $imageHandler->textLine1 = $config['textonpicture']['line1']; - $imageHandler->textLine2 = $config['textonpicture']['line2']; - $imageHandler->textLine3 = $config['textonpicture']['line3']; + $imageHandler->setTextConfig($config, 'picture', $scale); $imageResource = $imageHandler->applyText($imageResource); if (!$imageResource instanceof \GdImage) { throw new \Exception('Error applying text to image resource.'); diff --git a/api/chromakeying/save.php b/api/chromakeying/save.php index f1d8494cc..c490b602a 100644 --- a/api/chromakeying/save.php +++ b/api/chromakeying/save.php @@ -154,21 +154,10 @@ } if ($applyEffects) { - if ($config['textonpicture']['enabled']) { - $imageHandler->fontSize = $config['textonpicture']['font_size']; - $imageHandler->fontRotation = $config['textonpicture']['rotation']; - $imageHandler->fontLocationX = $config['textonpicture']['locationx']; - $imageHandler->fontLocationY = $config['textonpicture']['locationy']; - $imageHandler->fontColor = $config['textonpicture']['font_color']; - $imageHandler->fontPath = $config['textonpicture']['font']; - $imageHandler->textLine1 = $config['textonpicture']['line1']; - $imageHandler->textLine2 = $config['textonpicture']['line2']; - $imageHandler->textLine3 = $config['textonpicture']['line3']; - $imageHandler->textLineSpacing = $config['textonpicture']['linespace']; - $imageResource = $imageHandler->applyText($imageResource); - if (!$imageResource instanceof \GdImage) { - throw new \Exception('Error applying text to image resource.'); - } + $imageHandler->setTextConfig($config, 'picture'); + $imageResource = $imageHandler->applyText($imageResource); + if (!$imageResource instanceof \GdImage) { + throw new \Exception('Error applying text to image resource.'); } } diff --git a/api/print.php b/api/print.php index ac87e778d..62b257ca8 100644 --- a/api/print.php +++ b/api/print.php @@ -180,22 +180,10 @@ unset($qrCode); } - if ($config['textonprint']['enabled']) { - $imageHandler->fontSize = $config['textonprint']['font_size']; - $imageHandler->fontRotation = $config['textonprint']['rotation']; - $imageHandler->fontLocationX = $config['textonprint']['locationx']; - $imageHandler->fontLocationY = $config['textonprint']['locationy']; - $imageHandler->fontColor = $config['textonprint']['font_color']; - $imageHandler->fontPath = $config['textonprint']['font']; - $imageHandler->textLine1 = $config['textonprint']['line1']; - $imageHandler->textLine2 = $config['textonprint']['line2']; - $imageHandler->textLine3 = $config['textonprint']['line3']; - $imageHandler->textLineSpacing = $config['textonprint']['linespace']; - - $source = $imageHandler->applyText($source); - if (!$source instanceof \GdImage) { - throw new \Exception('Failed to apply text to image resource.'); - } + $imageHandler->setTextConfig($config, 'print'); + $source = $imageHandler->applyText($source); + if (!$source instanceof \GdImage) { + throw new \Exception('Failed to apply text to image resource.'); } if ($config['print']['crop']) { diff --git a/src/Collage.php b/src/Collage.php index 75d0b852b..a4d8ddb0f 100644 --- a/src/Collage.php +++ b/src/Collage.php @@ -638,6 +638,7 @@ public static function createCollage(array $config, array $srcImagePaths, string } if ($c->textOnCollageEnabled === 'enabled') { + $imageHandler->textEnabled = true; $imageHandler->fontSize = $c->textOnCollageFontSize; $imageHandler->fontRotation = $c->textOnCollageRotation; $imageHandler->fontLocationX = $c->textOnCollageLocationX; diff --git a/src/Image.php b/src/Image.php index 2cbcee4df..d0aba3355 100644 --- a/src/Image.php +++ b/src/Image.php @@ -47,6 +47,11 @@ class Image * */ + /** + * Enable text on image + */ + public bool $textEnabled = false; + /** * Font size for the text */ @@ -812,12 +817,40 @@ public function applyFrame(GdImage $sourceResource): GdImage } } + /** + * Helper function to define vars for applyText + */ + public function setTextConfig(array $config, string $style = 'picture', float $scale = 1.0): void + { + $key = 'texton' . $style; + if (!isset($config[$key])) { + $key = 'textonpicture'; + } + $textConfig = $config[$key]; + + // apply config if defined, fallback to defaults + $this->textEnabled = $textConfig['enabled'] ?? false; + $this->fontPath = $textConfig['font'] ?? ''; + $this->fontSize = (int) round(($textConfig['font_size'] ?? 80) * $scale); + $this->fontRotation = $textConfig['rotation'] ?? 0; + $this->fontLocationX = (int) round(($textConfig['locationx'] ?? 80) * $scale); + $this->fontLocationY = (int) round(($textConfig['locationy'] ?? 80) * $scale); + $this->fontColor = $textConfig['font_color'] ?? '#ffffff'; + $this->textLine1 = $textConfig['line1'] ?? ''; + $this->textLine2 = $textConfig['line2'] ?? ''; + $this->textLine3 = $textConfig['line3'] ?? ''; + $this->textLineSpacing = (int) round(($textConfig['linespace'] ?? 90) * $scale); + } + /** * Apply text to the source image resource */ public function applyText(GdImage $sourceResource): GdImage { try { + if (!$this->textEnabled) { + return $sourceResource; + } $fontPath = PathUtility::getAbsolutePath($this->fontPath); $tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf'; $isTempFont = false; diff --git a/test/photo.php b/test/photo.php index 16b36c5b6..689b59771 100644 --- a/test/photo.php +++ b/test/photo.php @@ -109,21 +109,10 @@ [$imageHandler, $vars, $config, $imageResource] = $processor->postImageProcessing($imageHandler, $vars, $config, $imageResource); } - if ($config['textonpicture']['enabled']) { - $imageHandler->fontSize = $config['textonpicture']['font_size']; - $imageHandler->fontRotation = $config['textonpicture']['rotation']; - $imageHandler->fontLocationX = $config['textonpicture']['locationx']; - $imageHandler->fontLocationY = $config['textonpicture']['locationy']; - $imageHandler->fontColor = $config['textonpicture']['font_color']; - $imageHandler->fontPath = $config['textonpicture']['font']; - $imageHandler->textLine1 = $config['textonpicture']['line1']; - $imageHandler->textLine2 = $config['textonpicture']['line2']; - $imageHandler->textLine3 = $config['textonpicture']['line3']; - $imageHandler->textLineSpacing = $config['textonpicture']['linespace']; - $imageResource = $imageHandler->applyText($imageResource); - if (!$imageResource) { - throw new \Exception('Error applying text to image resource.'); - } + $imageHandler->setTextConfig($config, 'picture'); + $imageResource = $imageHandler->applyText($imageResource); + if (!$imageResource) { + throw new \Exception('Error applying text to image resource.'); } if (!$imageHandler->saveJpeg($imageResource, $vars['tmpFile'])) {