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 分页分组类
Dec 10 PHP
用php制作简单分页(从数据库读取记录)的方法详解
May 04 PHP
PHP中$_FILES的使用方法及注意事项说明
Feb 14 PHP
php 判断服务器操作系统的类型
Feb 17 PHP
smarty中post用法实例
Nov 28 PHP
PHP+shell实现多线程的方法
Jul 01 PHP
PHP实现QQ空间自动回复说说的方法
Dec 02 PHP
PHP函数import_request_variables()用法分析
Apr 02 PHP
Yii2.0实现生成二维码功能实例
Oct 24 PHP
Yii框架函数简单用法分析
Sep 09 PHP
laravel 5.5 关闭token的3种实现方式
Oct 24 PHP
PHP中->和=>的意思
Mar 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+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
2006/12/16 PHP
PHP 一个随机字符串生成代码
2010/05/26 PHP
PHP之浮点数计算比较以及取整数不准确的解决办法
2015/07/29 PHP
老生常谈PHP中的数据结构:DS扩展
2017/07/17 PHP
Laravel5.7 Eloquent ORM快速入门详解
2019/04/12 PHP
Laravel 登录后清空COOKIE的操作方法
2019/10/14 PHP
js对象之JS入门之Array对象操作小结
2011/01/09 Javascript
jquery easyui中treegrid用法的简单实例
2014/02/18 Javascript
JS实现选项卡实例详解
2015/11/17 Javascript
分享jQuery插件的学习笔记
2016/01/14 Javascript
bootstrap table使用入门基本用法
2017/05/24 Javascript
JavaScript实现10秒后再次获取验证码
2020/12/02 Javascript
Python编程中的for循环语句学习教程
2015/10/14 Python
Python 模拟登陆的两种实现方法
2017/08/10 Python
Python简单生成随机姓名的方法示例
2017/12/27 Python
对numpy中的transpose和swapaxes函数详解
2018/08/02 Python
python使用PIL给图片添加文字生成海报示例
2018/08/17 Python
centos7之Python3.74安装教程
2019/08/15 Python
python实现布隆过滤器及原理解析
2019/12/08 Python
python使用SQLAlchemy操作MySQL
2020/01/02 Python
使用matplotlib的pyplot模块绘图的实现示例
2020/07/12 Python
Python如何读写CSV文件
2020/08/13 Python
Python Web项目Cherrypy使用方法镜像
2020/11/05 Python
Tory Burch英国官方网站:美国时尚生活品牌
2017/12/06 全球购物
美国户外烹饪产品购物网站:Outdoor Cooking
2020/01/10 全球购物
数据库的约束含义
2012/09/09 面试题
制作部班长职位说明书
2014/02/26 职场文书
大学生暑期社会实践证明范本
2014/10/24 职场文书
学校勤俭节约倡议书
2015/04/29 职场文书
中秋联欢会主持词
2015/07/04 职场文书
治庸问责工作总结
2015/08/11 职场文书
2015年中秋放假通知范文
2015/08/18 职场文书
七年级思品教学反思
2016/02/20 职场文书
详解Java实践之建造者模式
2021/06/18 Java/Android
MySQL基础快速入门知识总结(附思维导图)
2021/09/25 MySQL
mysql事务对效率的影响分析总结
2021/10/24 MySQL