封装了个在图片上写文字的程序

 

/* *
 * @author injection(injection.mail@gmail.com)
 * @package ImageContext.class.php
 * @var object mImages 图片相关的数据 
 * @descript 支持中文文字,但必须保证你的php环境支持iconv函数
 
*/  
class  drawImg{
 
var   $mImages ;

 
/* *
  * 设定本页面类型为图片类型
  
*/
 
function  __construct(){
  
header ( " content-type:image/gif " );
 }
 
 
/* *
  * 设置图片源
  *
  * @param string $filename
  * @example 'c://mypic/mypic.gif' or 'mypic.gif'
  
*/
 
function  setImage(  $filename  ){
  
$this -> mImages -> filename  =  imagecreatefromgif(  $filename  );
 }

 
/* *
  * 设置写入文字的字体
  *
  * @param string $font
  * @example simhei.ttf
  
*/
 
function  setFont(  $font  ){
  
$this -> mImages -> font  =   $font ;
 }

 
 
/* *
  * 设置写入的文字
  *
  * @param string $context
  * @example test
  
*/
 
function  setContext(  $context  ){
  
$this -> mImages -> context  =   $context ;
 }
 
 
/* *
  * 设置文字编码
  *
  * @param string $from_charset
  * @param string $to_charset
  
*/
 
function  setCharset(  $from_charset , $to_charset  ){
  
$this -> mImages -> context  =   iconv $from_charset ,   $to_charset   ,   $this -> mImages -> context );
 }

 
/* *
  * 设置写入黑白的具体数值
  *
  * @param string $white
  * @example 255,255,255
  * @param string $black
  * @example 0,0,0
  
*/
 
function  setColor(  $white , $black  ){
  
$this -> mImages -> white  =  imagecolorallocate(  $this -> mImages -> filename , $white  );
  
$this -> mImages -> black  =  imagecolorallocate(  $this -> mImages -> filename , $black  );
 }
 
 
/* *
  * 根据参数类型获取mImages对象属性或对象的信息
  *
  * @param enum 参数列表:filename,font,context,all 
  * @return 一个mImages的一个属性或mImages这个对象
  
*/
 
function  getImageInfo(  $type = " all "  ){
  
if $type   !=   " all " )
   
return   $this -> mImages -> $type ;
  
else
   
return   $this -> mImages;
 }

 
 
/* *
  * 将文字写入图片
  * 
  * @param int $size 字体大小
  * @param int $angle 起始角度
  * @param int $x 第一个字符的基本点(大概是字符的左下角)
  * @param int $y Y坐标。它设定了字体基线的位置,不是字符的最底端
  * @param enum $  值为white或black
  
*/
 
function  draw(  $size , $angle , $x , $y , $color  ){
  imagettftext( 
$this -> mImages -> filename , $size , $angle , $x , $y , $this -> mImages -> $color , $this -> mImages -> font , $this -> mImages -> context );
  imagegif( 
$this -> mImages -> filename );
 }

 
/* *
  * 释放图片源
  
*/
 
function  unsetImage(){
  imagedestroy( 
$this -> mImages -> filename );
 }

 
/* *
  * 释放本对象
  
*/
 
function  __destruct(){
  
unset $this  );
 }
}

 

/*  sample  */
$image   =   new  drawImg();
$image -> setImage(  ' mypic.gif '  );
$image -> setFont(  ' simhei.ttf '  );
$image -> setContext(  ' 我顶你个肺! '  );
$image -> setCharset( ' gb2312 ' , ' UTF-8 ' );
@
$image -> setColor( ' 255,255,255 ' , ' 0,0,0, ' );
$image -> draw(  20 , 0 , 0 , 20 , ' black '  );

// var_dump( $image->getImageInfo() ); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值