Skip to content

unlocomqx/text-measure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GD precise text size measurement

This code aims to provide a simple way to get a precise GD text size and position by building upon the imagettfbbox function.

To use this library, include the TextMeasure class and write something like this:

$text_measure = new TextMeasure($text, $font_path, $size);
$measure = $text_measure->measureText();

The measureText method will return an array with this structure

array (size=4)
  'x' => int -9
  'y' => int 211
  'width' => int 1560
  'height' => int 258

You can then use this array to create your text like this:

$gd_image = imagecreatetruecolor($measure['width'], $measure['height']);
imagettftext($gd_image, $size, 0, $measure['x'], $measure['y'], $red, $font_path, $text);

Here's a preview of the resulting image:

Technical details

This code first renders the text following the result given by imagettfbbox and uses black for background and red for text.

Then it checks for edges containing red pixels and resizes/moves the measurement accordingly.

Then it trims the image by removing black/empty regions to produce a precise measurement.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages