<?php
if
(!defined(
"__WEBROOT__"
)) define(
"__WEBROOT__"
,
$_SERVER
[
''
DOCUMENT_ROOT
''
]);
class
Img {
protected
$_Img
;
protected
$_FileImg
;
protected
$_FileInfo
;
protected
$_PicInfo
;
protected
$_Rand
=
''
abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890
''
;
protected
$_Code
=
''
''
;
public
$Width
= 300;
public
$Height
= 80;
public
$BackgroundColor
=
"000000"
;
public
$Font
=
"/phps/Public/Font/ARIALNB.TTF"
;
public
$FontSize
= 16;
public
$FontColor
=
"ffffff"
;
public
$Content
=
"Test Word"
;
public
$Align
=
"left"
;
public
$Codes
= 4;
public
$Line
= 6;
public
$LoadErr
=
''
''
;
public
function
Style(
$Options
){
$this
-> _Re_Set();
foreach
(
$Options
as
$K
=>
$V
){
if
(in_array(
$K
,
array
(
''
Width
''
,
''
Height
''
,
''
BackgroundColor
''
,
''
Font
''
,
''
FontSize
''
,
''
FontColor
''
,
''
Content
''
,
''
Align
''
,
''
Codes
''
,
''
Line
''
,
''
Snow
''
))){
if
(
$K
==
"BackgroundColor"
||
$K
==
"FontColor"
){
if
(preg_match(
"#([a-zA-Z0-9]{6})#"
,
$V
))
$this
->
$K
=
$V
;
}
else
{
$this
->
$K
=
$V
;
}
}
}
return
$this
;
}
protected
function
_Re_Set(){
$this
-> Width = 100;
$this
-> Height = 30;
$this
-> BackgroundColor =
"000000"
;
$this
-> Font =
"/phps/Public/Font/ARIALNB.TTF"
;
$this
-> FontSize = 16;
$this
-> FontColor =
"ffffff"
;
$this
-> Align =
"left"
;
$this
-> Codes =4;
$this
-> Line = 6;
}
protected
function
_Create_Img_GB(
$BGC
= True){
$this
-> _Img = imagecreatetruecolor(
$this
-> Width,
$this
-> Height);
if
(
$BGC
){
preg_match(
"#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})#"
,
$this
-> BackgroundColor,
$ColorArr
);
$Color
= imagecolorallocate(
$this
-> _Img,hexdec(
$ColorArr
[1]),hexdec(
$ColorArr
[2]),hexdec(
$ColorArr
[3]));
imagefilledrectangle(
$this
-> _Img,0,
$this
-> Height,
$this
-> Width,0,
$Color
);
}
}
protected
function
_Create_Code(){
$Len
=
strlen
(
$this
-> _Rand) - 1;
for
(
$i
= 0;
$i
<
$this
-> Codes;
$i
++){
$this
-> _Code .=
$this
-> _Rand[mt_rand(0,
$Len
)];
}
}
protected
function
_Write_Text(){
$FontWidth
= imagefontwidth(
$this
-> FontSize);
preg_match_all(
''
/(.)/us
''
,
$this
-> Content,
$TextArr
);
$FontHeight
= imagefontheight(
$this
-> FontSize);
$X
=
ceil
((
$this
-> Width - (
$FontWidth
*
count
(
$TextArr
[0]))) / 2);
$Y
=
ceil
((
$this
-> Height +
$FontHeight
) / 2);
preg_match(
"#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})#"
,
$this
-> FontColor,
$ColorArr
);
$Color
= imagecolorallocate(
$this
-> _Img,hexdec(
$ColorArr
[1]),hexdec(
$ColorArr
[2]),hexdec(
$ColorArr
[3]));
imagettftext(
$this
-> _Img,
$this
-> FontSize,0,
$X
,
$Y
,
$Color
,__WEBROOT__.
$this
-> Font,
$this
-> Content);
}
protected
function
_Write_Code(){
$_X
=
$this
-> Width /
$this
-> Codes;
for
(
$i
= 0;
$i
<
$this
-> Codes;
$i
++){
$Color
= imagecolorallocate(
$this
-> _Img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext(
$this
-> _Img,
$this
-> FontSize,mt_rand(-30,30),
$_X
*
$i
+mt_rand(1,5),
$this
-> Height / 1.3,
$Color
,__WEBROOT__.
$this
-> Font,
$this
-> _Code[
$i
]);
}
}
protected
function
_Write_Line() {
for
(
$i
=0;
$i
<
$this
-> Line;
$i
++) {
$Color
= imagecolorallocate(
$this
-> _Img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline(
$this
-> _Img,mt_rand(0,
$this
-> Width),mt_rand(0,
$this
-> Height),mt_rand(0,
$this
-> Width),mt_rand(0,
$this
-> Height),
$Color
);
}
}
protected
function
_Img_Jpeg(){
header(
''
Content-type:image/jpeg
''
);
imagejpeg(
$this
-> _Img);
imagedestroy(
$this
-> _Img);
}
protected
function
_Img_Png(){
header(
''
Content-type:image/png
''
);
imagepng(
$this
-> _Img);
imagedestroy(
$this
-> _Img);
}
public
function
Create_Img_Jpg(){
$this
-> _Create_Img_GB(True);
$this
-> _Write_Text();
$this
-> _Img_Jpeg();
}
public
function
Create_Img_Png(){
$this
-> _Create_Img_GB(True);
$this
-> _Write_Text();
$this
-> _Img_Png();
}
public
function
Create_Verify(){
$this
-> BackgroundColor =
''
''
;
for
(
$I
= 0;
$I
< 3;
$I
++){
$this
-> BackgroundColor .=
dechex
(mt_rand(20,155));
}
$this
-> _Create_Img_GB(True);
$this
-> _Create_Code();
$this
-> _Write_Line();
$this
-> _Write_Code();
$this
-> _Img_Png();
}
public
function
Get_Verify(){
return
md5(
$this
-> _Code);
}
public
function
Load_Img(
$FilePath
){
$FilePath
= __WEBROOT__.
$FilePath
;
if
(!
is_file
(
$FilePath
)){
$this
-> LoadErr =
"路径错误,文件不存在"
;
Return False;
}
$this
-> _PicInfo =
getimagesize
(
$FilePath
);
$this
-> _FileInfo =
pathinfo
(
$FilePath
);
switch
(
$this
-> _PicInfo[2]){
case
1:
$this
->_FileImg = imagecreatefromgif(
$FilePath
);
break
;
case
2:
$this
->_FileImg = imagecreatefromjpeg(
$FilePath
);
break
;
case
3:
$this
->_FileImg = imagecreatefrompng(
$FilePath
);
break
;
default
:
$this
-> LoadErr =
"类型错误,不支持的图片类型"
;Return False;
}
Return True;
}
public
function
Create_Thumb(
$FileName
,
$FilePath
){
$SavePath
= __WEBROOT__.
$FilePath
;
if
(!
file_exists
(
$SavePath
)){
mkdir
(
$SavePath
,0777,true);
}
$FileName
=
$FileName
.
date
(
"YmdHis"
).rand(100,999).
''
.
''
.
$this
-> _FileInfo[
''
extension
''
];
$FilePath
=
$FilePath
.
$FileName
;
$SavePath
=
$SavePath
.
$FileName
;
$this
-> _Create_Img_GB(False);
imagecopyresampled(
$this
-> _Img,
$this
-> _FileImg,0,0,0,0,
$this
-> Width,
$this
-> Height,
$this
-> _PicInfo[0],
$this
-> _PicInfo[1]);
switch
(
$this
-> _PicInfo[2]){
case
1:imagegif(
$this
-> _Img,
$SavePath
);
break
;
case
2:imagejpeg(
$this
-> _Img,
$SavePath
);
break
;
case
3:imagepng(
$this
-> _Img,
$SavePath
);
break
;
}
$FIleInfo
[
''
FileName
''
] =
$FileName
;
$FIleInfo
[
''
FilePath
''
] =
$FilePath
;
Return
$FIleInfo
;
}
}