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编程效率 引入缓存机制提升性能
Feb 15 PHP
PHP中Fatal error session_start()错误解决步骤
Aug 05 PHP
ThinkPHP分页实例
Oct 15 PHP
php检查是否是ajax请求的方法
Apr 16 PHP
ThinkPHP实现递归无级分类――代码少
Jul 29 PHP
Linux下从零开始安装配置Nginx服务器+PHP开发环境
Dec 21 PHP
PHP与Java对比学习日期时间函数
Jul 03 PHP
老生常谈PHP面向对象之命令模式(必看篇)
May 24 PHP
PHP简单获取上月、本月、近15天、近30天的方法示例
Jul 03 PHP
Laravel框架实现redis集群的方法分析
Sep 14 PHP
浅谈PHP array_search 和 in_array 函数效率问题
Oct 15 PHP
搭建PhpStorm+PhpStudy开发环境的超详细教程
Sep 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
解析左右值无限分类的实现算法
2013/06/20 PHP
PHP中开启gzip压缩的2种方法
2015/01/31 PHP
PHP实现的简单对称加密与解密方法实例小结
2017/08/28 PHP
PHP7内核之Reference详解
2019/03/14 PHP
PHP中abstract(抽象)、final(最终)和static(静态)原理与用法详解
2020/06/05 PHP
从URL中提取参数与将对象转换为URL查询参数的实现代码
2012/01/12 Javascript
基于jQuery的遍历同id元素 并响应事件的代码
2012/06/14 Javascript
js动态删除div元素基本思路及实现代码
2014/05/08 Javascript
javascript实现类似超链接的效果
2014/12/26 Javascript
使用JQuery实现Ctrl+Enter提交表单的方法
2015/10/22 Javascript
浅谈jQuery双事件多重加载的问题
2016/10/05 Javascript
domReady的实现案例
2016/11/23 Javascript
ES6中参数的默认值语法介绍
2017/05/03 Javascript
详解vue-cli快速构建项目以及引入bootstrap、jq
2017/05/26 Javascript
js基于FileSaver.js 浏览器导出Excel文件的示例
2017/08/15 Javascript
layui table 参数设置方法
2018/08/14 Javascript
jQuery实现的简单手风琴效果示例
2018/08/29 jQuery
Vue实现一个无限加载列表功能
2018/11/13 Javascript
JQuery通过键盘控制键盘按下与松开触发事件
2020/08/07 jQuery
[01:33:25]DOTA2-DPC中国联赛 正赛 Elephant vs IG BO3 第一场 1月24日
2021/03/11 DOTA
浅谈Python2获取中文文件名的编码问题
2018/01/09 Python
python得到qq句柄,并显示在前台的方法
2018/10/14 Python
python装饰器常见使用方法分析
2019/06/26 Python
python基于pygame实现飞机大作战小游戏
2020/11/19 Python
绝对经典成功的大学生推荐信
2013/11/08 职场文书
护理学毕业生求职信
2013/11/14 职场文书
体育老师的教学自我评价分享
2013/11/19 职场文书
实习教师自我鉴定
2013/12/09 职场文书
高三毕业典礼演讲稿
2014/05/13 职场文书
服装设计师求职信
2014/06/04 职场文书
小学生美德少年事迹材料
2014/08/24 职场文书
自荐信格式模板
2015/03/27 职场文书
给校长的建议书作文300字
2015/09/14 职场文书
详解Nginx 被动检查服务器的存活状态
2021/10/16 Servers
如何解决goland,idea全局搜索快捷键失效问题
2022/04/03 Golang
Java详细解析==和equals的区别
2022/04/07 Java/Android