<?php
//我们将他们两个共同的一些属性给提取成成员属性,其他不一样的属性作为参数给传递过来
//类的设计
//成员属性
//$path;
//$isRandName;
//$type;
//成员方法
//water($waterPath,$dstPath,$pos,$prefix,$opcity);
//set();
$image = new Image('./upload','png',true);
$image->water('baidu.png','lyf.jpeg',0);
class Image
{
//保存路径
protected $path = './upload/';
//图片保存格式
protected $type = 'png';
protected $isRandName = true;
//水印方法
function __construct($path, $type,$isRandName)
{
$this->path = $path;
$this->type = $type;
$this->isRandName = $isRandName;
}
//水印方法
function water($waterPath,$dstPath,$position = 9,$tmd = 50,$prefix = 'wa_')
{
//判断两个图片是否存在
if (!file_exists($waterPath)||!file_exists($dstPath)) {
die('图片不存在');
}
//判断水印图片的大小是否小于目标图片
$waterInfo = self::getImageInfo($waterPath);
$dstInfo = self::getImageInfo($dstPath);
if ($waterInfo['width'] > $dstInfo['width']||$waterInfo['height'] > $dstInfo['height']) {
die('水印图片太大');
}
//打开图片,不同的图片使用不同的函数打开
$waterRes = self::openAnyImage($waterPath);
$dstRes = self::openAnyImage($dstPath);
//计算位置
if ($position) {
$w = ($dstInfo['width'] - $waterInfo['width']) / 2;
$h = ($dstInfo['height'] - $waterInfo['height'] ) / 2;
//计算行号和列号
$row = floor(($position - 1) / 3);
$col = ($position - 1) % 3;
$x = $w * $col;
$y = $h * $row;
}else{
$x = mt_rand(0,($dstInfo['width']-$waterInfo['width']));
$y = mt_rand(0,($dstInfo['height'] - $waterInfo['height']));
}
//将水印图片给贴过去
imagecopymerge($dstRes, $waterRes, $x, $y, 0, 0, $waterInfo['width'], $waterInfo['height'], $tmd);
//生成新的文件名
$newName = $this->createNewName($prefix,$dstPath);
$filePath = rtrim($this->path,'/').'/'.$newName;
//用相应的函数保存图片
$func = 'image'.$this->type;
$func($dstRes,$filePath);
//释放资源
imagedestroy($waterRes);
imagedestroy($dstRes);
}
protected function createNewName($prefix,$dstPath)
{
if ($this->isRandName) {
return $prefix.uniqid().'.'.$this->type;
}else{
return $prefix.pathinfo($dstPath)['name'];
}
}
//根据图片的路径得到图片的所有信息并且返回
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类型
$info = self::getImageInfo($imagePath);
$mime = $info['mime'];
switch ($mime) {
case 'image/png':
$res = imagecreatefrompng($imagePath);
break;
case 'image/gif':
$res = imagecreatefromgif($imagePath);
break;
case 'image/jpeg':
$res = imagecreatefromjpeg($imagePath);
break;
default:
die('图片类型不正确');
}
return $res;
}
}
//我们将他们两个共同的一些属性给提取成成员属性,其他不一样的属性作为参数给传递过来
//类的设计
//成员属性
//$path;
//$isRandName;
//$type;
//成员方法
//water($waterPath,$dstPath,$pos,$prefix,$opcity);
//set();
$image = new Image('./upload','png',true);
$image->water('baidu.png','lyf.jpeg',0);
class Image
{
//保存路径
protected $path = './upload/';
//图片保存格式
protected $type = 'png';
protected $isRandName = true;
//水印方法
function __construct($path, $type,$isRandName)
{
$this->path = $path;
$this->type = $type;
$this->isRandName = $isRandName;
}
//水印方法
function water($waterPath,$dstPath,$position = 9,$tmd = 50,$prefix = 'wa_')
{
//判断两个图片是否存在
if (!file_exists($waterPath)||!file_exists($dstPath)) {
die('图片不存在');
}
//判断水印图片的大小是否小于目标图片
$waterInfo = self::getImageInfo($waterPath);
$dstInfo = self::getImageInfo($dstPath);
if ($waterInfo['width'] > $dstInfo['width']||$waterInfo['height'] > $dstInfo['height']) {
die('水印图片太大');
}
//打开图片,不同的图片使用不同的函数打开
$waterRes = self::openAnyImage($waterPath);
$dstRes = self::openAnyImage($dstPath);
//计算位置
if ($position) {
$w = ($dstInfo['width'] - $waterInfo['width']) / 2;
$h = ($dstInfo['height'] - $waterInfo['height'] ) / 2;
//计算行号和列号
$row = floor(($position - 1) / 3);
$col = ($position - 1) % 3;
$x = $w * $col;
$y = $h * $row;
}else{
$x = mt_rand(0,($dstInfo['width']-$waterInfo['width']));
$y = mt_rand(0,($dstInfo['height'] - $waterInfo['height']));
}
//将水印图片给贴过去
imagecopymerge($dstRes, $waterRes, $x, $y, 0, 0, $waterInfo['width'], $waterInfo['height'], $tmd);
//生成新的文件名
$newName = $this->createNewName($prefix,$dstPath);
$filePath = rtrim($this->path,'/').'/'.$newName;
//用相应的函数保存图片
$func = 'image'.$this->type;
$func($dstRes,$filePath);
//释放资源
imagedestroy($waterRes);
imagedestroy($dstRes);
}
protected function createNewName($prefix,$dstPath)
{
if ($this->isRandName) {
return $prefix.uniqid().'.'.$this->type;
}else{
return $prefix.pathinfo($dstPath)['name'];
}
}
//根据图片的路径得到图片的所有信息并且返回
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类型
$info = self::getImageInfo($imagePath);
$mime = $info['mime'];
switch ($mime) {
case 'image/png':
$res = imagecreatefrompng($imagePath);
break;
case 'image/gif':
$res = imagecreatefromgif($imagePath);
break;
case 'image/jpeg':
$res = imagecreatefromjpeg($imagePath);
break;
default:
die('图片类型不正确');
}
return $res;
}
}