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

首頁 > 開發 > PHP > 正文

php實現在限定區域里自動調整字體大小的類實例

2024-05-04 23:33:49
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現在限定區域里自動調整字體大小的類,實例分析了php操作圖片及字體的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php實現在限定區域里自動調整字體大小的類。分享給大家供大家參考。具體如下:

這里的php類imagefittext.class.php實現在限定的區域里自動調整字體大小的功能。

 

 
  1. <?php 
  2. // Image Fit Text Class 0.1 by ming0070913 
  3. CLASS ImageFitText{ 
  4. public $font$fontsize$width$height
  5. public $step_wrap$step_fontsize
  6. public function __construct($font$step_wrap=1, $step_fontsize=1){ 
  7. $this->font = $font
  8. $this->step_wrap = $step_wrap>1?$step_wrap:1; 
  9. $this->step_fontsize = $step_fontsize>1?$step_fontsize:1; 
  10. function fit($width$height$text$fontsize$min_fontsize=5, $min_wraplength=0){ 
  11. $this->fontsize = & $fontsize
  12. $text_ = $text
  13. while($this->TextHeight($text_)>$height && $fontsize>$min_fontsize
  14. $fontsize -= $this->step_fontsize; 
  15. while(($this->TextWidth($text_)>$width || $this->TextHeight($text_)>$height) && $fontsize>$min_fontsize){ 
  16. $fontsize -= $this->step_fontsize; 
  17. $wraplength = $this->maxLen($text); 
  18. $text_ = $text
  19. while($this->TextWidth($text_)>$width && $wraplength>=$min_wraplength+$this->step_wrap){ 
  20. $wraplength -= $this->step_wrap; 
  21. $text_ = wordwrap($text$wraplength"/n", true); 
  22. //To speed up: 
  23. if($this->TextHeight($text_)>$heightbreak
  24. if($wraplength<=$min_wraplengthbreak
  25. $wraplength_ = $wraplength
  26. $wraplength = ceil($wraplength/($this->TextWidth($text_)/$width)); 
  27. $wraplength = $wraplength<($min_wraplength+$this->step_wrap)?($min_wraplength+$this->step_wrap):$wraplength
  28. $this->width = $this->TextWidth($text_); 
  29. $this->height = $this->TextHeight($text_); 
  30. return array("fontsize"=>$fontsize"text"=>$text_"width"=>$this->width, "height"=>$this->height); 
  31. function maxLen($text){ 
  32. $lines = explode("/n"str_replace("/r"""$text)); 
  33. foreach($lines as $line
  34. $t[] = strlen($line); 
  35. return max($t); 
  36. function TextWidth($text){ 
  37. $t = imagettfbbox($this->fontsize, 0, $this->font, $text); 
  38. return $t[2]-$t[0]; 
  39. function TextHeight($text){ 
  40. $t = imagettfbbox($this->fontsize, 0, $this->font, $text); 
  41. return $t[1]-$t[7]; 
  42. ?> 

使用范例如下:

 

 
  1. <?php 
  2. // Image Fit Text Class 0.1 by ming0070913 
  3. // Example File 
  4. include "imagefittext.class.php"
  5. // Settings : 
  6. // The text 
  7. $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."
  8. // The maximun width 
  9. $width = 200; 
  10. // The maximun height 
  11. $height = 100; 
  12. // Position of the text and the box 
  13. $x1 = 50; 
  14. $y1 = 50; 
  15. // The starting font size 
  16. $fontsize = 10; 
  17. // The minimun font size. The script will stop if it cannot fit the text even with this size. 
  18. $min_fontsize = 3; 
  19. // 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. 
  20. $min_wraplength = 0; 
  21. // The font 
  22. $font = "arial.ttf"
  23. // The space between the box and the text. It's independent to the script which can be ignored 
  24. $padding = 3; 
  25. // If the script cannot fit the text for certain wrap length, it will try the wrap length again with the reduction in this value. 
  26. // It reduce the accuracy, but will slightly speed up the process. 
  27. $step_wrap = 1; 
  28. // 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. 
  29. // It reduce the accuracy, but will slightly speed up the process. 
  30. $step_fontsize = 1; 
  31. // Create a image 
  32. $im = @imagecreatetruecolor($width+$x1*2, $height+$y1*2+80) or die('Cannot Initialize new GD image stream'); 
  33. // Start the timer 
  34. $time_start = microtime_float(); 
  35. // The class 
  36. $imagefittext = new ImageFitText($font$step_wrap$step_fontsize); 
  37. // Fit the text 
  38. // It returns the result in a array with "fontsize", "text", "width", "height" 
  39. $fit = $imagefittext->fit($width-$padding*2, $height-$padding*2, $text$fontsize$min_fontsize$min_wraplength); 
  40. // Stop the timer 
  41. $time = round(microtime_float()-$time_start, 3); 
  42. $white = imagecolorallocate($im, 255, 255, 255); 
  43. // Draw a box 
  44. imagerectangle($im$x1$y1$x1+$width$y1+$height$white); 
  45. // Write the text +8 because the text will move up originally 
  46. imagettftext($im$fit['fontsize'], 0, $x1+$padding$y1+$padding+8, $white$font$fit['text']); 
  47. // Print some info. about the text 
  48. imagestring($im, 5, $x1$y1+$height+30, 'Fontsize : '.$fit['fontsize'], $white); 
  49. imagestring($im, 5, $x1$y1+$height+45, 'Text Size : '.$fit['width']."x".$fit['height'], $white); 
  50. imagestring($im, 5, $x1$y1+$height+60, 'Box Size : '.($width-$padding*2)."x".($height-$padding*2), $white); 
  51. imagestring($im, 5, $x1$y1+$height+75, 'Time used : '.$time.'s'$white); 
  52. // Print the image 
  53. header ('Content-Type: image/png'); 
  54. imagepng($im); 
  55. imagedestroy($im); 
  56. function microtime_float(){ // Timer 
  57. list($usec$sec) = explode(" ", microtime()); 
  58. return ((float)$usec + (float)$sec); 
  59. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安县| 临漳县| 沂南县| 隆昌县| 姜堰市| 岳普湖县| 北安市| 闽清县| 罗江县| 多伦县| 五常市| 惠安县| 新泰市| 保德县| 秦安县| 武邑县| 六安市| 杭锦后旗| 辽中县| 荣成市| 凯里市| 宿松县| 叙永县| 三台县| 峨眉山市| 瓦房店市| 凤城市| 彰化市| 山丹县| 永福县| 筠连县| 鲁山县| 柳河县| 天柱县| 周宁县| 永泰县| 杨浦区| 樟树市| 呼玛县| 莒南县| 湖南省|