This example describes the PHP implementation of a class that automatically adjusts the font size in a qualified area. Share to everyone for your reference. Specifically as follows:
The PHP class here imagefittext.class.php the ability to automatically adjust the font size in a restricted area.
<?php//Image fit Text Class 0.1 by ming0070913 class imagefittext{public $font, $fontsize, $width, $height;
Public $step _wrap, $step _fontsize;
Public function __construct ($font, $step _wrap=1, $step _fontsize=1) {$this->font = $font;
$this->step_wrap = $step _wrap>1 $step _wrap:1;
$this->step_fontsize = $step _fontsize>1 $step _fontsize:1; function Fit ($width, $height, $text, $fontsize, $min _fontsize=5, $min _wraplength=0) {$this->fontsize = & $font
Size
$text _ = $text; while ($this->textheight ($text _) > $height && $fontsize > $min _fontsize) $fontsize-= $this->step_
FontSize; while ($this->textwidth ($text _) > $width | | $this->textheight ($text _) > $height) && $fontsize >$
Min_fontsize) {$fontsize-= $this->step_fontsize;
$wraplength = $this->maxlen ($text);
$text _ = $text; while ($this->textwidth ($text _) > $width && $wraplength >= $min _wraplength+ $this->step_wrap) {$ WraplengTH-= $this->step_wrap;
$text _ = WordWrap ($text, $wraplength, "\ n", true);
To speed Up:if ($this->textheight ($text _) > $height) break;
if ($wraplength <= $min _wraplength) break;
$wraplength _ = $wraplength;
$wraplength = Ceil ($wraplength/($this->textwidth ($text _)/$width)); $wraplength = $wraplength < ($min _wraplength+ $this->step_wrap)?
($min _wraplength+ $this->step_wrap): $wraplength;
}} $this->width = $this->textwidth ($text _);
$this->height = $this->textheight ($text _); Return Array ("FontSize" => $fontsize, "text" => $text _, "width" => $this->width, "height" => $this->
height);
function MaxLen ($text) {$lines = explode ("\ n", Str_replace ("\ R", "", $text));
foreach ($lines as $line) $t [] = strlen ($line);
Return Max ($t);
function TextWidth ($text) {$t = Imagettfbbox ($this->fontsize, 0, $this->font, $text);
return $t [2]-$t [0]; The function TextHeight ($text) {$t = Imagettfbbox ($this->fonTsize, 0, $this->font, $text);
return $t [1]-$t [7]; }}?>
The
Use examples are as follows:
<?php//Image fit Text Class 0.1 by ming0070913//Example File include "imagefittext.class.php"; Settings://The text $text = "PHP is a widely-used general-purpose scripting language This is especially suited for W EB development and can is embedded into HTML. If you are are new to PHP and want to get some idea of how it works, try the introductory tutorial.
After this, check out the online manual. "
The maximun width $width = 200;
The maximun height $height = 100;
Position of the text and the box $x 1 = 50;
$y 1 = 50;
The starting font size $fontsize = 10; The Minimun font size.
The script would stop if it cannot fit to the text even with this size.
$min _fontsize = 3; The Minimun wrap length for each line.
The script'll try another font size if it cannot fit the "text even with" this wrap length.
$min _wraplength = 0;
The font $font = "Arial.ttf"; The space between the box and the text.
It ' s independent to the script which can be ignored $padding = 3; If theScript cannot fit the text for certain wrap length, it'll try the wrap length again with the reduction in this value.
It reduce the accuracy, but'll slightly speed up the process.
$step _wrap = 1; If The script cannot fit is the text for certain font size, it'll try the font size again with the reduction in this
Value.
It reduce the accuracy, but'll slightly speed up the process.
$step _fontsize = 1; Create a image $im = @imagecreatetruecolor ($width + $x 1*2, $height + $y 1*2+80) or Die (' cannot Initialize new GD image Strea
M ');
Start Timer $time _start = Microtime_float ();
The class $imagefittext = new Imagefittext ($font, $step _wrap, $step _fontsize); Fit the text//It Returns the result in an array with "FontSize", "Text", "width", "height" $fit = $imagefittext->fi
T ($width-$padding *2, $height-$padding *2, $text, $fontsize, $min _fontsize, $min _wraplength);
Stop Timer $time = round (Microtime_float ()-$time _start, 3); $white = Imagecolorallocate ($im, 255,255, 255);
Draw a box Imagerectangle ($im, $x 1, $y 1, $x 1+ $width, $y 1+ $height, $white); Write the text +8 because the text would move up originally Imagettftext ($im, $fit [' fontsize '], 0, $x 1+ $paddi
Ng, $y 1+ $padding +8, $white, $font, $fit [' text ']); Print some info.
About the text imagestring ($im, 5, $x 1, $y 1+ $height +30, ' fontsize: '. $fit [' fontsize '], $white); Imagestring ($im, 5, $x 1, $y 1+ $height +45, ' Text Size: '. $fit [' width ']. "
X ". $fit [' height '], $white); Imagestring ($im, 5, $x 1, $y 1+ $height +60, ' Box Size: '. $width-$padding *2). " X ".
($height-$padding *2), $white);
Imagestring ($im, 5, $x 1, $y 1+ $height +75, ' time used: '. $time. ' s ', $white);
Print the image header (' content-type:image/png ');
Imagepng ($im);
Imagedestroy ($im);
function Microtime_float () {//Timer list ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec); }?>
I hope this article will help you with your PHP programming.