php实现在限定区域里自动调整字体大小的类实例
本文实例讲述了php实现在限定区域里自动调整字体大小的类。分享给大家供大家参考。具体如下:
这里的php类imagefittext.class.php实现在限定的区域里自动调整字体大小的功能。
<?php //ImageFitTextClass0.1byming0070913 CLASSImageFitText{ public$font,$fontsize,$width,$height; public$step_wrap,$step_fontsize; publicfunction__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; } functionfit($width,$height,$text,$fontsize,$min_fontsize=5,$min_wraplength=0){ $this->fontsize=&$fontsize; $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); //Tospeedup: 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_); returnarray("fontsize"=>$fontsize,"text"=>$text_,"width"=>$this->width,"height"=>$this->height); } functionmaxLen($text){ $lines=explode("\n",str_replace("\r","",$text)); foreach($linesas$line) $t[]=strlen($line); returnmax($t); } functionTextWidth($text){ $t=imagettfbbox($this->fontsize,0,$this->font,$text); return$t[2]-$t[0]; } functionTextHeight($text){ $t=imagettfbbox($this->fontsize,0,$this->font,$text); return$t[1]-$t[7]; } } ?>
使用范例如下:
<?php //ImageFitTextClass0.1byming0070913 //ExampleFile include"imagefittext.class.php"; //Settings: //Thetext $text="PHPisawidely-usedgeneral-purposescriptinglanguagethatisespeciallysuitedforWebdevelopmentandcanbeembeddedintoHTML.IfyouarenewtoPHPandwanttogetsomeideaofhowitworks,trytheintroductorytutorial.Afterthat,checkouttheonlinemanual."; //Themaximunwidth $width=200; //Themaximunheight $height=100; //Positionofthetextandthebox $x1=50; $y1=50; //Thestartingfontsize $fontsize=10; //Theminimunfontsize.Thescriptwillstopifitcannotfitthetextevenwiththissize. $min_fontsize=3; //Theminimunwraplengthforeachline.Thescriptwilltryanotherfontsizeifitcannotfitthetextevenwiththiswraplength. $min_wraplength=0; //Thefont $font="arial.ttf"; //Thespacebetweentheboxandthetext.It'sindependenttothescriptwhichcanbeignored $padding=3; //Ifthescriptcannotfitthetextforcertainwraplength,itwilltrythewraplengthagainwiththereductioninthisvalue. //Itreducetheaccuracy,butwillslightlyspeeduptheprocess. $step_wrap=1; //Ifthescriptcannotfitthetextforcertainfontsize,itwilltrythethefontsizeagainwiththereductioninthisvalue. //Itreducetheaccuracy,butwillslightlyspeeduptheprocess. $step_fontsize=1; //Createaimage $im=@imagecreatetruecolor($width+$x1*2,$height+$y1*2+80)ordie('CannotInitializenewGDimagestream'); //Startthetimer $time_start=microtime_float(); //Theclass $imagefittext=newImageFitText($font,$step_wrap,$step_fontsize); //Fitthetext //Itreturnstheresultinaarraywith"fontsize","text","width","height" $fit=$imagefittext->fit($width-$padding*2,$height-$padding*2,$text,$fontsize,$min_fontsize,$min_wraplength); //Stopthetimer $time=round(microtime_float()-$time_start,3); $white=imagecolorallocate($im,255,255,255); //Drawabox imagerectangle($im,$x1,$y1,$x1+$width,$y1+$height,$white); //Writethetext+8becausethetextwillmoveuporiginally imagettftext($im,$fit['fontsize'],0,$x1+$padding,$y1+$padding+8,$white,$font,$fit['text']); //Printsomeinfo.aboutthetext imagestring($im,5,$x1,$y1+$height+30,'Fontsize:'.$fit['fontsize'],$white); imagestring($im,5,$x1,$y1+$height+45,'TextSize:'.$fit['width']."x".$fit['height'],$white); imagestring($im,5,$x1,$y1+$height+60,'BoxSize:'.($width-$padding*2)."x".($height-$padding*2),$white); imagestring($im,5,$x1,$y1+$height+75,'Timeused:'.$time.'s',$white); //Printtheimage header('Content-Type:image/png'); imagepng($im); imagedestroy($im); functionmicrotime_float(){//Timer list($usec,$sec)=explode("",microtime()); return((float)$usec+(float)$sec); } ?>
希望本文所述对大家的php程序设计有所帮助。