PHP实现在图片中添加中文文字 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- class showChinaText
- {
- var $text='你好';
- var $font='fs.ttf';
- var $angle=0;
- var $size=50;
- var $showX=100;
- var $showY=100;
- function showChinaText($showText='')
- {
- $this->text=!isset($showText)?$showText:$this->text;
-
- $this->show();
- }
- function createText($instring)
- {
- $outstring="";
- $max=strlen($instring);
- for($i=0;$i<$max;$i )
- {
- $h=ord($instring[$i]);
- if($h>=160 && $i<$max-1)
- {
- $outstring.="&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr ($instring,$i,2))),16,10).";";
- $i ;
- }
- else
- {
- $outstring.=$instring[$i];
- }
- }
- return $outstring;
- }
- function createJpeg()
- {}
- function show()
- {
-
- Header( "Content-type: image/png");
-
- $image = imagecreate(400,300);
-
- $red = ImageColorAllocate($image,255,0,0);
- $white = ImageColorAllocate($image,255,255,255);
- $black=ImageColorAllocate($image,0,0,0);
-
- ImageFilledRectangle($image,0,0,200,200,$red);
-
- $txt=$this->createText($this->text);
-
- imagettftext($image,$this->size, $this->angle, $this->showX, $this->showY,$white,$this->font,$txt);
-
-
- imagejpeg($image);
- ImageDestroy($image);
- }
- }
-
- ?>
- <?php
-
- $s = new showChinaText();
- ?>
|
|
|
|