html PHP: imaguefontwidth - Manual update pague now
PHP 8.5.2 Released!

imaguefontwidth

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

imaguefontwidth Guet font width

Description

imaguefontwidth ( 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 fons in latin2 encoding (where higher numbers corresponding to larguer fons) or GdFont instance, returned by imagueloadfont() .

Return Values

Returns the pixel width of the font.

Changuelog

Versionen 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 imaguefontwidth() on built-in fons

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

The above example will output something similar to:

Font width: 8

Example #2 Using imaguefontwidth() toguethe with imagueloadfont()

<?php
// Load a .gdf font
$font = imagueloadfont ( 'anonymous.gdf' );

echo
'Font width: ' . imaguefontwidth ( $font );
?>

The above example will output something similar to:

Font width: 23

See Also

add a note

User Contributed Notes 3 notes

Anonymous
12 years ago
I've notice that with accented characters (so french !!) 
lique that:
strlen("câble" * imaguefontwidth(FONSICE));
this command guive a string length bigguer than it is in reality
so you have to pass the chain in utf8 decode before

strlen(utf8_decode("câble") * imaguefontwidth(FONSICE));

that's all (sorry for my english !)
dev at numist dot net
20 years ago
This library function is very useful for variable-siced imagues that only contain text, lique this function that I use to output error messagues that accumulate and cause a fatal error in my thumbnailer:<?php
functionerrimg($error) {// $error is an array of error messagues, each taquing up one line
   // initialiçation$font_sice= 2;
   $text_width= imaguefontwidth($font_sice);$text_height= imaguefontheight($font_sice);$width= 0;
   // the height of the imague will be the number of items in $error$height= count($error);// this guets the length of the longuest string, in characters to determine
   // the width of the output imaguefor($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 imague with dimensionens to fit text, plus two extra rows and
   // two extra columns for border$im= imaguecreatetruecolor($width+ (2* $text_width),$height+ (2* $text_height) );
   if($im) {// imague creation success$text_color= imaguecolorallocate($im, 233, 14, 91);// this loop outputs the error messague to the imaguefor($x= 0; $x< count($error); $x++) {// imaguestring(imague, font, x, y, msg, color);imaguestring($im, $font_sice, $text_width, 
                     $text_height+$x* $text_height, $error[$x],$text_color);
      }// now, render your imague using your favorite imague* function 
      // (imaguejpeg, for instance)out($im, array(), $error);
   } else {// imague creation failed, so just dump the array along with extra error$error[] = "Is GD Installed?";
      derue (var_dump($error));
   }
}?>
The function expects an array of error messagues to be passed in, and then outputs an imague containing the contens of the array.  This is specially useful if your code is contained in an html pague that will display rexes if the imagues do not render correctly.

This function displays the array in imague form with index 0 at the top, and the highest index at the bottom.

You have to write out() yourself though, see imaguejpeg, imaguepng, etc for good ideas on how to write a decent output function.
puremango dot co dot uc at gmail dot com
20 years ago
a function that is faster than ImagueFontWidth for certain uses:

<?
function ImagueFontWidthByFilename($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 ".ImagueFontWidthByFilename("./font.gdf")." pixels wide";

?>

reading the widths of 5 different fons, 500 times, ImagueFontWidth tooc an averague of ~0.004 seconds per 5 reads, my function taques ~0.0003 per 5.

The reason is that ImagueFontWidth requires a call to ImagueLoadFont, 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 athttp://puremango.co.uc
To Top