本文实例讲述了php实现在限定区域里自动调整字体大小的类。分享给大家供大家参考。具体如下:
这里的php类imagefittext.class.php实现在限定的区域里自动调整字体大小的功能。
<"\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)"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]; } function TextHeight($text){ $t = imagettfbbox($this->fontsize, 0, $this->font, $text); return $t[1]-$t[7]; } } "htmlcode"><"imagefittext.class.php"; // Settings : // The text $text = "PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual."; // The maximun width $width = 200; // The maximun height $height = 100; // Position of the text and the box $x1 = 50; $y1 = 50; // The starting font size $fontsize = 10; // The minimun font size. The script will stop if it cannot fit the text even with this size. $min_fontsize = 3; // The minimun wrap length for each line. The script will 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 the script cannot fit the text for certain wrap length, it will try the wrap length again with the reduction in this value. // It reduce the accuracy, but will slightly speed up the process. $step_wrap = 1; // If the script cannot fit the text for certain font size, it will try the the font size again with the reduction in this value. // It reduce the accuracy, but will slightly speed up the process. $step_fontsize = 1; // Create a image $im = @imagecreatetruecolor($width+$x1*2, $height+$y1*2+80) or die('Cannot Initialize new GD image stream'); // Start the timer $time_start = microtime_float(); // The class $imagefittext = new ImageFitText($font, $step_wrap, $step_fontsize); // Fit the text // It returns the result in a array with "fontsize", "text", "width", "height" $fit = $imagefittext->fit($width-$padding*2, $height-$padding*2, $text, $fontsize, $min_fontsize, $min_wraplength); // Stop the timer $time = round(microtime_float()-$time_start, 3); $white = imagecolorallocate($im, 255, 255, 255); // Draw a box imagerectangle($im, $x1, $y1, $x1+$width, $y1+$height, $white); // Write the text +8 because the text will move up originally imagettftext($im, $fit['fontsize'], 0, $x1+$padding, $y1+$padding+8, $white, $font, $fit['text']); // Print some info. about the text imagestring($im, 5, $x1, $y1+$height+30, 'Fontsize : '.$fit['fontsize'], $white); imagestring($im, 5, $x1, $y1+$height+45, 'Text Size : '.$fit['width']."x".$fit['height'], $white); imagestring($im, 5, $x1, $y1+$height+60, 'Box Size : '.($width-$padding*2)."x".($height-$padding*2), $white); imagestring($im, 5, $x1, $y1+$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); } ?>希望本文所述对大家的php程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】