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令牌 Token改进版
Jul 18 PHP
用mysql触发器自动更新memcache的实现代码
Oct 11 PHP
简单的php写入数据库类代码分享
Jul 26 PHP
从PHP $_SERVER相关参数判断是否支持Rewrite模块
Sep 26 PHP
php中利用str_pad函数生成数字递增形式的产品编号
Sep 30 PHP
wordpress自定义url参数实现路由功能的代码示例
Nov 28 PHP
php检测url是否存在的方法
Apr 14 PHP
PHP结合Jquery和ajax实现瀑布流特效
Jan 07 PHP
PHP与服务器文件系统的简单交互
Oct 21 PHP
form表单传递数组数据、php脚本接收的实例
Feb 09 PHP
phpquery中文手册
Mar 18 PHP
php修改word的实例方法
Nov 17 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
开源SNS系统-ThinkSNS
2008/05/18 PHP
一个简单的网页密码登陆php代码
2012/07/17 PHP
PHP中上传文件打印错误错误类型分析
2019/04/14 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
2019/05/13 PHP
PHP命名空间与自动加载机制的基础介绍
2019/08/25 PHP
基于jquery &amp; json的省市区联动代码
2012/06/26 Javascript
web基于浏览器的本地存储方法应用
2012/11/27 Javascript
jquery动态增加text元素以及删除文本内容实例代码
2013/07/01 Javascript
jquery中交替点击事件toggle方法的使用示例
2013/12/08 Javascript
利用js正则表达式验证手机号,email地址,邮政编码
2014/01/23 Javascript
教你如何在 Javascript 文件里使用 .Net MVC Razor 语法
2014/07/23 Javascript
js实现HashTable(哈希表)的实例分析
2016/11/21 Javascript
iscroll动态加载数据完美解决方法
2017/07/18 Javascript
全选复选框JavaScript编写小结(附代码)
2017/08/16 Javascript
详解ECMAScript typeof用法
2018/07/25 Javascript
关于Node.js中频繁修改代码重启服务器的问题
2020/10/15 Javascript
Python读写Excel文件的实例
2013/11/01 Python
python基于BeautifulSoup实现抓取网页指定内容的方法
2015/07/09 Python
Python中进程和线程的区别详解
2017/10/29 Python
基于python log取对数详解
2018/06/08 Python
python利用7z批量解压rar的实现
2019/08/07 Python
python2爬取百度贴吧指定关键字和图片代码实例
2019/08/14 Python
opencv-python 提取sift特征并匹配的实例
2019/12/09 Python
Python时间差中seconds和total_seconds的区别详解
2019/12/26 Python
python字典的值可以修改吗
2020/06/29 Python
Python+Selenium随机生成手机验证码并检查页面上是否弹出重复手机号码提示框
2020/09/21 Python
python3中for循环踩过的坑记录
2020/12/14 Python
python解决OpenCV在读取显示图片的时候闪退的问题
2021/02/23 Python
Sunglasses Shop瑞典:欧洲领先的太阳镜网上商店
2018/04/22 全球购物
Haglöfs瑞典官方网站:haglofs火柴棍,欧洲顶级户外品牌
2018/10/18 全球购物
自荐信格式简述
2014/01/25 职场文书
运动会广播稿150字(9篇)
2014/09/20 职场文书
县政府领导班子“四风”方面突出问题整改措施
2014/09/23 职场文书
2014年保密工作总结
2014/11/22 职场文书
2015年检察院个人工作总结
2015/05/20 职场文书
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2021/08/30 Java/Android