• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php实现在限定区域里自动调整字体大小的类实例_PHP

php 搞代码 3年前 (2022-01-25) 14次浏览 已收录 0个评论

本文实例讲述了php实现在限定区域里自动调整字体大小的类。分享给大家供大家参考。具体如下:

这里的php类imagefittext.class.php实现在限定的区域里自动调整字体大小的功能。

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 = & $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 -= $thi<span>%本文来源gaodai#ma#com搞*代#码9网#</span><strong>搞gaodaima代码</strong>s->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($wraplengthTextWidth($text_)/$width));    $wraplength = $wraplengthstep_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]; } function TextHeight($text){  $t = imagettfbbox($this->fontsize, 0, $this->font, $text);  return $t[1]-$t[7]; }}?>

使用范例如下:

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 boximagerectangle($im, $x1, $y1, $x1+$width, $y1+$height, $white);// Write the text            +8 because the text will move up originallyimagettftext($im, $fit['fontsize'], 0, $x1+$padding, $y1+$padding+8, $white, $font, $fit['text']);// Print some info. about the textimagestring($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 imageheader ('Content-Type: image/png');imagepng($im);imagedestroy($im);function microtime_float(){ // Timer list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);}?>

希望本文所述对大家的php程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php实现在限定区域里自动调整字体大小的类实例_PHP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址