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


Posted in PHP onApril 02, 2015

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
PHP 工厂模式使用方法
May 18 PHP
php数组函数序列之array_pop() - 删除数组中的最后一个元素
Nov 07 PHP
php文件操作实例代码
May 10 PHP
php class类的用法详细总结
Oct 17 PHP
PHP函数http_build_query使用详解
Aug 20 PHP
Chrome Web App开发小结
Sep 04 PHP
php实现通用的从数据库表读取数据到数组的函数实例
Mar 21 PHP
PHP数据库连接mysql与mysqli对比分析
Jan 04 PHP
ThinkPHP使用Ueditor的方法详解
May 20 PHP
php 微信公众平台开发模式实现多客服的实例代码
Nov 07 PHP
详谈PHP面向对象中常用的关键字和魔术方法
Feb 04 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
Sep 22 PHP
php实现专业获取网站SEO信息类实例
Apr 02 #PHP
php获得网站访问统计信息类Compete API用法实例
Apr 02 #PHP
php实现从上传文件创建缩略图的方法
Apr 02 #PHP
php调用KyotoTycoon简单实例
Apr 02 #PHP
PHP中数据类型转换的三种方式
Apr 02 #PHP
php在apache环境下实现gzip配置方法
Apr 02 #PHP
PHP中使用socket方式GET、POST数据实例
Apr 02 #PHP
You might like
在Windows中安装Apache2和PHP4的权威指南
2006/10/09 PHP
PHP curl模拟浏览器采集阿里巴巴的实现代码
2011/04/20 PHP
PHP ? EasyUI DataGrid 资料存的方式介绍
2012/11/07 PHP
PHP中对于浮点型的数据需要用不同的方法解决
2014/03/11 PHP
php cookie名使用点号(句号)会被转换
2014/10/23 PHP
php中实现获取随机数组列表的自定义函数
2015/04/02 PHP
php 类自动载入的方法
2015/06/03 PHP
PHP汉字转换拼音的函数代码
2015/12/30 PHP
mysql_escape_string()函数用法分析
2016/04/25 PHP
ExtJS GridPanel 根据条件改变字体颜色
2010/03/08 Javascript
javascript显示用户停留时间的简单实例
2013/08/05 Javascript
Nodejs全栈框架StrongLoop推荐
2014/11/09 NodeJs
深入浅出分析javaScript中this用法
2015/05/09 Javascript
XML文件转化成NSData对象的方法
2015/08/12 Javascript
如何实现JavaScript动态加载CSS和JS文件
2020/12/28 Javascript
jQuery购物网页经典制作案例
2016/08/19 Javascript
javascript 动态生成css代码的两种方法
2017/03/17 Javascript
浅谈Vue响应式(数组变异方法)
2018/05/07 Javascript
node跨域转发 express+http-proxy-middleware的使用
2018/05/31 Javascript
微信小程序实现简单评论功能
2018/11/28 Javascript
jQuery实现当拉动滚动条到底部加载数据的方法分析
2019/01/24 jQuery
js实现数字从零慢慢增加到指定数字示例
2019/11/07 Javascript
js实现适配移动端的拖动效果
2020/01/13 Javascript
NodeJS多种创建WebSocket监听的方式(三种)
2020/06/04 NodeJs
VUE子组件向父组件传值详解(含传多值及添加额外参数场景)
2020/09/01 Javascript
Python 异常处理Ⅳ过程图解
2019/10/18 Python
tensorflow-gpu安装的常见问题及解决方案
2020/01/20 Python
什么是python的列表推导式
2020/05/26 Python
金融专业毕业生推荐信
2013/11/26 职场文书
个人贷款承诺书
2014/03/28 职场文书
酒店管理失职检讨书
2014/09/16 职场文书
2014年高中班主任工作总结
2014/11/08 职场文书
2014年小学美术工作总结
2014/12/20 职场文书
2017年寒假少先队活动总结
2016/04/06 职场文书
Django migrate报错的解决方案
2021/05/20 Python
mybatis中注解与xml配置的对应关系和对比分析
2021/08/04 Java/Android