From e58a7bcd94b60ad4b01bb249f8ab7249bb9d6e62 Mon Sep 17 00:00:00 2001 From: Alex Thomassen Date: Wed, 19 Jun 2024 19:52:42 +0000 Subject: [PATCH] Center that shit --- generate.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- index.html | 4 ++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/generate.php b/generate.php index 66426d2..1e454e2 100644 --- a/generate.php +++ b/generate.php @@ -15,6 +15,7 @@ $validFiles = [ 'positions' => [ 'top' => ['x' => 125, 'y' => 270, 'maxWidth' => 262], 'bottom' => ['x' => 150, 'y' => 350, 'maxWidth' => 216], + 'bottomSecond' => ['x' => 150, 'y' => 400, 'maxWidth' => 216], ], ], ]; @@ -22,6 +23,7 @@ $validFiles = [ $template = $_GET['template'] ?? ''; $topText = $_GET['top'] ?? ''; $bottomText = $_GET['bottom'] ?? ''; +$bottomSecondText = $_GET['bottomSecond'] ?? ''; if (!isset($validFiles[$template]) || !file_exists(sprintf('%s/%s', $templatesDirectory, $template))) { die('Invalid template'); @@ -31,23 +33,65 @@ if (empty($topText) || empty($bottomText)) { die('Invalid text'); } +/** + * Get centered X coordinate + * + * @param int $originalX + * @param int $maxWidth + * @param Imagine\Image\BoxInterface $textBox + * + * @return int + */ +function getXCoord($originalX, $maxWidth, $textBox) +{ + $result = $originalX + ($maxWidth - $textBox->getWidth()) / 2; + + if ($result < 1) { + return 1; + } + + return (int) $result; +} + $templateFile = sprintf('%s/%s', $templatesDirectory, $template); $positions = $validFiles[$template]['positions']; $imagine = new Imagine(); $palette = new RGB(); $colorBlack = $palette->color('#000000', 100); +$transparent = $palette->color('#FFFFFF', 0); $image = $imagine->open($templateFile); $font = new Font($_ENV['FONT_LOCATION'], 12, $colorBlack); +$transparentFont = new Font($_ENV['FONT_LOCATION'], 0, $transparent); $top = $positions['top']; $bottom = $positions['bottom']; -$topPosition = new Point($top['x'], $top['y']); -$bottomPosition = new Point($bottom['x'], $bottom['y']); +$bottomSecond = $positions['bottomSecond']; + +$topBox = $font->box($topText); +$topX = getXCoord($top['x'], $top['maxWidth'], $topBox); +$topY = $top['y']; + +$bottomBox = $font->box($bottomText); +$bottomX = getXCoord($bottom['x'], $bottom['maxWidth'], $bottomBox); +$bottomY = $bottom['y']; + +$bottomSecondBox = $transparentFont->box('placeholder'); +if (!empty($bottomSecondText)) { + $bottomSecondBox = $font->box($bottomSecondText); +} + +$bottomSecondX = getXCoord($bottomSecond['x'], $bottomSecond['maxWidth'], $bottomSecondBox); +$bottomSecondY = $bottomSecond['y']; + +$topPosition = new Point($topX, $topY); +$bottomPosition = new Point($bottomX, $bottomY); +$bottomSecondPosition = new Point($bottomSecondX, $bottomSecondY); $image->draw()->text($topText, $font, $topPosition, 0, $top['maxWidth'] ?? null); $image->draw()->text($bottomText, $font, $bottomPosition, 0, $bottom['maxWidth'] ?? null); +$image->draw()->text($bottomSecondText, $font, $bottomSecondPosition, 0, $bottomSecond['maxWidth'] ?? null); $image->show('png'); diff --git a/index.html b/index.html index f079451..303d297 100644 --- a/index.html +++ b/index.html @@ -25,6 +25,9 @@ + + + @@ -49,6 +52,7 @@ url.searchParams.set('template', form.elements.template.value); url.searchParams.set('top', form.elements.top.value); url.searchParams.set('bottom', form.elements.bottom.value); + url.searchParams.set('bottomSecond', form.elements.bottomSecond.value); preview.src = url.href; preview.classList.remove('hidden');