• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

PHP实现在图片中添加中文文字

发布: 2007-7-01 15:06 | 作者: admin | 来源: | 查看: 65次 | 进入软件测试论坛讨论

领测软件测试网 <?php
/*
注意:需要gd库支持,需要iconv支持(php5已经包含不用加载)
*version 1.0
sanshi(叁石)
*QQ: 35047205
*MSN:
*Create 2005/10/20
在图片中添加中文文字
*/
 /*
  param $image   图象资源
  param size     字体大小
  param angle    字体输出角度
  param showX    输出位置x坐标
  param showY    输出位置y坐标
  param font    字体文件位置
  param content  要在图片里显示的内容
 */
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;
  //exit();
  $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);
  //ImageString($image,5,50,10,$txt,$white);
  //显示图形
  imagejpeg($image);
  ImageDestroy($image);
 }
}
//本类,并没有经过很好的考虑,只是简单的进行了封装,以后有机会,可能跟原来的图片类整合
?>
<?php
//使用示例
$s = new showChinaText();
?>

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网