<?php
class water{
//待添加图片资源,水印资源
public $src_im,$water_im;
//水印位置
public $posW,$posH;
//待加水印图片宽高
private $fileW,$fileH;
//水印图片宽高和按比例生成的宽高
private $waterW,$waterH,$waterNW,$waterNH;
//水印尺寸
private $size;
//构造函数
public function __construct($file,$water,$size,$pos){
$this->size = $this->setSize($size);
$this->setFileInfo($file);
$this->setWaterInfo($water);
$this->setPos($pos);
$this->dealImage();
}
/**
* 得到水印大小
* @param $size int 尺寸
*/
private function setSize($size){
switch($size){
case 1:
return 0.5;
break;
case 2:
return 0.75;
break;
case 3:
return 0.98;
break;
default:
return 0.75;
}
}
/**
* 得到图片宽高和图片资源
* @param $file string 待加水印图片
*/
private function setFileInfo($file){
$arr = getimagesize($file);
$this->src_im = $this->setSourse($file,$arr['mime']);
$this->fileW = $arr[0];
$this->fileH = $arr[1];
return true;
}
/**
* 得到水印图片宽高和按比例缩小的宽高和水印图片资源
* @param $water string 水印图片
*/
private function setWaterInfo($water){
$arr = getimagesize($water);
$this->water_im = $this->setSourse($water,$arr['mime']);
$this->waterW = $arr[0];
$this->waterH = $arr[1];
//按比例得到新水印的宽高
$this->waterNW = $this->fileW * $this->size;
$this->waterNH = $this->waterNW * ($this->waterH / $this->waterW);
return true;
}
/**
* 生成水印
*/
private function dealImage(){
imagecopyresized($this->src_im,$this->water_im,$this->posW,$this->posH,0,0,$this->waterNW,$this->waterNH,$this->waterW,$this->waterH);
}
/**
* 按后缀得到图片资源
* @param $file string 图片
* @param $ext string 图片后缀
*/
private function setSourse($file,$ext){
switch($ext){
case 'image/jpeg':
return imagecreatefromjpeg($file);
break;
case 'image/png':
return imagecreatefrompng($file);
break;
case 'image/gif':
return imagecreatefromgif($file);
break;
}
}
/**
* 设定水印生成位置
* @param $pos int 位置
*/
private function setPos($pos){
switch($pos){
case 1:
$this->posW = ($this->fileW - $this->waterNW) / 5;
$this->posH = ($this->fileW - $this->waterNW) / 5;
break;
case 2:
$this->posW = ($this->fileW - $this->waterNW) / 2;
$this->posH = ($this->fileW - $this->waterNW) / 5;
break;
case 3:
$this->posW = $this->fileW - $this->waterNW;
$this->posH = ($this->fileW - $this->waterNW) / 5;
break;
case 4:
$this->posW = ($this->fileW - $this->waterNW) / 5;
$this->posH = ($this->fileH - $this->waterNH) / 2;
break;
case 5:
$this->posW = ($this->fileW - $this->waterNW) / 2;
$this->posH = ($this->fileH - $this->waterNH) / 2;
break;
case 6:
$this->posW = $this->fileW - $this->waterNW;
$this->posH = ($this->fileH - $this->waterNH) / 2;
break;
case 7:
$this->posW = ($this->fileW - $this->waterNW) / 5;
$this->posH = ($this->fileH - $this->waterNH) / 1.1;
break;
case 8:
$this->posW = ($this->fileW - $this->waterNW) / 2;
$this->posH = ($this->fileH - $this->waterNH) / 1.1;
break;
case 9:
$this->posW = $this->fileW - $this->waterNW;
$this->posH = ($this->fileH - $this->waterNH) / 1.1;
break;
}
}
/**
* 返回生成水印后的图片
*/
public function getImage(){
return $this->src_im;
}
}
if(isset($_POST['submit'])){
if(isset($_POST['file'])&&isset($_POST['water'])&&isset($_POST['size'])&&isset($_POST['pos'])){
$file = $_POST['file'];
$water = $_POST['water'];
$size = $_POST['size'];
$pos = $_POST['pos'];
$im = new water($file,$water,$size,$pos);
imagejpeg($im->getImage(),'newImage.jpg');
}else{
echo "<script>alert('数据不完整')</script>";
}
}
?>
<html>
<head>
<title>水印生成器</title>
</head>
<body>
<form name="myform" action="" method="POST">
<p>待生成水印图片:</p>
<input type="text" name="file" value="1.jpeg"/><br />
<p>水印图片:</p>
<input type="text" name="water" value="logo.png"/><br />
<p>大小:</p>
<input type="radio" name="size" value="1" />小 <input type="radio" name="size" value="2" />中 <input type="radio" name="size" value="3" />大<br />
<p>位置:</p>
<table border="1px">
<tr>
<td><input type="radio" name="pos" value="1" />左上</td>
<td><input type="radio" name="pos" value="2" />中上</td>
<td><input type="radio" name="pos" value="3" />右上</td>
</tr>
<tr>
<td><input type="radio" name="pos" value="4" />左中</td>
<td><input type="radio" name="pos" value="5" />中间</td>
<td><input type="radio" name="pos" value="6" />右中</td>
</tr>
<tr>
<td><input type="radio" name="pos" value="7" />左下</td>
<td><input type="radio" name="pos" value="8" />中下</td>
<td><input type="radio" name="pos" value="9" />右下</td>
</tr>
</table>
<input type="submit" name="submit" value="生成" />
</form>
<hr />
<img src="newImage.jpg" />
</body>
</html>