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

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

kerning and freetype

heya,

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
ImageTTFBBox

tia

-twans
0
twans
Asked:
twans
1 Solution
 
Diablo84Commented:
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:

_____________________________________________________________________

<?php
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);
  imagettftext($im,$size,0,$x,$y,$col,$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
imagekerntext($im,12,5,2,15,$col,$font,$text);

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

imagedestroy($im);
?>
0
 
twansAuthor Commented:
diablo,
 
thanks very much, it was exactly what i was after. appreciate it :)

-twans
0

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