DiplomGenerator/generate.php

98 lines
2.7 KiB
PHP
Raw Normal View History

2023-10-16 17:07:43 +02:00
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Imagine\Gd\Font;
use Imagine\Gd\Imagine;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;
2023-10-16 17:26:50 +02:00
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
2023-10-16 17:07:43 +02:00
$templatesDirectory = __DIR__ . '/templates';
$validFiles = [
'diplom-1.png' => [
'positions' => [
'top' => ['x' => 125, 'y' => 270, 'maxWidth' => 262],
'bottom' => ['x' => 150, 'y' => 350, 'maxWidth' => 216],
2024-06-19 21:52:42 +02:00
'bottomSecond' => ['x' => 150, 'y' => 400, 'maxWidth' => 216],
2023-10-16 17:07:43 +02:00
],
],
];
$template = $_GET['template'] ?? '';
$topText = $_GET['top'] ?? '';
$bottomText = $_GET['bottom'] ?? '';
2024-06-19 21:52:42 +02:00
$bottomSecondText = $_GET['bottomSecond'] ?? '';
2023-10-16 17:07:43 +02:00
if (!isset($validFiles[$template]) || !file_exists(sprintf('%s/%s', $templatesDirectory, $template))) {
die('Invalid template');
}
if (empty($topText) || empty($bottomText)) {
die('Invalid text');
}
2024-06-19 21:52:42 +02:00
/**
* 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;
}
2023-10-16 17:07:43 +02:00
$templateFile = sprintf('%s/%s', $templatesDirectory, $template);
$positions = $validFiles[$template]['positions'];
$imagine = new Imagine();
$palette = new RGB();
$colorBlack = $palette->color('#000000', 100);
2024-06-19 21:52:42 +02:00
$transparent = $palette->color('#FFFFFF', 0);
2023-10-16 17:07:43 +02:00
$image = $imagine->open($templateFile);
$font = new Font($_ENV['FONT_LOCATION'], 12, $colorBlack);
2024-06-19 21:52:42 +02:00
$transparentFont = new Font($_ENV['FONT_LOCATION'], 0, $transparent);
2023-10-16 17:07:43 +02:00
$top = $positions['top'];
$bottom = $positions['bottom'];
2024-06-19 21:52:42 +02:00
$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);
2023-10-16 17:07:43 +02:00
$image->draw()->text($topText, $font, $topPosition, 0, $top['maxWidth'] ?? null);
$image->draw()->text($bottomText, $font, $bottomPosition, 0, $bottom['maxWidth'] ?? null);
2024-06-19 21:52:42 +02:00
$image->draw()->text($bottomSecondText, $font, $bottomSecondPosition, 0, $bottomSecond['maxWidth'] ?? null);
2023-10-16 17:07:43 +02:00
$image->show('png');