规定区域内字体大小自适应php代码
admin
2023-06-28 15:03:25
0

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

代码1 :

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 = 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(); 

//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); } ?> 


相关内容

热门资讯

免去南开大学陈某院长、中山大学... 刚刚,南开大学和中山大学发布情况通报。南开大学:免去陈某院长职务南开大学通报中指出,论文第一作者郑某...
绿色算力全栈AI平台在呼和浩特...   新华社呼和浩特5月30日电(记者侯维轶)30日,绿色算力全栈AI平台——内蒙古词元交易平台在内蒙...
北京太空智算研究院在北京亦庄成... 红星资本局5月30日消息,近日,北京太空智算研究院在北京经济技术开发区(简称“北京经开区”,又称“北...
第二届西部医学科技创新学术大会... 封面新闻记者 邱添 2026年5月30日,在第十个全国科技工作者日来临之际,一场汇聚医学前沿智慧与创...
地博光电取得存储自动化料库专利... 国家知识产权局信息显示,昆山地博光电材料有限公司取得一项名为“一种存储自动化料库”的专利,授权公告号...
香港政务司司长谈黎家盈:香港由... 据凤凰卫视报道,首位来自香港的载荷专家黎家盈早前随神舟二十三号载人飞船升空。香港政务司司长陈国基表示...
亚美尼亚大选在即,俄方突然召回... 【文/观察者网 阮佳琪】当地时间周五(5月29日),在哈萨克斯坦首都阿斯塔纳,亚美尼亚由副总理格里戈...
财政部在香港发行60亿元人民币... 新华社北京5月30日电 《中国证券报》30日刊发文章《财政部在香港发行60亿元人民币绿色主权债券》。...
原创 追... 2026年5月30日是全国科技工作者日。在吉林省长春市王大珩故居前,这位中国光学事业奠基人的新雕像揭...
京东618独家首发欧姆龙血糖尿... 当前,国民居家慢病管理意识持续增强,血糖、尿酸等健康指标的常态化监测,早已成为家庭健康管理的刚需。5...