规定区域内字体大小自适应php代码

代码1 :

<?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];  } } ?>

代码2:

<?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();

//CODEGO.NET 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); } ?>

时间: 2024-10-28 14:37:45

规定区域内字体大小自适应php代码的相关文章

html页面控制字体大小的js代码

控制显示文章字体大小的js代码 1 <head> 2 3 4 <script type="text/javascript"> 5 6 function check(size){ 7 document.getElementById("mycode").style.fontSize=size+"pt"; 8 } 9 10 </script> 11 </head> 12 13 <body> 1

字体大小自适应纯css解决方案

viewpoint css3提供了一些与当前viewpoint相关的元素,vw,vh,vim等. “viewpoint” = window size vw = 1% of viewport width 1vh = 1% of viewport height 1vmin = 1vw or 1vh, 最小 1vmax = 1vw or 1vh, 最大 兼容性:chrome 20+/ safari 6+/ IE 10+ / FF 19+ / IOS 6+ DEMO地址:http://qianduann

android 字体大小自适应

  ndroid 想自适应哪个分辨率的就在你的工程res下添加values-XXXxXXX(如:values-values-1024x600)文件夹,在文件夹里新建dimens.xml文件格式如下: <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="text_size">40sp</dimen> </resource

响应式网页设计:rem设置网页字体大小自适应

首先简单科普一下什么是响应式网页.响应式网页是指会根据输出设备的分辨率不同,而自动调整布局的网页.同时,在输出设备分辨率改变时,也能及时自动调整.说穿了,就是三个字:自适应. 响应式网页不仅仅是响应不同类型的设备,而且需要响应不同的用户需求.响应式的初衷是为了让信息更好的传递交流,让所有人无障碍的获取信息,同时这也是 Web 的初衷. 当我们每天面对缤纷的互联网世界的时候,文字不仅仅传递给我们众多的信息资讯,而且在设计师的手里,文字在网页中的排版承载着一种艺术的直觉. 网页中常用的文字大小单位是

C# Winform控件字体大小自适应

using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace WGClient { class AutoSizeFormClass { //(1).声明结构,只记录窗体和其控件的初始位置和大小. public struct controlRect { public int Left; public int Top; public int Width; public int

字体大小自适应屏幕分辨率 CSS解决方案

viewpoint css3提供了一些与当前viewpoint相关的元素,vw,vh,vim等. “viewpoint” = window size vw = 1% of viewport width 1vh = 1% of viewport height 1vmin = 1vw or 1vh, 最小 1vmax = 1vw or 1vh, 最大 兼容性:chrome 20+/ safari 6+/ IE 10+ / FF 19+ / IOS 6+ <!DOCTYPE HTML> <ht

根据Label和字体大小自适应高度和宽度

- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.numberOfLines = 0; NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text]; [labelString addAttribute:NSFontAttributeName val

pc端字体大小自适应

$(window).resize(function ()// 绑定到窗口的这个事件中 {  var whdef = 100/1920;// 表示1920的设计图,使用100PX的默认值  var wH = window.innerHeight;// 当前窗口的高度  var wW = window.innerWidth;// 当前窗口的宽度  var rem = wW * whdef;// 以默认比例值乘以当前窗口宽度,得到该宽度下的相应FONT-SIZE值  $('html').css('fo

CSS font-size字体文字大小样式属性-字体大小样式篇

一.设置字体大小CSS单词与语法 - TOP基本语法结构: .divcss5{font-size:12px;}设置了文字大小为12px像素Font-size+字体大小数值+单位 单词:font-size语法:font-size : absolute-size | relative-size | length取值:xx-small | x-small | small | medium | large | x-large | xx-largexx-small:最小x-small:较小small:小m