Jump to content

Centering Text in a GD Generated Security Image


dooper3

Recommended Posts

I have written code to create an image on the fly to conduct a Turing (sp?!) test, the problem is, I can't seem to center the text in the image properly no matter what I try! Below is the code for my image centering calculations:

//Set the positioning for the text
$fromleft = (($img['width'] / 2) - ((imagefontwidth($img['font']) * $img['string_length']) / 2));
$fromtop =  (($img['height'] / 2) + (imagefontheight($img['font'])));
//Create the text itself
imagettftext($img['handle'],$img['font_size'],$img['random_angle'],$fromleft,$fromtop,$color,$img['font'],$img['random_string']);

No matter what I do it always ends up further to the right than in the center.

 

Thanks!

Solved it myself! wasn't taking into account the font size, the x-positioning line now reads:

$fromleft = (($img['width'] / 2) - (imagefontwidth($img['font']) * $img['string_length'] * ($img['font_size'] / 12)));

If anyone else out there wanted to know...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.