php 限定区域自动调整字体大小的类

PHP在限定的区域里自动调整字体大小的PHP类 imagefittext.class.PHP感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

<?PHP
/**
 * PHP在限定的区域里自动调整字体大小的PHP类 imagefittext.class.PHP
 *
 * @param 
 * @author 编程之家 jb51.cc jb51.cc
 **/
// 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,$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,$this->font,$text); 
		return $t[2]-$t[0]; 
	}
	function TextHeight($text){ 
		$t = imagettfbBox($this->fontsize,$text); 
		return $t[1]-$t[7]; 
	} 
} 

/***   来自编程之家 jb51.cc(jb51.cc)   ***/

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...