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 图片水印类代码
Aug 27 PHP
解析PHP提交后跳转
Jun 23 PHP
php中session使用示例
Mar 29 PHP
PHP中exec与system用法区别分析
Sep 22 PHP
配置Nginx+PHP的正确思路与过程
May 10 PHP
PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例
May 17 PHP
解决php extension 加载顺序问题
Aug 16 PHP
Laravel + Elasticsearch 实现中文搜索的方法
Feb 02 PHP
Thinkphp 框架基础之入口文件功能、定义与用法分析
Apr 27 PHP
PHP执行linux命令6个函数代码实例
Nov 24 PHP
Laravel中Kafka的使用详解
Mar 24 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
Cappuccino 卡布其诺咖啡之制作
2021/03/03 冲泡冲煮
php 一元分词算法
2009/11/30 PHP
PHP操作Memcache实例介绍
2013/06/14 PHP
php实现汉字验证码和算式验证码的方法
2015/03/07 PHP
PHP多态代码实例
2015/06/26 PHP
JavaScript 检测浏览器和操作系统的脚本
2008/12/26 Javascript
JQuery 网站换肤功能实现代码
2009/11/02 Javascript
jquery 打开窗口返回值实现代码
2010/03/04 Javascript
Extjs中ComboBox加载并赋初值的实现方法
2012/03/22 Javascript
JavaScript判断表单为空及获取焦点的方法
2016/02/12 Javascript
JS定时检测任务任务完成后执行下一步的解决办法
2016/12/22 Javascript
angular6.0使用教程之父组件通过url传递id给子组件的方法
2018/06/30 Javascript
对angularJs中$sce服务安全显示html文本的实例
2018/09/30 Javascript
vue style width a href动态拼接问题的解决
2020/08/07 Javascript
vue-cli4.0多环境配置变量与模式详解
2020/12/30 Vue.js
python实现根据月份和日期得到星座的方法
2015/03/27 Python
Python下载懒人图库JavaScript特效
2015/05/28 Python
读取本地json文件,解析json(实例讲解)
2017/12/06 Python
浅谈Django自定义模板标签template_tags的用处
2017/12/20 Python
python使用jieba实现中文分词去停用词方法示例
2018/03/11 Python
python根据txt文本批量创建文件夹
2020/12/08 Python
Django集成搜索引擎Elasticserach的方法示例
2019/06/04 Python
Django后台admin的使用详解
2019/07/08 Python
python 命令行传入参数实现解析
2019/08/30 Python
Django连接数据库并实现读写分离过程解析
2019/11/13 Python
Python列表操作方法详解
2020/02/09 Python
Python任务调度利器之APScheduler详解
2020/04/02 Python
解决Windows下python和pip命令无法使用的问题
2020/08/31 Python
美国优质宠物用品购买网站:Muttropolis
2020/02/17 全球购物
初中三年学生的学习自我评价
2013/11/13 职场文书
大学旷课检讨书
2014/01/28 职场文书
公司授权委托书范本
2014/09/18 职场文书
作文评语集锦
2014/12/25 职场文书
中班上学期个人总结
2015/02/12 职场文书
如何制定一份可行的计划!
2019/06/21 职场文书
mysql使用 not int 子查询隐含陷阱
2022/04/12 MySQL