a.php
<?php
$d=getimagesize('xbdl.jpg');
var_dump($d);
?>
image.class.php
<?php
class Image{
private $path;
public function __construct($path='./'){
$this->path=rtrim($path,'/').'/';
}
public function water($background,$water,$pos=0,$tm=100,$prefix='water_'){
$background=$this->path.$background;
$water=$this->path.$water;
if(file_exists($background)|file_exists($water)){
$backInfo=self::getImageInfo($background);
$waterInfo=self::getImageInfo($water);
if(!self::checkSize($backInfo,$waterInfo)){
echo '水印图片的大小超过了背景图片的大小';
return false;
}
$position=self::getPosition($backInfo,$waterInfo,$pos);
//打开图片资源
$backRes=self::openImage($background,$backInfo['type']);
$waterRes=self::openImage($water,$waterInfo['type']);
$newRes=self::mergeImage($backRes,$waterRes,$position,$waterInfo,$tm);
if($path=self::save($newRes,$backInfo,$prefix,$this->path)){
self::des($waterRes);
self::des($backRes);
return $path;
}else{
echo '保存文件失败';
return false;
}
}else{
echo '传入的水印图片或者是背景图片未找到';
return false;
}
}
static function des($res){
imagedestroy($res);
}
static private function save($res,$info,$prefix,$path){
$path=$path.$prefix.$info['name'];
switch($info['type']){
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
imagejpeg($res,$path);
break;
case 'image/gif':
imagegif($res,$path);
break;
case 'image/png':
case 'image/x-png':
imagepng($res,$path);
break;
case 'image/wbmp':
imagewbmp($res,$path);
break;
default:
echo '我不认识这个类型';
}
return $path;
}
static private function mergeImage($b,$w,$p,$wI,$tm){
imagecopymerge($b,$w,$p['x'],$p['y'],0,0,$wI['width'],$wI['height'],$tm);
return $b;
}
static private function openImage($path,$type){
switch($type){
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
$res=imagecreatefromjpeg($path);
break;
case 'image/gif':
$res=imagecreatefromgif($path);
break;
case 'image/png':
case 'image/x-png':
$res=imagecreatefrompng($path);
break;
case 'image/wbmp':
$res=imagecreatefromwbmp($path);
break;
default:
echo '我不认识这个类型';
$res=null;
}
return $res;
}
static private function getPosition($b,$w,$pos){
switch($pos){
case 1:
$x=0;
$y=0;
break;
case 2:
$x=ceil(($b['width']-$w['width'])/2);
$y=0;
break;
case 3:
$x=$b['width']-$w['width'];
$y=0;
break;
case 4:
$x=0;
$y=ceil(($b['height']-$w['height'])/2);
break;
case 5:
$x=ceil(($b['width']-$w['width'])/2);
$y=ceil(($b['height']-$w['height'])/2);
break;
case 6:
$x=$b['width']-$w['width'];
$y=ceil(($b['height']-$w['height'])/2);
break;
case 7:
$x=0;
$y=$b['height']-$w['height'];
break;
case 8:
$x=ceil(($b['width']-$w['width'])/2);
$y=$b['height']-$w['height'];
break;
case 9:
$x=$b['width']-$w['width'];
$y=$b['height']-$w['height'];
break;
case 0:
$x=mt_rand(0,$b['width']-$w['width']);
$y=mt_rand(0,$b['height']-$w['height']);
break;
}
return array('x'=>$x,'y'=>$y);
}
static private function checkSize($backInfo,$waterInfo){
if($backInfo['width']<=$waterInfo['width']|$backInfo['height']<=$waterInfo['height']){
return false;
}else{
return true;
}
}
private static function getImageInfo($path){
$info=getimagesize($path);
$data['width']=$info[0];
$data['height']=$info[1];
$data['type']=$info['mime'];
$data['name']=basename($path);
return $data;
}
}
?>
image.php
<?php
include 'image.class.php';
$a=new Image();
$a->water('a.jpg','taobao.png',1,100,'wa1_');
$a->water('a.jpg','taobao.png',2,100,'wa2_');
$a->water('a.jpg','taobao.png',3,100,'wa3_');
$a->water('a.jpg','taobao.png',4,100,'wa4_');
$a->water('a.jpg','taobao.png',5,100,'wa5_');
$a->water('a.jpg','taobao.png',6,100,'wa6_');
$a->water('a.jpg','taobao.png',7,100,'wa7_');
$a->water('a.jpg','taobao.png',8,100,'wa8_');
$a->water('a.jpg','taobao.png',9,100,'wa9_');
$a->water('a.jpg','taobao.png',0,100,'wa0_');
?>