国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

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

2020-03-22 18:34:41
字體:
供稿:網(wǎng)友
這篇文章主要介紹了php實現(xiàn)在限定區(qū)域里自動調(diào)整字體大小的類,實例分析了php操作圖片及字體的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

 ?php// Image Fit Text Class 0.1 by ming0070913CLASS ImageFitText{ html' target='_blank'>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 Fileinclude 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 boximagerectangle($im, $x1, $y1, $x1+$width, $y1+$height, $white);// Write the text +8 because the text will move up originallyimagettftext($im, $fit[ fontsize ], 0, $x1+$padding, $y1+$padding+8, $white, $font, $fit[ text // Print some info. about the textimagestring($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 imageheader ( Content-Type: image/png imagepng($im);imagedestroy($im);function microtime_float(){ // Timer list($usec, $sec) = explode( , microtime()); return ((float)$usec + (float)$sec);? 

總結(jié):以上就是本篇文的全部內(nèi)容,希望能對大家的學習有所幫助。

相關(guān)推薦:

php實現(xiàn)新聞發(fā)布系統(tǒng)

PHP讀取配置文件類實例

php生成縮略圖的方法

以上就是php實現(xiàn)在限定的區(qū)域里自動調(diào)整字體大小的功能的詳細內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 来安县| 镶黄旗| 饶河县| 梓潼县| 阳山县| 苗栗市| 辽中县| 海盐县| 龙井市| 济宁市| 兰西县| 漾濞| 南木林县| 洪雅县| 霍林郭勒市| 买车| 石城县| 宣化县| 始兴县| 定西市| 简阳市| 沙洋县| 汉源县| 云霄县| 上杭县| 福州市| 鹤壁市| 郎溪县| 马边| 内江市| 宁晋县| 吉首市| 东港市| 东平县| 宁海县| 正安县| 北辰区| 通渭县| 会同县| 长岭县| 澎湖县|