在imagettftext函数中右对齐文本

时间:2013-03-14 06:27:36

标签: php

我正在创建带有一些文本的图像,当旋转为0然后很好但是当rotation is > 0时,由于文本对齐,图像看起来很糟糕,因为默认对齐是左边的。因为每个值都是动态的所以我们无法解决,请帮助我。

$message = 'Thanks, Deep, for making time to have a coffee today Purushottam';
$no_of_characters_line = $temp_data['no_of_characters_line'];
$lines = explode('|', wordwrap($message, $no_of_characters_line, '|'));
// Starting Y position and X position
$y = $temp_data['position_from_top'];
$x = $temp_data['position_from_left'];
$font_size = $temp_data['font_size'];
$rotation_angle = $temp_data['rotation'];
$line_height = $temp_data['line_height'];   

foreach ($lines as $line)
    {
    imagettftext($im, $font_size,$rotation_angle, $x, $y, $black, $font, $line);
    // Increment Y so the next line is below the previous line
    $y += $line_height;
    }

我还附加了示例模板。enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用imagettfbbox来计算字符串大小(以像素为单位)。然后,使用宽度,您将能够计算将文本对齐所需的偏移量。另外,请查看imagettftext文档,了解文本对齐的一些示例。