GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Downloads
  • Documentation
  • Get Involved
  • Help
  • Getting Started
  • Introduction
  • A simple tutorial
  • Language Reference
  • Basic syntax
  • Types
  • Variables
  • Constants
  • Expressions
  • Operators

This view

  • Original page
  • New address

GeezerVison is showing www.php.net — PHP: imagefontwidth - Manual

PHP 8.6.0 RC5 available for testing
    imageftbbox »
    « imagefontheight
    • Preface
    • Function Reference
    • Image Processing and Generation
    • GD
    • GD and Image Functions

    imagefontwidth

    (PHP 4, PHP 5, PHP 7, PHP 8)

    imagefontwidth — Get font width

    Description

    function imagefontwidth(GdFont|int $font): int

    Returns the pixel width of a character in font.

    Parameters

    font

    Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers correspond to larger fonts) or GdFont instance, returned by imageloadfont().

    Return Values

    Returns the pixel width of the font.

    Changelog

    Version Description
    8.1.0 The font parameter now accepts both an GdFont instance and an int; previously only int was accepted.

    Examples

    Example #1 Using imagefontwidth() on built-in fonts

    <?php
    echo 'Font width: ' . imagefontwidth(4);
    ?>

    The above example will output something similar to:

    Font width: 8

    Example #2 Using imagefontwidth() together with imageloadfont()

    <?php
    // Load a .gdf font
    $font = imageloadfont('anonymous.gdf');
    
    echo 'Font width: ' . imagefontwidth($font);
    ?>

    The above example will output something similar to:

    Font width: 23

    See Also

    • imagefontheight() - Get font height
    • imageloadfont() - Load a new font

    Found A Problem?

    Learn How To Improve This Page • Submit a Pull Request • Report a Bug
    +add a note

    User Contributed Notes 3 notes

    up
    down
    -1
    Anonymous ¶
    13 years ago
    I've notice that with accented characters (so french !!) 
    like that:
    strlen("câble" * imagefontwidth(FONTSIZE));
    this command give a string length bigger than it is in reality
    so you have to pass the chain in utf8 decode before
    
    strlen(utf8_decode("câble") * imagefontwidth(FONTSIZE));
    
    that's all (sorry for my english !)
    up
    down
    -4
    dev at numist dot net ¶
    21 years ago
    This library function is very useful for variable-sized images that only contain text, like this function that I use to output error messages that accumulate and cause a fatal error in my thumbnailer:
    
    <?php
    function errimg($error) {
       // $error is an array of error messages, each taking up one line
       // initialization
       $font_size = 2;
       $text_width = imagefontwidth($font_size);
       $text_height = imagefontheight($font_size);
       $width = 0;
       // the height of the image will be the number of items in $error
       $height = count($error);
    
       // this gets the length of the longest string, in characters to determine
       // the width of the output image
       for($x = 0; $x < count($error); $x++) {
          if(strlen($error[$x]) > $width) {
             $width = strlen($error[$x]);
          }
       }
       
       // next we turn the height and width into pixel values
       $width = $width * $text_width;
       $height = $height * $text_height;
       
       // create image with dimensions to fit text, plus two extra rows and
       // two extra columns for border
       $im = imagecreatetruecolor($width + ( 2 * $text_width ), 
                                  $height + ( 2 * $text_height ) );
       if($im) {
          // image creation success
          $text_color = imagecolorallocate($im, 233, 14, 91);
          // this loop outputs the error message to the image
          for($x = 0; $x < count($error); $x++) {
             // imagestring(image, font, x, y, msg, color);
             imagestring($im, $font_size, $text_width, 
                         $text_height + $x * $text_height, $error[$x], 
                         $text_color);
          }
          // now, render your image using your favorite image* function 
          // (imagejpeg, for instance)
          out($im, array(), $error);
       } else {
          // image creation failed, so just dump the array along with extra error
          $error[] = "Is GD Installed?";
          die(var_dump($error));
       }
    }
    ?>
    
    The function expects an array of error messages to be passed in, and then outputs an image containing the contents of the array.  This is especially useful if your code is contained in an html page that will display rexes if the images do not render correctly.
    
    This function displays the array in image form with index 0 at the top, and the highest index at the bottom.
    
    You have to write out() yourself though, see imagejpeg, imagepng, etc for good ideas on how to write a decent output function.
    up
    down
    -4
    puremango dot co dot uk at gmail dot com ¶
    21 years ago
    a function that is faster than ImageFontWidth for certain uses:
    
    <?
    function ImageFontWidthByFilename($filename)
    {
        $handle = @fopen($font_locations[$i],"r");
        $c_wid = @fread($handle,11);
        @fclose($handle);
        return(ord($c_wid{8})+ord($c_wid{9})+ord($c_wid{10})+ord($c_wid{11}));
    }
    
    echo "./font.gdf is ".ImageFontWidthByFilename("./font.gdf")." pixels wide";
    
    ?>
    
    reading the widths of 5 different fonts, 500 times, ImageFontWidth took an average of ~0.004 seconds per 5 reads, my function takes ~0.0003 per 5.
    
    The reason is that ImageFontWidth requires a call to ImageLoadFont, but if for some reason you won't need to load the font, just find out the width, this is the function for you :-)
    
    more such wonders at http://puremango.co.uk
    +add a note
    • GD and Image Functions
      • gd_​info
      • getimagesize
      • getimagesizefromstring
      • image_​type_​to_​extension
      • image_​type_​to_​mime_​type
      • image2wbmp
      • imageaffine
      • imageaffinematrixconcat
      • imageaffinematrixget
      • imagealphablending
      • imageantialias
      • imagearc
      • imageavif
      • imagebmp
      • imagechar
      • imagecharup
      • imagecolorallocate
      • imagecolorallocatealpha
      • imagecolorat
      • imagecolorclosest
      • imagecolorclosestalpha
      • imagecolorclosesthwb
      • imagecolordeallocate
      • imagecolorexact
      • imagecolorexactalpha
      • imagecolormatch
      • imagecolorresolve
      • imagecolorresolvealpha
      • imagecolorset
      • imagecolorsforindex
      • imagecolorstotal
      • imagecolortransparent
      • imageconvolution
      • imagecopy
      • imagecopymerge
      • imagecopymergegray
      • imagecopyresampled
      • imagecopyresized
      • imagecreate
      • imagecreatefromavif
      • imagecreatefrombmp
      • imagecreatefromgd
      • imagecreatefromgd2
      • imagecreatefromgd2part
      • imagecreatefromgif
      • imagecreatefromjpeg
      • imagecreatefrompng
      • imagecreatefromstring
      • imagecreatefromtga
      • imagecreatefromwbmp
      • imagecreatefromwebp
      • imagecreatefromxbm
      • imagecreatefromxpm
      • imagecreatetruecolor
      • imagecrop
      • imagecropauto
      • imagedashedline
      • imageellipse
      • imagefill
      • imagefilledarc
      • imagefilledellipse
      • imagefilledpolygon
      • imagefilledrectangle
      • imagefilltoborder
      • imagefilter
      • imageflip
      • imagefontheight
      • imagefontwidth
      • imageftbbox
      • imagefttext
      • imagegammacorrect
      • imagegd
      • imagegd2
      • imagegetclip
      • imagegetinterpolation
      • imagegif
      • imagegrabscreen
      • imagegrabwindow
      • imageinterlace
      • imageistruecolor
      • imagejpeg
      • imagelayereffect
      • imageline
      • imageloadfont
      • imageopenpolygon
      • imagepalettecopy
      • imagepalettetotruecolor
      • imagepng
      • imagepolygon
      • imagerectangle
      • imageresolution
      • imagerotate
      • imagesavealpha
      • imagescale
      • imagesetbrush
      • imagesetclip
      • imagesetinterpolation
      • imagesetpixel
      • imagesetstyle
      • imagesetthickness
      • imagesettile
      • imagestring
      • imagestringup
      • imagesx
      • imagesy
      • imagetruecolortopalette
      • imagettfbbox
      • imagettftext
      • imagetypes
      • imagewbmp
      • imagewebp
      • imagexbm
      • iptcembed
      • iptcparse
      • jpeg2wbmp
      • png2wbmp
    • Deprecated
      • imagedestroy
    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.