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中使用灵巧的体系结构
Oct 09 PHP
PHP文本操作类
Nov 25 PHP
php heredoc和phpwind的模板技术使用方法小结
Mar 28 PHP
php select,radio和checkbox默认选择的实现方法
May 15 PHP
smarty基础之拼接字符串的详解
Jun 18 PHP
php和jquery实现地图区域数据统计展示数据示例
Feb 12 PHP
php的慢速日志引起的Mysql错误问题分析
May 13 PHP
PHP+Mysql+jQuery实现发布微博程序 php篇
Oct 15 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
Dec 31 PHP
ThinkPHP框架里隐藏index.php
Apr 12 PHP
php上传后台无法收到数据解决方法
Oct 28 PHP
ThinkPHP3.1.2 使用cli命令行模式运行的方法
Apr 14 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
如何实现给定日期的若干天以后的日期
2006/10/09 PHP
php下通过伪造http头破解防盗链的代码
2010/07/03 PHP
怎么在Windows系统中搭建php环境
2013/08/31 PHP
WordPress中获取所使用的模板的页面ID的简单方法
2015/12/31 PHP
PHP实现验证码校验功能
2017/11/16 PHP
JavaScript Event学习第四章 传统的事件注册模型
2010/02/07 Javascript
jQuery的实现原理的模拟代码 -3 事件处理
2010/08/03 Javascript
Ubuntu 11.10 安装Node.js的方法
2011/11/30 Javascript
JavaScript中的style.display属性操作
2013/03/27 Javascript
jQuery使用slideUp方法实现控制元素缓慢收起
2015/03/27 Javascript
JQuery validate插件Remote用法大全
2016/05/15 Javascript
利用CSS、JavaScript及Ajax实现图片预加载的方法
2016/11/29 Javascript
EasyUI学习之DataGird分页显示数据
2016/12/29 Javascript
vue+vux实现移动端文件上传样式
2017/07/28 Javascript
详解ES6通过WeakMap解决内存泄漏问题
2018/03/09 Javascript
vue如何使用async、await实现同步请求
2019/12/09 Javascript
python微信跳一跳系列之自动计算跳一跳距离
2018/02/26 Python
python 阶乘累加和的实例
2019/02/01 Python
Python爬虫库requests获取响应内容、响应状态码、响应头
2020/01/25 Python
GDAL 矢量属性数据修改方式(python)
2020/03/10 Python
Python读入mnist二进制图像文件并显示实例
2020/04/24 Python
Python基于network模块制作电影人物关系图
2020/06/19 Python
python爬虫请求头的使用
2020/12/01 Python
智利最大的网上商店:Linio智利
2016/11/24 全球购物
PPP协议组成及简述协议协商的基本过程
2015/05/28 面试题
Servlet如何得到客户端机器的信息
2014/10/17 面试题
环保专业大学生职业规划设计
2014/01/10 职场文书
三分钟演讲稿事例
2014/03/03 职场文书
学历公证书范本
2014/04/09 职场文书
求职信格式要求
2014/05/23 职场文书
党建工作经验交流材料
2014/05/25 职场文书
酒店人事专员岗位职责
2015/04/07 职场文书
学习雷锋精神倡议书
2015/04/27 职场文书
一文搞懂如何实现Go 超时控制
2021/03/30 Python
用JS创建一个录屏功能
2021/11/11 Javascript
mybatis 获取更新记录的id
2022/05/20 Java/Android