欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)在限定區(qū)域里自動調(diào)整字體大小的類實(shí)例

 更新時間:2015年04月02日 15:46:14   作者:pythoner  
這篇文章主要介紹了php實(shí)現(xiàn)在限定區(qū)域里自動調(diào)整字體大小的類,實(shí)例分析了php操作圖片及字體的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)在限定區(qū)域里自動調(diào)整字體大小的類。分享給大家供大家參考。具體如下:

這里的php類imagefittext.class.php實(shí)現(xiàn)在限定的區(qū)域里自動調(diào)整字體大小的功能。

<?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程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP常用設(shè)計(jì)模式之委托設(shè)計(jì)模式

    PHP常用設(shè)計(jì)模式之委托設(shè)計(jì)模式

    委托是對一個類的功能進(jìn)行擴(kuò)展和復(fù)用的方法。它的做法是:寫一個附加的類提供附加的功能,并使用原來的類的實(shí)例提供原有的功能,接下來通過本文給大家介紹PHP委托設(shè)計(jì)模式實(shí)例詳解,感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • PHP使用redis消息隊(duì)列發(fā)布微博的方法示例

    PHP使用redis消息隊(duì)列發(fā)布微博的方法示例

    這篇文章主要介紹了PHP使用redis消息隊(duì)列發(fā)布微博的方法,結(jié)合具體實(shí)例形式分析了php結(jié)合redis數(shù)據(jù)庫操作消息隊(duì)列實(shí)現(xiàn)微博發(fā)布的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • PHP無敵近乎加密方式!

    PHP無敵近乎加密方式!

    最近要用到PHP,所以去網(wǎng)上找了些資料!無意中看到這篇文章,推薦給大家.
    2010-07-07
  • php通過遞歸方式復(fù)制目錄和子目錄的方法

    php通過遞歸方式復(fù)制目錄和子目錄的方法

    這篇文章主要介紹了php通過遞歸方式復(fù)制目錄和子目錄的方法,涉及php遞歸及目錄操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • PHP中將一個字符串部分字符用星號*替代隱藏的實(shí)現(xiàn)代碼

    PHP中將一個字符串部分字符用星號*替代隱藏的實(shí)現(xiàn)代碼

    這篇文章主要介紹了PHP中將一個字符串部分字符用星號*替代隱藏的實(shí)現(xiàn)代碼,有時候我們需要將部分內(nèi)容隱藏那么就可能需要下面的代碼了,需要的朋友可以參考下
    2019-09-09
  • 使用 MySQL Date/Time 類型

    使用 MySQL Date/Time 類型

    上次對于 MySQL 方面已經(jīng)有的一些總結(jié),但是昨晚 wiLdGoose 兄說他也碰到同樣的問題,但是無法解決。結(jié)果經(jīng)過種種的假設(shè)和判斷以后,到最后發(fā)現(xiàn)原來是 Zend Studio 的時區(qū)配置問題(我狂汗ing)。而在和他討論期間也談到了很多關(guān)于 MySQL 的細(xì)節(jié)問題,還是記錄一下當(dāng)作備忘比較好。這篇文章同時也做說服 wiLdGoose 兄用。
    2008-03-03
  • PHP數(shù)據(jù)過濾的方法

    PHP數(shù)據(jù)過濾的方法

    關(guān)于如何確保數(shù)據(jù)過濾無法被繞過有各種各樣的觀點(diǎn),今天我們就來看看PHP的數(shù)據(jù)過濾,對你一定會有幫助的。
    2013-10-10
  • php實(shí)現(xiàn)編輯和保存文件的方法

    php實(shí)現(xiàn)編輯和保存文件的方法

    這篇文章主要介紹了php實(shí)現(xiàn)編輯和保存文件的方法,涉及php針對文件的讀取、編輯和保存操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • php無限級分類實(shí)現(xiàn)方法分析

    php無限級分類實(shí)現(xiàn)方法分析

    這篇文章主要介紹了php無限級分類實(shí)現(xiàn)方法,結(jié)合2個簡單實(shí)例形式分析了php通過遞歸與普通算法實(shí)現(xiàn)無限級分類的相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • PHP實(shí)現(xiàn)隨機(jī)發(fā)撲克牌

    PHP實(shí)現(xiàn)隨機(jī)發(fā)撲克牌

    這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)隨機(jī)發(fā)撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04

最新評論