kerning and freetype

Posted on 2004-11-08
Last Modified: 2012-06-27

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


Question by:twans
    LVL 27

    Accepted 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");


    Author Comment

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


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now