php字体大小_规定区域内字体大小自适应php代码

规定区域内字体大小自适应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 -= $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 = $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 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); } ?>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30187653/viewspace-1484646/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值