欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-04-03 08:29:09
本文实例讲述了php实现在限定区域里自动调整字体大小的类。分享给大家供大家参考。具体如下: 这里的php类imagefittext.class.php实现在限定的区域里自...

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

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

<?php
// image fit text class 0.1 by ming0070913
class imagefittext{
 public $font, $fontsize, $width, $height;
 public $step_wrap, $step_fontsize;
 public function __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;
 }
 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];
 }
}
?>

使用范例如下:

<?php
// image fit text class 0.1 by ming0070913
// example file
include "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程序设计有所帮助。