php实现在限定区域里自动调整字体大小的类实例_php技巧

本文实例讲述了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
, 字体大小
, 类
, 自动调整
限定区域
少女前线打捞限定技巧、lol生日半价限定技巧、口岸限定区域、区域的空间限定、标志安全区域限定,以便于您获取更多的相关知识。

时间: 2024-09-18 12:46:59

php实现在限定区域里自动调整字体大小的类实例_php技巧的相关文章

NPOI里XWPFTable字体大小设置

问题描述 NPOI里XWPFTable字体大小如何一起设置,源码如下:XWPFDocumentworkbook=OpenWordTemplate(TemplateNamePath);XWPFTablelist=workbook.Tables[0];XWPFTableCellcell=list.GetRow(0).GetCell(1);直接设置cell.SetTest里的值,字体比原来Word里的大,自定义后XWPFParagraphpIO=cell.AddParagraph();XWPFRunr

Android 自定义TextView实现文本内容自动调整字体大小

最近做通讯录小屏机 联系人姓名显示--长度超过边界字体变小 /** * 自定义TextView,文本内容自动调整字体大小以适应TextView的大小 * @author yzp */ public class AutoFitTextView extends TextView { private Paint mTextPaint; private float mTextSize; public AutoFitTextView(Context context) { super(context); }

Excel 单元格如何自动调整字体大小?

  在使用Excel的过程中,我们往往先把表格根据要求做好,这时单元格的列宽已经固定好了,但有的列字符数目不等.比如在录入家庭住址时,有的家庭住址比较长,则会使该单元格中的内容不能完全显示在屏幕上,为了让这些单元格中的内容能全部显示在屏幕上,就不得不重新定义单元格的字号.如果一个一个的调整势必会大大增加我们的工作量,其实,我们可以采用下面的方法让其自动调整字号大小: 选中需要调整字号的单元格区域,依次选择"格式→单元格"菜单命令,出现"单元格格式"对话框,选择&qu

android根据分辨率自动调整字体大小的实例代码_Android

手机设备太多,分辨率也不一样,看到网上大部分的适应字体的方法是定义values320×480或value-hdpi方式去处理.采用第一种的就惨了,很多设备的分辨率是不一样的,难道要每种都定义吗?采用第二种的在平板电脑里没有效果. 最后还是代码的方式方便快捷... [java] 复制代码 代码如下: //遍历设置字体  public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {/

android根据分辨率自动调整字体大小的实例代码

手机设备太多,分辨率也不一样,看到网上大部分的适应字体的方法是定义values320×480或value-hdpi方式去处理.采用第一种的就惨了,很多设备的分辨率是不一样的,难道要每种都定义吗?采用第二种的在平板电脑里没有效果. 最后还是代码的方式方便快捷... [java]复制代码 代码如下://遍历设置字体  public static void changeViewSize(ViewGroup viewGroup,int screenWidth,int screenHeight) {//传

JavaScript 控制字体大小设置的方法_javascript技巧

在做公司的官网的时候,新闻内页会有一个让浏览者自己调整文字大小的功能,因此在这个空闲时间,把这个功能整理下来: function setFontSize (id,content,params){ var oTarget = document.getElementById(id), content = document.getElementById(content), size = params.size || 14, maxSize = params.maxSize || 20, step =

javascript 改变字体大小方法集合[原创]_javascript技巧

字体: 小中大 www.jb51.net 欢迎大家的光临.

php插入中文到sqlserver 2008里出现乱码的解决办法分享_php技巧

今天使用php操作数据库时发现插入SQL Server 2008数据库里的中文字段出现乱码,下面是我一开始时的一些情况: 开发环境是php5.3.3+Apache2.2.17+SQL Server 2008,php脚本文件的编码是utf-8,传给数据库的编码是GB2312(SQL Server的默认字符编码可能是这个,我不肯定),我用的是微软官方提供的SQLSRV库来连接数据库的(PS:SQL Server 2005开始已经不支持用mssql.dll来连接了),故使用sqlsrv_query($

php获取网页里所有图片并存入数组的方法_php技巧

本文实例讲述了php获取网页里所有图片并存入数组的方法.分享给大家供大家参考.具体如下: $images = array(); preg_match_all('/(img|src)=("|')[^"'>]+/i', $data, $media); unset($data); $data=preg_replace('/(img|src)("|'|="|=')(.*)/i',"$3",$media[0]); foreach($data as $