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+dbfile开发小型留言本
Oct 09 PHP
phpinfo 系统查看参数函数代码
Jun 05 PHP
php产生随机数的两种方法实例代码 输出随机IP
Apr 08 PHP
浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题
Aug 06 PHP
php获得用户ip地址的比较不错的方法
Feb 08 PHP
php环境套包 dedeampz 伪静态设置示例
Mar 26 PHP
使用symfony命令创建项目的方法
Mar 17 PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
May 28 PHP
PHP检查网站是否宕机的方法示例
Jul 24 PHP
PHP使用mysqli同时执行多条sql查询语句的实例
Mar 22 PHP
PHP框架实现WebSocket在线聊天通讯系统
Nov 21 PHP
php ActiveMQ的安装与使用方法图文教程
Feb 23 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
php创建多级目录代码
2008/06/05 PHP
实用的PHP带公钥加密类分享(每次加密结果都不一样哦)
2014/08/20 PHP
PHP使用MPDF类生成PDF的方法
2015/12/08 PHP
yii2使用ajax返回json的实现方法
2016/05/14 PHP
Yii2实现自定义独立验证器的方法
2017/05/05 PHP
PHP实现对xml的增删改查操作案例分析
2017/05/19 PHP
Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
2009/08/09 Javascript
JavaScript 学习笔记(四)
2009/12/31 Javascript
web前端开发也需要日志
2010/12/09 Javascript
根据经纬度计算地球上两点之间的距离js实现代码
2013/03/05 Javascript
js拼接html注意问题示例探讨
2014/07/14 Javascript
使用JavaScript获取地址栏参数的方法
2014/12/19 Javascript
jQuery制作简洁的多级联动Select下拉框
2014/12/23 Javascript
跟我学习javascript的this关键字
2020/05/28 Javascript
利用原生js和jQuery实现单选框的勾选和取消操作的方法
2016/09/04 Javascript
详解用JS添加和删除class类名
2019/03/25 Javascript
微信小程序实现同一页面取值的方法分析
2019/04/30 Javascript
[54:41]2018DOTA2亚洲邀请赛3月30日 小组赛B组 VGJ.T VS paiN
2018/03/31 DOTA
[01:00:14]DOTA2官方TI8总决赛纪录片 真视界True Sight
2019/01/16 DOTA
python实现查询苹果手机维修进度
2015/03/16 Python
Python scikit-learn 做线性回归的示例代码
2017/11/01 Python
python使用knn实现特征向量分类
2018/12/26 Python
对Python实现累加函数的方法详解
2019/01/23 Python
python+mysql实现学生信息查询系统
2019/02/21 Python
Python发送邮件的实例代码讲解
2019/10/16 Python
python 并发下载器实现方法示例
2019/11/22 Python
Python如何实现强制数据类型转换
2019/11/22 Python
Python新手如何理解循环加载模块
2020/05/29 Python
virtualenv介绍及简明教程
2020/06/23 Python
详解python命令提示符窗口下如何运行python脚本
2020/09/11 Python
美国最大的在线寄售和旧货店:Swap.com
2018/08/27 全球购物
三年级语文教学反思
2014/02/01 职场文书
竞选班委演讲稿
2014/04/28 职场文书
2016年圣诞节活动总结范文
2016/04/01 职场文书
在js中修改html body的样式
2021/11/11 Javascript
Win11 引入 Windows 365 云操作系统,适应疫情期间混合办公模式:启动时直接登录、模
2022/04/06 数码科技