有没有人有一个函数可以绘制一个指定字母间距的ttf字符串(imagettftext)?
我找不到任何内置的GD函数,所以我认为应该逐个字母地添加一些恒定宽度。
也许有人已经有这样的功能了:)
ps。最好的字体将是arial.ttf
发布于 2011-08-04 14:37:58
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
{
if ($spacing == 0)
{
imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
}
else
{
$temp_x = $x;
for ($i = 0; $i < strlen($text); $i++)
{
$bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]);
$temp_x += $spacing + ($bbox[2] - $bbox[0]);
}
}
}还有这个电话:
imagettftextSp($image, 30, 0, 30, 30, $black, 'arial.ttf', $text, 23);函数参数顺序满足标准的imagettftext参数顺序,最后一个参数是可选的$spacing参数。如果未设置或传递的值为0,则不设置紧排/字母间距。
发布于 2012-08-14 21:36:10
我知道这个问题不久前就得到了回答,但我需要一个具有字母间距并保持角度偏移量的解决方案。
我修改了radzi的代码来实现这一点:
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
{
if ($spacing == 0)
{
imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
}
else
{
$temp_x = $x;
$temp_y = $y;
for ($i = 0; $i < strlen($text); $i++)
{
imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $text[$i]);
$bbox = imagettfbbox($size, 0, $font, $text[$i]);
$temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
$temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
}
}
}发布于 2013-07-16 20:32:34
只是为了完成pidalia的答案(这是最好的),以避免一些特殊字符的麻烦(如"é“或"à")
static function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) {
if ($spacing == 0) {
imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
} else {
$temp_x = $x;
$temp_y = $y;
//to avoid special char problems
$char_array = preg_split('//u',$text, -1, PREG_SPLIT_NO_EMPTY);
foreach($char_array as $char) {
imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $char);
$bbox = imagettfbbox($size, 0, $font, $char);
$temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
$temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
}
}
}https://stackoverflow.com/questions/6926613
复制相似问题