2014-09-05
下午没事干,就写了个图片加水印处理的类,本类实现了给图片加文字水印,加图片水印,并且实现了透明度的功能,以供各位网友学习和交流
<?php
/**
* 加水印类,支持文字、图片水印以及对透明度的设置、水印图片背景透明。
* @author litx date:2011-12-05下午3点于迈科龙快播研发中心
*/
class WaterMask
{
/**
* 水印类型
* @var int $waterType 0为文字水印 ;1为图片水印
*/
private $waterType = 1;
/**
* 水印位置 类型
* @var int $pos 默认为9(右下角)
*/
private $pos = 9;
/**
* 水印透明度
* @var int $transparent 水印透明度(值越小越透明)
*/
private $transparent = 20;
/**
* 如果是文字水印,则需要加的水印文字
* @var string $waterStr 默认值 (李铁雄个人作品集)
*/
private $waterStr = '个人作品集';
/**
* 文字字体大小
* @var int $fontSize 字体大小
*/
private $fontSize = 14;
/**
* 水印文字颜色(RGB)
* @var array $fontColor 水印文字颜色(RGB)
*/
private $fontColor = array ( 255, 255, 255 );
/**
* 字体文件
* @var unknown_type
*/
private $fontFile = 'AHGBold.ttf';
/**
* 水印图片
* @var string $waterImg
*/
private $waterImg = 'logo.png';
/**
* 需要添加水印的图片
* @var string $srcImg
*/
private $srcImg = '';
/**
* 图片句柄
* @var string $im
*/
private $im = '';
/**
* 水印图片句柄
* @var string $water_im
*/
private $water_im = '';
/**
* 图片信息
* @var array $srcImg_info
*/
private $srcImg_info = '';
/**
* 水印图片信息
* @var array $waterImg_info
*/
private $waterImg_info = '';
/**
* 水印文字宽度
* @var int $str_w
*/
private $str_w = '';
/**
* 水印文字高度
* @var int $str_h
*/
private $str_h = '';
/**
* 水印X坐标
* @var int $x
*/
private $x = '';
/**
* 水印y坐标
* @var int $y
*/
private $y = '';
/**
* 构造函数,通过传入需要加水印的源图片初始化源图片
* @param string $img 需要加水印的源图片
*/
public function __construct ($img)
{
if(file_exists($img)){//源文件存在
$this -> srcImg = $img ;
}else{//源文件不存在
echo '源文件'.$img.'不存在,请检查看文件路径是否正确';
exit();
}
}
/**
* 获取需要添加水印的图片的信息,并载入图片
*/
public function imginfo ()
{
$this -> srcImg_info = getimagesize($this -> srcImg);
var_dump($this -> srcImg_info);exit();
switch ($this -> srcImg_info[2]) {
case 3 ://png
$this -> im = imagecreatefrompng($this -> srcImg);
break 1;
case 2 : // jpeg/jpg
$this -> im = imagecreatefromjpeg($this -> srcImg);
break 1;
case 1 : //gif
$this -> im = imagecreatefromgif($this -> srcImg);
break 1;
default :
echo '源图片文件'. $this -> srcImg .'格式不正确,目前本函数只支持PNG、JPEG、GIF图片水印功能';
exit();
}
}
/**
* 获取水印图片的信息,并载入图片
*/
private function waterimginfo ()
{
$this -> waterImg_info = getimagesize($this -> waterImg);
switch ($this -> waterImg_info[2]) {
case 3 :
$this -> water_im = imagecreatefrompng($this -> waterImg);
break 1;
case 2 :
$this -> water_im = imagecreatefromjpeg($this -> waterImg);
break 1;
case 1 :
$this -> water_im = imagecreatefromgif($this -> waterImg);
break 1;
default :
echo '源图片文件'. $this -> srcImg .'格式不正确,目前本函数只支持PNG、JPEG、GIF图片水印功能';
exit();
}
}
/**
* 水印位置算法
*/
private function waterpos ()
{
switch ($this -> pos) {
case 0 : //随机位置
$this -> x = rand(0, $this -> srcImg_info[0] - $this -> waterImg_info[0]);
$this -> y = rand(0, $this -> srcImg_info[1] - $this -> waterImg_info[1]);
break 1;
case 1 : //上左
$this -> x = 20;
$this -> y = 20;
break 1;
case 2 : //上中
$this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2;
$this -> y = 20;
break 1;
case 3 : //上右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0];
$this -> y = 20;
break 1;
case 4 : //中左
$this -> x = 20;
$this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2;
break 1;
case 5 : //中中
$this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2;
$this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2;
break 1;
case 6 : //中右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20;
$this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2;
break 1;
case 7 : //下左
$this -> x = 20;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
case 8 : //下中 www.zhishiwu.com
$this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
case 9 : //下右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
default : //下右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
}
}
/**
* 加图片水印
*/
private function waterimg ()
{
if ($this -> srcImg_info[0] <= $this -> waterImg_info[0] || $this -> srcImg_info[1] <= $this -> waterImg_info[1]) {
echo '图片尺寸太小,无法加水印,请上传一张大图片';
exit();
}
//计算水印位置
$this->waterpos();
$cut = imagecreatetruecolor($this -> waterImg_info[0], $this -> waterImg_info[1]);
imagecopy($cut, $this -> im, 0, 0, $this -> x, $this -> y, $this -> waterImg_info[0],
$this -> waterImg_info[1]);
$pct = $this -> transparent;
imagecopy($cut, $this -> water_im, 0, 0, 0, 0, $this -> waterImg_info[0],
$this -> waterImg_info[1]);
//将图片与水印图片合成
imagecopymerge($this -> im, $cut, $this -> x, $this -> y, 0, 0, $this -> waterImg_info[0], $this -> waterImg_info[1], $pct);
}
/**
* 加文字水印
*/
private function waterstr ()
{
$rect = imagettfbbox($this -> fontSize, 0, $this -> fontFile, $this -> waterStr);
$w = abs($rect[2] - $rect[6]);
$h = abs($rect[3] - $rect[7]);
$fontHeight = $this -> fontSize;
$this -> water_im = imagecreatetruecolor($w, $h);
imagealphablending($this -> water_im, false);
imagesavealpha($this -> water_im, true);
$white_alpha = imagecolorallocatealpha($this -> water_im, 255, 255, 255, 127);
imagefill($this -> water_im, 0, 0, $white_alpha);
$color = imagecolorallocate($this -> water_im, $this -> fontColor[0], $this -> fontColor[1],
$this -> fontColor[2]);
imagettftext($this -> water_im, $this -> fontSize, 0, 0, $this -> fontSize, $color,
$this -> fontFile, $this -> waterStr);
$this -> waterImg_info = array (
0 => $w, 1 => $h
);
$this->waterimg();
}
/**
* 水印图片输出
*/
public function output ()
{
$this->imginfo();
if ($this -> waterType == 0) {
$this->waterstr();
} else {
$this->waterimginfo();
$this->waterimg();
}
switch ($this -> srcImg_info[2]) {
case 3 :
imagepng($this -> im, $this -> srcImg);
break 1;
case 2 :
imagejpeg($this -> im, $this -> srcImg);
break 1;
case 1 :
imagegif($this -> im, $this -> srcImg);
break 1;
default :
die('添加水印失败!');
break;
}
//图片合成后的后续销毁处理
imagedestroy($this -> im);
imagedestroy($this -> water_im);
}
}
使用方法示例:
//实例化对象
$obj = new WaterMask('img/10451.jpg');
//类型:0为文字水印、1为图片水印
$obj->waterType = 0;
//水印透明度,值 越小透明度越高
$obj->transparent = 15;
//水印文字
//$obj->waterStr = '生日快乐';
//水印图片
//$obj->waterImg = '';//水印图片
//文字字体大小
$obj->fontSize = 14;
//水印文字颜色(RGB)
$obj->fontColor = array(255,255,100);
//字体文件
$obj->fontFile = 'STCAIYUN.ttf';
//输出水印图片文件覆盖到输入的图片文件
$obj->output();
使用方式比较简单,也很实用。
作者 ltx851201
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
将视频设置为Android手机开机动画的教程
12-11
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
常用dos命令及语法
2014-09-27
PHP中include和require区别之我见
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
php递归返回值的问题
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
学习使用C语言/C++编程的7个步骤!超赞~
2022-03-20
零基础的初学者怎样学习java,或者应该先学什么?
2022-03-21
收割者剑客传奇免谷歌汉化版下载v2.0.1 安卓版
其它手游 40.19MB
下载
reaper收割者破解版下载v2.0.1 安卓完美版
其它手游 40.19MB
下载
reaper汉化版下载v2.0.1 安卓中文版
其它手游 40.19MB
下载
死神苍白剑士的传说中文版(Reaper)下载v2.0.1 安卓版
其它手游 40.19MB
下载
宝宝巴士迷宫小镇下载v9.89.99.01 官方安卓版
其它手游 113.4MB
下载
宝宝迷宫大作战游戏下载v9.89.99.01 安卓版
其它手游 113.4MB
下载
宝宝巴士干净的妙妙游戏下载v9.89.99.00 安卓版
其它手游 76.43MB
下载
妙妙爱干净宝宝巴士下载v9.89.99.00 安卓版
其它手游 76.43MB
下载翻炒厨师游戏下载v2.1.3 安卓版
下载
维塔战士游戏最新版下载v976 安卓版
下载
泡泡小镇城堡游戏下载v1.1.5 安卓完整版
下载
3d蚊子模拟器游戏最新版下载v2023.08.22 安卓版
下载
超级机器人英雄游戏下载v1.1.3 安卓版
下载
robot super游戏下载v1.1.3 安卓版
下载
zombie tsunami游戏下载v4.6.8 安卓版
下载
2026僵尸尖叫正版下载v4.6.8 安卓官方版
下载