Skip to content

Commit 58a2fad

Browse files
committed
WIP: add helper to apply text config
Change-Id: I439faa2f58fb7f11c8731e68593826255fbad60a
1 parent 3b66a28 commit 58a2fad

File tree

6 files changed

+48
-59
lines changed

6 files changed

+48
-59
lines changed

api/applyEffects.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,14 @@
255255
}
256256
}
257257

258-
if ($config['textonpicture']['enabled'] && (!$vars['isCollage'] && !$vars['isChroma'] || $vars['editSingleCollage'])) {
258+
if (!$vars['isCollage'] && !$vars['isChroma'] || $vars['editSingleCollage']) {
259259
// calculate and apply text on picture if image got downscaled before
260260
$scale = 1.0;
261261
if (isset($originalWidth) && isset($originalHeight)) {
262262
$currentWidth = imagesx($imageResource);
263263
$scale = $currentWidth / $originalWidth;
264264
}
265-
266-
// Cast after scaling to avoid implicit float-to-int deprecation warnings in PHP 8.4
267-
$imageHandler->fontSize = (int) round($config['textonpicture']['font_size'] * $scale);
268-
$imageHandler->textLineSpacing = (int) round($config['textonpicture']['linespace'] * $scale);
269-
$imageHandler->fontLocationX = (int) round($config['textonpicture']['locationx'] * $scale);
270-
$imageHandler->fontLocationY = (int) round($config['textonpicture']['locationy'] * $scale);
271-
$imageHandler->fontRotation = $config['textonpicture']['rotation'];
272-
$imageHandler->fontColor = $config['textonpicture']['font_color'];
273-
$imageHandler->fontPath = $config['textonpicture']['font'];
274-
$imageHandler->textLine1 = $config['textonpicture']['line1'];
275-
$imageHandler->textLine2 = $config['textonpicture']['line2'];
276-
$imageHandler->textLine3 = $config['textonpicture']['line3'];
265+
$imageHandler->setTextConfig($config, 'picture', $scale);
277266
$imageResource = $imageHandler->applyText($imageResource);
278267
if (!$imageResource instanceof \GdImage) {
279268
throw new \Exception('Error applying text to image resource.');

api/chromakeying/save.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,10 @@
154154
}
155155

156156
if ($applyEffects) {
157-
if ($config['textonpicture']['enabled']) {
158-
$imageHandler->fontSize = $config['textonpicture']['font_size'];
159-
$imageHandler->fontRotation = $config['textonpicture']['rotation'];
160-
$imageHandler->fontLocationX = $config['textonpicture']['locationx'];
161-
$imageHandler->fontLocationY = $config['textonpicture']['locationy'];
162-
$imageHandler->fontColor = $config['textonpicture']['font_color'];
163-
$imageHandler->fontPath = $config['textonpicture']['font'];
164-
$imageHandler->textLine1 = $config['textonpicture']['line1'];
165-
$imageHandler->textLine2 = $config['textonpicture']['line2'];
166-
$imageHandler->textLine3 = $config['textonpicture']['line3'];
167-
$imageHandler->textLineSpacing = $config['textonpicture']['linespace'];
168-
$imageResource = $imageHandler->applyText($imageResource);
169-
if (!$imageResource instanceof \GdImage) {
170-
throw new \Exception('Error applying text to image resource.');
171-
}
157+
$imageHandler->setTextConfig($config, 'picture');
158+
$imageResource = $imageHandler->applyText($imageResource);
159+
if (!$imageResource instanceof \GdImage) {
160+
throw new \Exception('Error applying text to image resource.');
172161
}
173162
}
174163

api/print.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,10 @@
180180
unset($qrCode);
181181
}
182182

183-
if ($config['textonprint']['enabled']) {
184-
$imageHandler->fontSize = $config['textonprint']['font_size'];
185-
$imageHandler->fontRotation = $config['textonprint']['rotation'];
186-
$imageHandler->fontLocationX = $config['textonprint']['locationx'];
187-
$imageHandler->fontLocationY = $config['textonprint']['locationy'];
188-
$imageHandler->fontColor = $config['textonprint']['font_color'];
189-
$imageHandler->fontPath = $config['textonprint']['font'];
190-
$imageHandler->textLine1 = $config['textonprint']['line1'];
191-
$imageHandler->textLine2 = $config['textonprint']['line2'];
192-
$imageHandler->textLine3 = $config['textonprint']['line3'];
193-
$imageHandler->textLineSpacing = $config['textonprint']['linespace'];
194-
195-
$source = $imageHandler->applyText($source);
196-
if (!$source instanceof \GdImage) {
197-
throw new \Exception('Failed to apply text to image resource.');
198-
}
183+
$imageHandler->setTextConfig($config, 'print');
184+
$source = $imageHandler->applyText($source);
185+
if (!$source instanceof \GdImage) {
186+
throw new \Exception('Failed to apply text to image resource.');
199187
}
200188

201189
if ($config['print']['crop']) {

src/Collage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ public static function createCollage(array $config, array $srcImagePaths, string
638638
}
639639

640640
if ($c->textOnCollageEnabled === 'enabled') {
641+
$imageHandler->textEnabled = true;
641642
$imageHandler->fontSize = $c->textOnCollageFontSize;
642643
$imageHandler->fontRotation = $c->textOnCollageRotation;
643644
$imageHandler->fontLocationX = $c->textOnCollageLocationX;

src/Image.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Image
4747
*
4848
*/
4949

50+
/**
51+
* Enable text on image
52+
*/
53+
public bool $textEnabled = false;
54+
5055
/**
5156
* Font size for the text
5257
*/
@@ -812,12 +817,40 @@ public function applyFrame(GdImage $sourceResource): GdImage
812817
}
813818
}
814819

820+
/**
821+
* Helper function to define vars for applyText
822+
*/
823+
public function setTextConfig(array $config, string $style = 'picture', float $scale = 1.0): void
824+
{
825+
$key = 'texton' . $style;
826+
if (!isset($config[$key])) {
827+
$key = 'textonpicture';
828+
}
829+
$textConfig = $config[$key];
830+
831+
// apply config if defined, fallback to defaults
832+
$this->textEnabled = $textConfig['enabled'] ?? false;
833+
$this->fontPath = $textConfig['font'] ?? '';
834+
$this->fontSize = (int) round(($textConfig['font_size'] ?? 80) * $scale);
835+
$this->fontRotation = $textConfig['rotation'] ?? 0;
836+
$this->fontLocationX = (int) round(($textConfig['locationx'] ?? 80) * $scale);
837+
$this->fontLocationY = (int) round(($textConfig['locationy'] ?? 80) * $scale);
838+
$this->fontColor = $textConfig['font_color'] ?? '#ffffff';
839+
$this->textLine1 = $textConfig['line1'] ?? '';
840+
$this->textLine2 = $textConfig['line2'] ?? '';
841+
$this->textLine3 = $textConfig['line3'] ?? '';
842+
$this->textLineSpacing = (int) round(($textConfig['linespace'] ?? 90) * $scale);
843+
}
844+
815845
/**
816846
* Apply text to the source image resource
817847
*/
818848
public function applyText(GdImage $sourceResource): GdImage
819849
{
820850
try {
851+
if (!$this->textEnabled) {
852+
return $sourceResource;
853+
}
821854
$fontPath = PathUtility::getAbsolutePath($this->fontPath);
822855
$tempFontPath = $_SERVER['DOCUMENT_ROOT'] . '/tempfont.ttf';
823856
$isTempFont = false;

test/photo.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,10 @@
109109
[$imageHandler, $vars, $config, $imageResource] = $processor->postImageProcessing($imageHandler, $vars, $config, $imageResource);
110110
}
111111

112-
if ($config['textonpicture']['enabled']) {
113-
$imageHandler->fontSize = $config['textonpicture']['font_size'];
114-
$imageHandler->fontRotation = $config['textonpicture']['rotation'];
115-
$imageHandler->fontLocationX = $config['textonpicture']['locationx'];
116-
$imageHandler->fontLocationY = $config['textonpicture']['locationy'];
117-
$imageHandler->fontColor = $config['textonpicture']['font_color'];
118-
$imageHandler->fontPath = $config['textonpicture']['font'];
119-
$imageHandler->textLine1 = $config['textonpicture']['line1'];
120-
$imageHandler->textLine2 = $config['textonpicture']['line2'];
121-
$imageHandler->textLine3 = $config['textonpicture']['line3'];
122-
$imageHandler->textLineSpacing = $config['textonpicture']['linespace'];
123-
$imageResource = $imageHandler->applyText($imageResource);
124-
if (!$imageResource) {
125-
throw new \Exception('Error applying text to image resource.');
126-
}
112+
$imageHandler->setTextConfig($config, 'picture');
113+
$imageResource = $imageHandler->applyText($imageResource);
114+
if (!$imageResource) {
115+
throw new \Exception('Error applying text to image resource.');
127116
}
128117

129118
if (!$imageHandler->saveJpeg($imageResource, $vars['tmpFile'])) {

0 commit comments

Comments
 (0)