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

规定区域内字体大小自适应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 = $wraplengthstep_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 = p_w_picpathttfbbox($this->fontsize, 0, $this->font, $text);   return $t[2]-$t[0];  }  function TextHeight($text){   $t = p_w_picpathttfbbox($this->fontsize, 0, $this->font, $text);   return $t[1]-$t[7];  } } ?>

代码2:

// 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 p_w_picpath $im = @p_w_picpathcreatetruecolor($width+$x1*2, $height+$y1*2+80) or die('Cannot Initialize new GD p_w_picpath stream'); // Start the timer $time_start = microtime_float();

//CODEGO.NET The class $p_w_picpathfittext = new ImageFitText($font, $step_wrap, $step_fontsize); // Fit the text // It returns the result in a array with "fontsize", "text", "width", "height" $fit = $p_w_picpathfittext->fit($width-$padding*2, $height-$padding*2, $text, $fontsize, $min_fontsize, $min_wraplength); // Stop the timer $time = round(microtime_float()-$time_start, 3); $white = p_w_picpathcolorallocate($im, 255, 255, 255); // Draw a box p_w_picpathrectangle($im, $x1, $y1, $x1+$width, $y1+$height, $white); // Write the text            +8 because the text will move up originally p_w_picpathttftext($im, $fit['fontsize'], 0, $x1+$padding, $y1+$padding+8, $white, $font, $fit['text']); // Print some info. about the text p_w_picpathstring($im, 5, $x1, $y1+$height+30, 'Fontsize : '.$fit['fontsize'], $white); p_w_picpathstring($im, 5, $x1, $y1+$height+45, 'Text Size : '.$fit['width']."x".$fit['height'], $white); p_w_picpathstring($im, 5, $x1, $y1+$height+60, 'Box Size : '.($width-$padding*2)."x".($height-$padding*2), $white); p_w_picpathstring($im, 5, $x1, $y1+$height+75, 'Time used : '.$time.'s', $white); // Print the p_w_picpath header ('Content-Type: p_w_picpath/png'); p_w_picpathpng($im); p_w_picpathdestroy($im); function microtime_float(){ // Timer  list($usec, $sec) = explode(" ", microtime());  return ((float)$usec + (float)$sec); } ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值