diff --git a/image.php b/image.php index aeebeca..c128ce1 100644 --- a/image.php +++ b/image.php @@ -336,16 +336,37 @@ if ($currentLine !== '') { $lines[] = $currentLine; } +// Center text vertically in the bubble +$lineCount = count($lines); +if (isset($config['use_builtin_font'])) { + $totalTextHeight = $lineCount * imagefontheight(5); +} else { + $totalTextHeight = $lineCount * $config['line_height']; +} + +// Calculate vertical starting position for centered text +$startY = $textAreaY + ($textAreaHeight - $totalTextHeight) / 2; +if ($startY < $textAreaY) $startY = $textAreaY; // Ensure text doesn't overflow top + // Draw text -$y = $textAreaY + $config['font_size']; // Start position for text +$y = $startY + $config['font_size']; // Start position for centered text foreach ($lines as $line) { if (isset($config['use_builtin_font'])) { + // Calculate width for centering horizontally + $lineWidth = imagefontwidth(5) * strlen($line); + $x = $textAreaX + ($textAreaWidth - $lineWidth) / 2; + // Use built-in font (less nice but always available) - imagestring($canvas, 5, $textAreaX, $y, $line, $textColor); + imagestring($canvas, 5, $x, $y, $line, $textColor); $y += imagefontheight(5); } else { + // Calculate width for centering horizontally + $bbox = imagettfbbox($config['font_size'], 0, $config['font'], $line); + $lineWidth = $bbox[2] - $bbox[0]; + $x = $textAreaX + ($textAreaWidth - $lineWidth) / 2; + // Use TrueType font (nicer but requires font file) - imagettftext($canvas, $config['font_size'], 0, $textAreaX, $y, $textColor, $config['font'], $line); + imagettftext($canvas, $config['font_size'], 0, $x, $y, $textColor, $config['font'], $line); $y += $config['line_height']; } }