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 相关文章推荐
php2html php生成静态页函数
Dec 08 PHP
PHP数组对比函数,存在交集则返回真,否则返回假
Feb 03 PHP
Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL报错解决方法
Jun 30 PHP
PHP实现对文本数据库的常用操作方法实例演示
Jul 04 PHP
php中ltrim()、rtrim()与trim()删除字符空格实例
Nov 25 PHP
php递归调用删除数组空值元素的方法
Apr 28 PHP
PHP获取音频文件的相关信息
Jun 22 PHP
Yii操作数据库实现动态获取表名的方法
Mar 29 PHP
Thinkphp连表查询及数据导出方法示例
Oct 15 PHP
PHP实现搜索时记住状态的方法示例
May 11 PHP
Laravel 5.4前后台分离,通过不同的二级域名访问方法
Oct 13 PHP
PHP实用小技巧之调用录像的方法
Dec 05 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 GD 图像处理组件的常用函数总结
2010/04/28 PHP
php将csv文件导入到mysql数据库的方法
2014/12/24 PHP
YII Framework框架教程之缓存用法详解
2016/03/14 PHP
php微信公众号开发之简答题
2018/10/20 PHP
jqgrid 编辑添加功能详细解析
2013/11/08 Javascript
js和css写一个可以自动隐藏的悬浮框
2014/03/05 Javascript
javascript中Math.random()使用详解
2015/04/15 Javascript
使用bat打开多个cmd窗口执行gulp、node
2017/02/17 Javascript
three.js中文文档学习之创建场景
2017/11/20 Javascript
微信小程序实现图片上传、删除和预览功能的方法
2017/12/18 Javascript
ES6 迭代器(Iterator)和 for.of循环使用方法学习(总结)
2018/02/08 Javascript
用Axios Element实现全局的请求loading的方法
2018/03/15 Javascript
使用 Vue 实现一个虚拟列表的方法
2019/08/20 Javascript
vue路由传参页面刷新参数丢失问题解决方案
2019/10/08 Javascript
Windows上node.js的多版本管理工具用法实例分析
2019/11/06 Javascript
JS实现简易计算器
2020/02/14 Javascript
微信小程序学习总结(四)事件与冒泡实例分析
2020/06/04 Javascript
JS自定义右键菜单实现代码解析
2020/07/16 Javascript
如何构建 vue-ssr 项目的方法步骤
2020/08/04 Javascript
学习 Vue.js 遇到的那些坑
2021/02/02 Vue.js
实例讲解Python中的私有属性
2014/08/21 Python
python分析作业提交情况
2017/11/22 Python
详谈python3 numpy-loadtxt的编码问题
2018/04/29 Python
对python中for、if、while的区别与比较方法
2018/06/25 Python
在PyCharm中实现关闭一个死循环程序的方法
2018/11/29 Python
解决Jupyter无法导入已安装的 module问题
2020/04/17 Python
用Python制作mini翻译器的实现示例
2020/08/17 Python
英国休闲奢华的缩影:Crew Clothing
2019/05/05 全球购物
土木工程专业自荐信
2013/10/04 职场文书
优秀班主任经验交流材料
2014/06/02 职场文书
农民工预备党员思想汇报
2014/09/14 职场文书
工商管理专业毕业生自我鉴定2014
2014/10/04 职场文书
市级三好学生评语
2014/12/29 职场文书
百万英镑观后感
2015/06/09 职场文书
2016年区委书记抓基层党建工作公开承诺书
2016/03/25 职场文书
nginx配置文件使用环境变量的操作方法
2021/06/02 Servers