SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Hervard's Avatar
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Counting the Pixel-widths of Font Characters

    Is there any way using PHP to get the pixel-width of a character in a font?

    For example, I would like to be able to determine the width of a sentence (by calculating the pixel-width of each character) using the font Verdana. Is it possible, and how would I go about doing it?

  2. #2
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a tricky one. What you could do, however, is write the string to an image, then check the image size.

    Try the Image functions.

  3. #3
    SitePoint Enthusiast Hervard's Avatar
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, thanks, I'll have a look at trying that out. Sounds like it might do the trick.

  4. #4
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  5. #5
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dmsuperman
    Holy Jesus. PHP > Me.

  6. #6
    SitePoint Enthusiast Hervard's Avatar
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes, that looks like it's right on the money. However, I'm having a bit of difficulty figuring out how to use that function. The imagefontwidth() page mentions the imageloadfont() function, so I'm assuming I have to first load in the font that I want to use, and then deal with imagefontwidth().

    I can't seem to get imageloadfont() working. I've got verdana.ttf placed in the same directory as my PHP file.

    imageloadfont('verdana.ttf') results in a warning saying "Error reading font". I've tried putting the full path to the file in, no luck, and putting in the URL to the file makes it say that the "stream does not support seeking". How can I get it to load the font?

    Update: It would appear that it can access verdana.ttf just fine, but it just can't read it properly for some reason. The font I want to do stuff with is Verdana. How can I get it to be read without error?

    Update: Did some searching, and it appears that imageloadfont() only supports bitmap fonts, which means that TrueType fonts won't work...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •