Skip to content
Draft
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
15 changes: 2 additions & 13 deletions api/applyEffects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
19 changes: 4 additions & 15 deletions api/chromakeying/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}

Expand Down
20 changes: 4 additions & 16 deletions api/print.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
1 change: 1 addition & 0 deletions src/Collage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Image
*
*/

/**
* Enable text on image
*/
public bool $textEnabled = false;

/**
* Font size for the text
*/
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 4 additions & 15 deletions test/photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
Loading