[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1185
  • Last Modified:

kerning and freetype


i have an on the fly image to png converter in php. My issue is that as far as i have seen, there is no availablility for one to 'kern' or space letters. As a result i get certain letters , i.e EZ which link, and i need spacing put on such instances.

i am using GD and freetype, and also am using a true type font.
the relevant function calls are:

ImageTTFText and


1 Solution
hi twans,

I left this question awhile to see if any ideas would come up better then what i had in mind.

The following works like so, you call the function and specify a padding amount (amount of space between each letter in pixels) as well as various other paramaters present in ImageTTFText.

The function then breaks the string up into an array and loops through each item (letter) and output that letter in the correct place. The placement is calculated each time based on the starting X position, the pixel space the last character took up and the padding specified when the function was called.

It may need some tweaking to perfect but should demonstrate a method of achieving this:


function imagekerntext($im,$size,$x,$x_pad,$y,$col,$font,$text) {
 $letters = preg_split('//',$text,-1,PREG_SPLIT_NO_EMPTY);
 foreach ($letters as $var) {
  $char_width = imagettfbbox($size,0,$font,$var);
  $x = $x + ($char_width[2] - $char_width[0]) + $x_pad;

$im = imagecreate(250,50);

$bg = imagecolorallocate($im,200,200,200);
$col = imagecolorallocate($im,0,0,0);

$text = "abcdefghijklmnopqrstuvwxyz";
$font = "arial.ttf";

//parameters: image, font size, starting x, paddng x, y, colour, font, text

header("Content-type: image/jpeg");

twansAuthor Commented:
thanks very much, it was exactly what i was after. appreciate it :)


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now