2026-02-15 08:01:20 +00:00

95 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . '/../includes/I18N/Arabic.php';
use I18N\Arabic;
// --- Parameters ---
$text = $_GET['text'] ?? 'Default Text';
$lang = $_GET['lang'] ?? 'en';
$template = $_GET['template'] ?? 'default';
// --- Configuration ---
$width = 800;
$height = 600;
$font_size = 24;
$font_file = __DIR__ . '/../includes/fpdf/font/amiri.ttf';
$template_image = __DIR__ . '/../assets/images/card_templates/' . basename($template) . '.png';
if (!file_exists($font_file)) {
http_response_code(500);
die('Font file not found');
}
if (!file_exists($template_image)) {
$template_image = __DIR__ . '/../assets/images/card_templates/default.png';
}
// --- Text Processing for Arabic ---
if ($lang === 'ar') {
$arabic = new Arabic();
$text = $arabic->utf8Glyphs($text);
}
// --- Image Generation ---
header('Content-Type: image/png');
$image = imagecreatefrompng($template_image);
imagealphablending($image, true);
imagesavealpha($image, true);
// Colors
$text_color = imagecolorallocate($image, 0, 0, 0); // Black
// --- Text Wrapping and Positioning ---
$lines = [];
if ($lang === 'ar') {
// Simple reverse for RTL line wrapping (basic)
$words = explode(' ', $text);
$line = '';
foreach ($words as $word) {
$new_line = $line . ' ' . $word;
$bbox = imagettfbbox($font_size, 0, $font_file, $new_line);
$line_width = abs($bbox[2] - $bbox[0]);
if ($line_width > ($width - 80)) { // 40px margin
$lines[] = $line;
$line = $word;
} else {
$line = $new_line;
}
}
$lines[] = $line;
} else {
$words = explode(' ', $text);
$line = '';
foreach ($words as $word) {
$new_line = $line . ($line ? ' ' : '') . $word;
$bbox = imagettfbbox($font_size, 0, $font_file, $new_line);
$line_width = abs($bbox[2] - $bbox[0]);
if ($line_width > ($width - 80)) { // 40px margin
$lines[] = $line;
$line = $word;
} else {
$line = $new_line;
}
}
$lines[] = $line;
}
// Calculate total text height
$total_text_height = count($lines) * ($font_size * 1.5);
$y_start = ($height - $total_text_height) / 2;
// Draw text line by line
foreach ($lines as $i => $line) {
$line = trim($line);
$bbox = imagettfbbox($font_size, 0, $font_file, $line);
$line_width = abs($bbox[2] - $bbox[0]);
$x = ($width - $line_width) / 2;
$y = $y_start + ($i * $font_size * 1.5);
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_file, $line);
}
// --- Output ---
imagepng($image);
imagedestroy($image);