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脚本的10个技巧(4)
Oct 09 PHP
介绍几个array库的新函数 php
Dec 29 PHP
php 模拟 asp.net webFrom 按钮提交事件的思路及代码
Dec 02 PHP
php处理restful请求的路由类分享
Feb 27 PHP
自编函数解决pathinfo()函数处理中文问题
Nov 03 PHP
在CentOS上搭建LAMP+vsftpd环境的简单指南
Aug 01 PHP
php生成静态html页面的方法(2种方法)
Sep 14 PHP
php调用自己java程序的方法详解
May 13 PHP
ThinkPHP框架表单验证操作方法
Jul 19 PHP
深入理解PHP的远程多会话调试
Sep 21 PHP
php ZipArchive实现多文件打包下载实例
Oct 31 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中__FILE__、dirname与basename用法实例分析
2014/12/01 PHP
帮助避免错误的Javascript陷阱清单
2009/05/31 Javascript
文本框根据输入内容自适应高度的代码
2011/10/24 Javascript
jquery load事件(callback/data)使用方法及注意事项
2013/02/06 Javascript
你必须知道的JavaScript 中字符串连接的性能的一些问题
2013/05/07 Javascript
js菜单点击显示或隐藏效果的简单实例
2014/01/13 Javascript
JS在IE下缺少标识符的错误
2014/07/23 Javascript
javascript查询字符串参数的方法
2015/01/28 Javascript
jQuery的文档处理程序详解
2016/05/10 Javascript
AngularJS中过滤器的使用与自定义实例代码
2016/09/17 Javascript
Angular的事件和表单详解
2016/12/26 Javascript
jQuery插件FusionCharts绘制2D柱状图和折线图的组合图效果示例【附demo源码】
2017/04/10 jQuery
Bootstrap 表单验证formValidation 实现表单动态验证功能
2017/05/17 Javascript
JS原生轮播图的简单实现(推荐)
2017/07/22 Javascript
浅谈React Native Flexbox布局(小结)
2018/01/08 Javascript
Vue CLI3 如何支持less的方法示例
2018/08/29 Javascript
Vue指令v-for遍历输出JavaScript数组及json对象的常见方式小结
2019/02/11 Javascript
使用纯前端JavaScript实现Excel导入导出方法过程详解
2020/08/07 Javascript
python的re模块应用实例
2014/09/26 Python
Python中模拟enum枚举类型的5种方法分享
2014/11/22 Python
深入讲解Python函数中参数的使用及默认参数的陷阱
2016/03/13 Python
python中matplotlib的颜色及线条控制的示例
2018/03/16 Python
python筛选出两个文件中重复行的方法
2018/05/31 Python
python操作excel的方法
2018/08/16 Python
对Python3+gdal 读取tiff格式数据的实例讲解
2018/12/04 Python
python3下pygame如何实现显示中文
2020/01/11 Python
tensorflow2.0教程之Keras快速入门
2021/02/20 Python
意大利婴儿产品网上商店:Mukako
2018/10/14 全球购物
汽车运用工程毕业生自荐信
2013/10/29 职场文书
个人评价范文分享
2014/01/11 职场文书
公民代理授权委托书
2014/09/24 职场文书
2014年酒店服务员工作总结
2014/12/08 职场文书
2016年优秀教师先进事迹材料
2016/02/26 职场文书
学校团代会开幕词
2016/03/04 职场文书
pytorch实现线性回归以及多元回归
2021/04/11 Python
美国运营商 T-Mobile 以 117.83Mb/s 的速度排第一位
2022/04/21 数码科技