centering text/different fonts

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
danhash
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Aug 08, 2003 11:26 am
Contact:

Fri Aug 08, 2003 11:51 am

how do you center text in an image? i'm trying to have a form where a user types in a text string, font size, and font color, and have php output it to a file with the text centered. for the x-axis this seemed to work: (($width-(strlen($text))*($fontSize + 4))/2)+1, but what about the y-axis? I couldn't get ANYTHIN to work. and how to you use other fonts? how would you detect the width and height of a string of text in a non-fixed-width font?
<?php
print "|2|-||2 R0><0R\$!!";
?>

User avatar
swirlee
Moderator
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Fri Aug 08, 2003 1:22 pm

I'm going to assume that you're using GD, and it looks as though you're going about this the most difficult way imaginable. Also, it looks as though you may've skipped, or at least skimped on, the documentation.

Use the imagettftext() function to create text using a TrueType font.

Use the imagettfbbox() function to find out the dimensions of a particular string as rendered in your font of choice. Once you get the dimensions, then it's easy to figure ought how far left and above center you need to render it to center-align it.

This will save you a lot of math, and save you from using PHP's fugly bulit-in fonts.
Image

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest