For a design project I am required to have spacing between characters; since imagefttext does not support this feature I have created a function which does support this.
The arguments are identical to imagefttext, accept that (array)$extrainfo now accepts the 'character_spacing' spacing parameter. The return values are as expected, and include the image boundaries of the entire string including the character spacing.
The downside is that $angle rotates each letter instead of rotating the entire word (could be seen as a feature on its own).
I hope this is of some use to someone.
- KeepSake
<?php
header("Content-type: image/png");
$image = imagecreatetruecolor(300, 20);
imagefill($image, 0, 0, imagecolorallocate($image, 21, 21, 21));
$imageBox = imagefttext2($image, 9, 0, 2, 15, imagecolorallocate($image, 255, 255, 255), 'tahomabold.ttf', 'The quick brown fox...', array('character_spacing' => 5));
imagepng($image);
imagedestroy($image);
function imagefttext2($imageResource, $font_size, $text_angle, $start_x, $start_y, $color, $font_file, $text, $extra_info = array()) {
if($extra_info['character_spacing'] == NULL || !is_numeric($extra_info['character_spacing'])) {
$extra_info['character_spacing'] = 0;
}
$lastX = $start_x - $extra_info['character_spacing'];
foreach(str_split($text) as $v) {
$coordinates = imagefttext($imageResource, $font_size, $text_angle, $lastX + $extra_info['character_spacing'], $start_y, $color, $font_file, $v, $extra_info);
$lastX = max($coordinates[2], $coordinates[4]);
}
return array($start_x, $start_y, $coordinates[2], $coordinates[3], $coordinates[4], $coordinates[5], $start_x, $coordinates[7]);
}
?>