View Full Version : imageTTFtext spacing

03-08-2005, 08:31 AM

i am use imagettftext - have tried various things, all is great!

How can I control the spacing of characters within a text?

either bring them closer together or have them spaced a bit further appart


03-08-2005, 09:18 AM
I've never heard of 'imagettftext', but you could use a little CSS to do that.....Look HERE (http://www.tizag.com/cssT/reference/letter-spacing.php) ....

03-08-2005, 09:31 AM
Thanks for the CSS example, looking for a PHP solution ...

Using imagettftext to dynamically generate text on images ... like having an email address writen on top of a image ....


03-08-2005, 09:38 AM
chump- this is a GD lib function, which is processed at the server, so css is not applicable

03-08-2005, 09:40 AM
you could try adding spaces between each character like this:

$string = "my string of text";
for($i=0; $i < strlen($string); $i++){
$string[$0] .= " ";
# string now has spaces between letters

03-08-2005, 10:50 AM
Cool, forgive my ignorance....didn't know about those functions... :p

I know this doesn't help jojo any, but what are some examples of when imagettftext (or any of the image functions) is better to use than CSS formatting and positioning...Or is it just an alternative when you are generating dynamic conent (not necessarily better or worse)?

03-08-2005, 05:07 PM
He tryes to position the text INSIDE an image.

03-08-2005, 06:05 PM
the spacing is a good idea, have tried that as well! :)

Wanting so that the user can increase/ decrease the amount of space between the characters ... making the word 'tighter' if required ....

03-08-2005, 07:54 PM
if this helps, this is what I have ....
managed to get the spacing, but it does not look consistent .... probably because each letter within string is a different size ...

// Set the content-type
header("Content-type: image/png");
// Create the image
$im = imagecreate(900, 900);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
// The text to draw
$Text = 'Testing1234...';
$length_string = strlen($Text);
// font file + location
$font_file = 'fonts/arial.ttf';
$Size = 12;
$font_color = $black;
$Spacing = 5;
$line = 3;
$x = 2;
$y = 20;
$a = 0;
//write string at (x,y) in black
for ($i = 1; $i <=$length_string; $i++) {
imagettftext ($im, $Size, 0, $x, $y, $font_color, $font_file, $Text{$a});
$x = $x + 8; // used for x position (spacing)
$a = $a + 1; // used for character within string


03-08-2005, 08:03 PM
ah yes, i didn't think of that, processing each character should work fine as long as you use a monotype font

03-08-2005, 08:22 PM
if it was a prefect world I would stay with monotype! :D

Have to use different fonts as well ... Arial, Tahoma, Times .... etc.

Without the spacing issue, all is fine, now am requested to implement spacing .....

03-16-2005, 09:34 AM
$im = imagecreatetruecolor(300,300);
$text = "Testing Text";
$font_file = "fonts/arial.ttf";
$font_size = 10;
$spacing = 2;
for ($i = 0; $i <strlen($text); $i++)
$arr = imagettftext ($im, $font_size,0, $x, $y, $textcolor, $font_file, $text{$i});
$x = $arr[4]+$spacing;

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum