php 之 图片水印 (支持批量图片处理)(支持添加图片水印,文字水印)

运行文件: index.php

<?php
	require_once 'ImageWaterMake.class.php';
	require_once 'waterImg.conf.php';
	
	$iwm = new ImageWaterMake();	//创建水印对象
	
	if(!empty($waterImg)){
		foreach ($waterImg as $arrI){
			waterImg($iwm,$bgPath,$arrI,'');
		}
	}
	
	if (!empty($waterText)) {
		foreach ($waterText as $arrT){
			waterImg($iwm,$bgPath,'',$arrT);
		}
	}
	
	echo "程序结束!";
	//--------------------------------------------------------------------------------------------
	//打水印
	function waterImg($iwm,$bgPath,$waterImg,$waterText){
		if (is_dir($bgPath)) {
			if(($dir_handle = @opendir($bgPath))!=null){
				while(($filename = readdir($dir_handle))!=null){
					
					if ($filename != '.' && $filename != '..') {
						$subFile=$bgPath.'/'.$filename;//获取文件名字
						if (is_file($subFile)) {//为文件  打水印
							$info = pathinfo($subFile);
							if($info['extension'] =='jpg' || $info['extension'] =='png' || $info['extension'] =='gif'){
								
							
							$iwm->setGroundImage($subFile);//设置背景图
							///设置水印属性
							if(!empty($waterImg)){
								$iwm->setWaterImg($waterImg[0]);//设置水印图片
								$iwm->setWaterPerPos($waterImg[1]);//设置水印位置
								$iwm->setWaterSize($waterImg[2]);//设置水印图片大小
							}
							if (!empty($waterText)) {
								$iwm->setWaterText($waterText[0]);//设置文字水印中的文字
								$iwm->setTextPerPos($waterText[1]);//设置文字水印位置
								$iwm->setTextSize($waterText[2]);//设置文字水印大小
								$iwm->setTextColor($waterText[3]);//设置文字颜色
							}
							///设置水印属性
							$iwm->run();//打水印
							}else {
								continue;
							}
						}
						if (is_dir($subFile)) {//为目录
							waterImg($iwm,$subFile,$waterImg,$waterText);
						}
					}
				}
			}else{
				echo "背景文件夹打开错误!";
			}
	
		}else{
			echo "背景图片目录无法获取!";
		}
	}
?>



水印处理类 ImageWaterMake.class.php

<?php
/**
 * 图片水印类
 * @author skyling
 * 2014/7/28
 */
class ImageWaterMake {
	private $groundImage; //背景图
	private $waterImg;	  //水印图
	private $waterPos;	  //水印位置
	private $waterText;	  //水印文字
	private $textSize;	  //文字大小
	private $textColor;	  //文字颜色
	private $textPos;	  //文字水印位置 
	private $waterSize;		//图片水印缩放比例
	
	
	private $waterPerPos;
	private $textPerPos;
	private $bgIm;
	private $waterIm;
	private $saveFile;
	
	private $isWaterImage=TRUE;	//是否可以打水印
	private $bgInfo;//[0] width [1]height [2] .jpg
	private $waterInfo;//[0] width [1]height [2] .jpg

	public function run(){
		
		$this->readBgImg();		//读取背景文件		
		$this->readWaterImg();	//读取水印文件		
		$this->getTextPos();
		$this->getWaterPos();
		$this->chImgSize();	//改变水印大小
		
		$this->imageWaterMarker();
		$this->saveImg();
	}
	
	
	/**
	 * 改变水印大小
	 */
	private function chImgSize(){
		if ($this->saveFile == $this->waterImg || $this->waterSize==1) {
			return ;
		}
		$this->saveFile = dirname($this->waterImg).'\ch'.$this->waterSize.basename($this->waterImg);
		if(!file_exists($this->saveFile)){
			$im  =  imagecreatetruecolor( $this->waterInfo[0]*$this->waterSize, $this->waterInfo[1]*$this->waterSize);			
			imagealphablending($im, true);
			imagesavealpha($im, true);
			$white = imagecolorallocatealpha($im,255,255,255,127);
			imagefill($im,0,0,$white);
			
			imagecopyresized($im, $this->waterIm, 0, 0, 0, 0, $this->waterInfo[0]*$this->waterSize, $this->waterInfo[0]*$this->waterSize, $this->waterInfo[0], $this->waterInfo[1]);
			switch($this->bgInfo[2])//取得背景图片的格式
			{
				case 1:imagegif($im,$this->saveFile);break;
				case 2:imagejpeg($im,$this->saveFile);break;
				case 3:imagepng($im,$this->saveFile);break;
				default:die($this->waterIm."改变大小发生错误!");
			}
		}
		$this->setWaterImg($this->saveFile);
		$this->readWaterImg();
	}
	/**
	 * 读取背景
	 */
	private function readBgImg(){
		if (!empty($this->groundImage) && file_exists($this->groundImage)) {
			$this->bgInfo = getimagesize($this->groundImage);
			
			switch($this->bgInfo[2])//取得水印图片的格式
			{
				case 1:$this->bgIm = imagecreatefromgif($this->groundImage);break;
				case 2:$this->bgIm = imagecreatefromjpeg($this->groundImage);break;
				case 3:$this->bgIm = imagecreatefrompng($this->groundImage);break;
				default:die("背景图片暂时只支持gif,jpg,png!");$this->isWaterImage = FALSE;break;
			}
			return;
		}
		$this->isWaterImage = FALSE;
	}
	
	/**
	 * 读取水印
	 */
	private function readWaterImg(){
		if (!empty($this->waterImg) && file_exists($this->waterImg)) {
			$this->waterInfo = getimagesize($this->waterImg);
			switch($this->waterInfo[2])//取得水印图片的格式
			{
				case 1:$this->waterIm = imagecreatefromgif($this->waterImg);break;
				case 2:$this->waterIm = imagecreatefromjpeg($this->waterImg);break;
				case 3:$this->waterIm = imagecreatefrompng($this->waterImg);break;
				default:die("水印图片暂时只支持gif,jpg,png!");$this->isWaterImage = FALSE;echo "水印图片路径不正确!";
			}
			return;
		}
		
		$this->isWaterImage = FALSE;
		echo "水印图片路径不正确!";
	}
	
	public function saveImg(){
		//生成水印后的图片
		$saveFile = $this->groundImage;
		switch($this->bgInfo[2])//取得背景图片的格式
		{
			case 1:imagegif($this->bgIm,$saveFile);break;
			case 2:imagejpeg($this->bgIm,$saveFile);break;
			case 3:imagepng($this->bgIm,$saveFile);break;
			default:die($this->groundImage."生成图片错误!");break;
		}
		echo $this->groundImage. "   +   ".$this->waterImg."水印生成成功!<br>";
	}
	
	function __destruct(){

	}
	/**
	 * 创建水印
	 */
	public function imageWaterMarker(){
		imagealphablending($this->bgIm, true);
		
		if ($this->isWaterImage) {//图片水印
			imagecopy($this->bgIm, $this->waterIm, $this->waterPos['x'], $this->waterPos['y'], 0, 0, $this->waterInfo[0], $this->waterInfo[1]);
		}
		if ($this->waterText) {//文字水印
			if( !empty($this->textColor) && (strlen($this->textColor)==7) )
			{
				$R = hexdec(substr($this->textColor,1,2));
				$G = hexdec(substr($this->textColor,3,2));
				$B = hexdec(substr($this->textColor,5));
			}
			else
			{
				die("水印文字颜色格式不正确!");
			}
			imagestring ( $this->bgIm, $this->textSize, $this->textPos['x'], $this->textPos['y'], $this->waterText, imagecolorallocate($this->bgIm, $R, $G, $B));
		}
	}
	
	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function getWaterPos() {
		if ($this->waterPerPos) {
			$this->waterPos['x']=ceil($this->bgInfo[0]*$this->waterPerPos[0]);
			$this->waterPos['y']=ceil($this->bgInfo[1]*$this->waterPerPos[1]);
		}else {
			$this->waterPos['x']=ceil($this->bgInfo[0]*0.3);
			$this->waterPos['y']=ceil($this->bgInfo[1]*0.4);
		}
	}
	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function getTextPos() {
	
		if ($this->textPerPos) {
			$this->textPos['x']=ceil($this->bgInfo[0]*$this->textPerPos[0]);
			$this->textPos['y']=ceil($this->bgInfo[1]*$this->textPerPos[1]);
		}else {
			$this->textPos['x']=ceil($this->bgInfo[0]*0.1);
			$this->textPos['y']=ceil($this->bgInfo[1]*0.9);
		}
	}
	/**
	 * 设置水印大小
	 * @param number $per
	 */
	public function setWaterSize($waterSize=1){
		$this->waterSize=$waterSize;
	}
	
	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setGroundImage($groundImage) {
		$this->groundImage = $groundImage;
	}

	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setWaterImg($waterImg) {
		$this->waterImg = $waterImg;
	}

	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setWaterText($waterText) {
		$this->waterText = $waterText;
	}

	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setTextSize($textSize) {
		$this->textSize = $textSize;
	}

	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setTextColor($textColor) {
		$this->textColor = $textColor;
	}
	
	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setWaterPerPos($waterPerPos) {
		$this->waterPerPos = $waterPerPos;
	}

	/**
	 * @param !CodeTemplates.settercomment.paramtagcontent!
	 */
	public function setTextPerPos($textPerPos) {
		$this->textPerPos = $textPerPos;
	}


	
	
}

?>


配置文件 waterImg.conf.php

<?
#生成水印前请备份文件
#背景路径  绝对路径
$bgPath='C:/Users/skyling/Desktop/ypyt';
#水印图片
#可设置多张
/*格式如下
waterImg[n][0]  可为相对或绝对路径                                  string
waterImg[n][1]  位置  上左百分比位置      0-1 array(1,1)
waterImg[n][2]  缩放大小                                 0-1 若无需改变大小请默认为1  因为浏览器支持不同情慎用保持值为1
 * */
$waterImg = array(
array(
	"logo.png",
	array(0.02,0.02),
	1
),
array(
	"text.png",
	array(0.3,0.4),
	1
)
);
#文字水印
#可设置多个
/*参数格式:
$waterText[n][0]  文字
$waterText[n][1]  位置  上左百分比位置      0-1 array(1,1)
$waterText[n][2]  大小   1-5
$waterText[n][3]  颜色  #000000
 */
$waterText = array(
array(
		'hello',
		array(0.1,0.9),
		2,
		'#ffffff'
)
);
?>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值