使用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 = $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]; } 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); } ?>
关于使用php怎么在限定区域中调整字体大小问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。