/*
*
* @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() );
本文介绍了一个使用PHP进行图像处理的方法,包括如何加载图像文件、设置字体、定义文字内容及编码,以及如何设置颜色并最终在图像上绘制文字。

被折叠的 条评论
为什么被折叠?



