<?php
header('Content-type:text/html;charset=utf8');
$img
=
new
Image();
class
Image{
protected
$path
;
protected
$isRandName
;
protected
$type
;
function
__construct(
$path
='./',
$isRandName
=true,
$type
='png'){
$this
->path =
$path
;
$this
->isRandName =
$isRandName
;
$this
->type =
$type
;
}
function
water(
$image
,
$water
,
$postion
,
$tmp
=100,
$prefix
='water_'){
if
(!
file_exists
(
$image
)||!
file_exists
(
$water
)){
die
('图片资源不存在');
}
$imageInfo
= self::getImageInfo(
$image
);
$waterInfo
= self::getImageInfo(
$water
);
if
(!
$this
->checkImage(
$imageInfo
,
$waterInfo
)){
die
('水印图片太大');
}
$imageRes
= self::openAnyImage(
$image
);
$waterRes
= self::openAnyImage(
$water
);
$pos
=
$this
->getPosition(
$postion
,
$imageInfo
,
$waterInfo
);
imagecopymerge(
$imageRes
,
$waterRes
,
$pos
['x'],
$pos
['y'], 0, 0,
$waterInfo
[
"width"
],
$waterInfo
[
"height"
],
$tmp
);
$newName
=
$this
->createNewName(
$image
,
$prefix
);
$newPath
= rtrim(
$this
->path,'/').'/'.
$newName
;
$this
->saveImage(
$imageRes
,
$newPath
);
imagedestroy(
$imageRes
);
imagedestroy(
$waterRes
);
return
$newPath
;
}
protected
function
saveImage(
$imageRes
,
$newPath
){
$func
= 'image'.
$this
->type;
$func
(
$imageRes
,
$newPath
);
}
protected
function
createNewName(
$imagePath
,
$prefix
){
if
(
$this
->isRandName){
$name
=
$prefix
.uniqid().'.'.
$this
->type;
}
else
{
$name
=
$prefix
.
pathinfo
(
$imagePath
)['filename'].'.'.
$this
->type;
}
return
$name
;
}
protected
function
getPosition(
$postion
,
$imageInfo
,
$waterInfo
){
switch
(
$postion
){
case
1:
$x
= 0;
$y
= 0;
break
;
case
2:
$x
= (
$imageInfo
['width']-
$waterInfo
[
"width"
])/2;
$y
= 0;
break
;
case
3:
$x
=
$imageInfo
[
"width"
]-
$waterInfo
[
"width"
];
$y
= 0;
break
;
case
4:
$x
= 0;
$y
= (
$imageInfo
[
"height"
]-
$waterInfo
[
"height"
])/2;
break
;
case
5:
$x
= (
$imageInfo
['width']-
$waterInfo
[
"width"
])/2;
$y
= (
$imageInfo
[
"height"
]-
$waterInfo
[
"height"
])/2;
break
;
case
6:
$x
=
$imageInfo
[
"width"
]-
$waterInfo
[
"width"
];
$y
= (
$imageInfo
[
"height"
]-
$waterInfo
[
"height"
])/2;
break
;
case
7:
$x
= 0;
$y
=
$imageInfo
['height'] -
$waterInfo
[
"height"
];
break
;
case
8:
$x
= (
$imageInfo
['width']-
$waterInfo
[
"width"
])/2;
$y
=
$imageInfo
['height'] -
$waterInfo
[
"height"
];
break
;
case
9:
$x
=
$imageInfo
[
"width"
]-
$waterInfo
[
"width"
];
$y
=
$imageInfo
['height'] -
$waterInfo
[
"height"
];
break
;
case
0:
$x
= mt_rand(0,
$imageInfo
[
"width"
]-
$waterInfo
[
"width"
]);
$y
= mt_rand(0,
$imageInfo
['height'] -
$waterInfo
[
"height"
]);
break
;
}
return
['x'=>
$x
, 'y'=>
$y
];
}
protected
function
checkImage(
$imageInfo
,
$waterInfo
){
if
((
$waterInfo
['width'] >
$imageInfo
['width'])||(
$waterInfo
['height'] >
$imageInfo
['height'])){
return
false;
}
return
true;
}
static
function
getImageInfo(
$imagePath
){
$info
=
getimagesize
(
$imagePath
);
$data
['width']=
$info
[0];
$data
['height']=
$info
[1];
$data
['mime'] =
$info
['mime'];
return
$data
;
}
static
function
openAnyImage(
$imagePath
){
$mime
= self::getImageInfo(
$imagePath
)['mime'];
switch
(
$mime
){
case
'image/png':
$image
= imagecreatefrompng(
$imagePath
);
break
;
case
'image/gif':
$image
= imagecreatefromgif(
$imagePath
);
break
;
case
'image/jpeg':
$image
= imagecreatefromjpeg(
$imagePath
);
break
;
case
'image/wbmp':
$image
= imagecreatefromwbmp(
$imagePath
);
break
;
}
return
$image
;
}
}