Font size Adaptive PHP code in the specified area

Source: Internet
Author: User

Font size Adaptive PHP code in the specified area

Code Listing 1:

<?php//Image Fit Text class 0.1 by ming0070913 class imagefittext{ public $font, $fontsize, $width, $height; &n Bsp;public $step _wrap, $step _fontsize;  public function __construct ($font, $step _wrap=1, $step _fontsize=1) {  $this->font = $font;   $this-& Gt;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-&Gt TextWidth ($text _) > $width && $wraplength >= $min _wraplength+ $this->step_wrap) {    $ Wraplength-= $this->step_wrap;     $text _ = WordWrap ($text, $wraplength, "\ n", true);    //TO:     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);   R Eturn $t [2]-$t [0];  }  function TextHeight ($text) {  $t = Imagettfbbox ($this->fontsize, 0, $this->font, $text);   return $t [1]-$t [7];  }}?> 


Code Listing 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's especially suited for W EB development and can is embedded into HTML. If you is new to PHP and want to get some idea of how it works, try the introductory tutorial. After this, check out the online manual. "; The maximun width $width = 200;

The maximun height $height = 100;

Position of the text and the box $x 1 = 50; $y 1 = 50;

The starting font size $fontsize = 10;

The Minimun font size. The script would stop if it cannot fit the text even with this size. $min _fontsize = 3;

The Minimun wrap length for each line. The script would 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 ignored $padding = 3;

If the script cannot fit the text for certain wrap length, it'll try the wrap length again with the reduction in this Value. It reduce the accuracy, but would slightly speed up the process. $step _wrap = 1; If the script cannot fit the text for certain font size, it'll try the the the font size again with the reduction in this Value.

It reduce the accuracy, but would slightly speed up the process. $step _fontsize = 1;

//Create a image $im = @imagecreatetruecolor ($width + $x 1*2, $height + $y 1*2+80) or Die (' cannot Initialize new GD image St Ream '); 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 an array with "FontSize", "Text", "width", "height" $fit = $imagefittext->fi T ($width-$padding, $height-$padding * *, $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, $x 1, $y 1, $x 1+ $width, $y 1+ $height, $white); Write the text           &NBSP;+8 because the text would move up originally Imagettftext ($im, $ fit[' FontSize ', 0, $x 1+ $padding, $y 1+ $padding +8, $white, $font, $fit [' text ']); Print some info. About the text imagestring ($im, 5, $x 1, $y 1+ $height +30, ' Fontsize: '. $fit [' Fontsize '], $white); Imagestring ($im, 5, $x 1, $y 1+ $height +45, ' Text Size: '. $fit [' width ']. " X ". $fit [' height '], $white); Imagestring ($im, 5, $x 1, $y 1+ $height +60, ' Box Size: '. ( $width-$padding). " X ". ($height-$padding), $white); Imagestring ($im, 5, $x 1, $y 1+ $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); }?> 


This article is from the "kfdsia115" blog, make sure to keep this source http://10078237.blog.51cto.com/10068237/1627882

Font size Adaptive PHP code in the specified area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.