PHP水印类
<?php
/**
* 水印类
* @author zhaoyingnan 2015/07/16
**/
include_once('extend.php');
class Watermark_extend extends Extend
{
private $_isString;//水印类型是否为字符串
private $_imageInfo;//图片的信息array('dirname' => '路径','basename' => '文件名.扩展名','extension' => '扩展名','filename' => '文件名','type' => 类型,'height' => 高,'width' =>宽)
private $_iAngle = 0;//角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
private $_iPct = 15;//图片水印透明度,值越小透明度越高
private $_srcImage;
private $_resImage;//resource of type (gd)
private $_resWaterImage;//resource of type (gd)
private $_sString = 'Fuck You';//水印文字
private $_iStringSize = 20;//字体大小
private $_trueType;//字体库
private $_waterImg;//水印图片 /**
* 设置水印参数
* @author zhaoyingnan 2015/07/16
* @param string $image 图片路径
* @param bool $isString
* @return array('error'=>int,'msg'=>string) error大于0为成功,msg为描述
**/
public function setParam($image, $isString)
{
if(!file_exists($image))return array('error'=>-1,'msg'=>'文件不存在');
$this->_trueType = __ROOTPATH__.'Kreepshow.ttf';//字体库位置
$this->_waterImg = __ROOTPATH__.'img/logo/logo_16.png';//水印图片位置
$this->_srcImage = $image;
$this->_isString = $isString ? true : false;//是否为文字水印
$this->_imageInfo = pathinfo($this->_srcImage);//图片信息
list($this->_imageInfo['width'], $this->_imageInfo['height'], $this->_imageInfo['type']) = @getimagesize($this->_srcImage);//图片信息
//dump($this->_imageInfo);
switch($this->_imageInfo['type'])
{
case IMAGETYPE_PNG://
$this->_resImage = @imagecreatefrompng($this->_srcImage);
break;
case IMAGETYPE_JPEG://
$this->_resImage = @imagecreatefromjpeg($this->_srcImage);
break;
case IMAGETYPE_GIF://
$this->_resImage = @imagecreatefromgif($this->_srcImage);
break;
default:
return array('error'=>-2,'msg'=>'文件类型不正确');
}
//dump($this->_resImage);
return array('error'=>1,'msg'=>'ok');
} /**
* 计算水印的坐标
* @author zhaoyingnan 2015/07/17
* @param int $iWidth 水印的宽度
* @param int $iHeight 水印的高度
* @return array array(int $x,int $y)
**/
public function calculatePosition($iWidth, $iHeight)
{
if($this->_imageInfo['width'] < $iWidth || $this->_imageInfo['height'] < $iHeight)
return null;
//return array(($this->_imageInfo['width']-$iWidth)/2, ($this->_imageInfo['height']-$iHeight)/2);
if($this->_isString)
return array($this->_imageInfo['width']/2-$iWidth, $this->_imageInfo['height']/2);
else
return array(($this->_imageInfo['width']-$iWidth)/2, ($this->_imageInfo['height']-$iHeight)/2);
} /**
* 执行添加水印
* @author zhaoyingnan 2015/07/17
* @return array('error'=>int,'msg'=>string) error大于0为成功,msg为描述
**/
public function executeWater()
{
if($this->_isString)
{
//水印为文字
$black = @imagecolorallocate($this->_resImage, 0, 0, 0);#颜色
/*
array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )
size 像素单位的字体大小。
angle text 将被度量的角度大小。
fontfile TrueType 字体文件的文件名(可以是 URL)。根据 PHP 所使用的 GD 库版本,可能尝试搜索那些不是以 '/' 开头的文件名并加上 '.ttf' 的后缀并搜索库定义的字体路径
text 要度量的字符串。
imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
0 左下角 X 位置
1 左下角 Y 位置
2 右下角 X 位置
3 右下角 Y 位置
4 右上角 X 位置
5 右上角 Y 位置
6 左上角 X 位置
7 左上角 Y 位置
*/
//list(,$rightbottomX,$rightbottomY,,,$lefttopX,$lefttopY) = imagettfbbox($this->_iStringSize, 0, __ROOTPATH__.'movieola.ttf', $this->_sString);
list(,$rightbottomX,$rightbottomY,,,$lefttopX,$lefttopY) = @imagettfbbox($this->_iStringSize, $this->_iAngle, $this->_trueType, $this->_sString); //计算水印的宽度,长度
$iWidth = $rightbottomX - $lefttopX;
$iHeight = $rightbottomY - $lefttopY; list($x, $y) = $this->calculatePosition($iWidth, $iHeight); //调试
//echo '图片宽度'.$this->_imageInfo['width'].'<br/>';
//echo '图片高度'.$this->_imageInfo['height'].'<br/>';
//echo '水印宽度'.$iWidth.'<br/>';
//echo '水印高度'.$iHeight.'<br/>';
//echo '坐标X'.$x.'<br/>';
//echo '坐标Y'.$y.'<br/>'; if(!$x||!$y)return array('error'=>-3,'msg'=>'图片大小小于水印的大小,无法添加水印');
/*
imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
image 由图象创建函数(例如imagecreatetruecolor())返回的图象资源。
size 字体的尺寸。根据 GD 的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。
angle 角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
x 由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0
Y 坐标。它设定了字体基线的位置,不是字符的最底端。
color 颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见 imagecolorallocate()。
fontfile 是想要使用的 TrueType 字体的路径。
text UTF-8 编码的文本字符串
*/
//imagettftext($this->_resImage, $this->_iStringSize, 0, $x, $y, $black, __ROOTPATH__.'movieola.ttf', $this->_sString);
@imagettftext($this->_resImage, $this->_iStringSize, $this->_iAngle, $x, $y, $black, $this->_trueType, $this->_sString);
}
else
{
//图片水印
$this->_resWaterImage = @imagecreatefrompng($this->_waterImg);
/*
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。 bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。
当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
*/ list($x, $y) = $this->calculatePosition(16, 16);//已经知道了水印图片的高度和宽度,不用获取了
if(!$x||!$y)return array('error'=>-3,'msg'=>'图片大小小于水印的大小,无法添加水印');
$bool = @imagecopy($this->_resImage, $this->_resWaterImage, $x, $y, 0, 0, 16, 16);
//$bool = @imagecopymerge($this->_resImage, $this->_resWaterImage, $x, $y, 0, 0, 512, 512, $this->_iPct);
if(!$bool)return array('error'=>-4,'msg'=>'添加水印失败');
} switch($this->_imageInfo['type'])
{
case IMAGETYPE_PNG://
$this->_resImage = @imagepng($this->_resImage, $this->_srcImage);
break;
case IMAGETYPE_JPEG://
$this->_resImage = @imagejpeg($this->_resImage, $this->_srcImage);
break;
case IMAGETYPE_GIF://
$this->_resImage = @imagegif($this->_resImage, $this->_srcImage);
break;
default:
return array('error'=>-2,'msg'=>'文件类型不正确');
}
@imagedestroy($this->_resImage);
return array('error'=>1,'msg'=>'ok');
}
}
?>
PHP水印类的更多相关文章
- pdo文字水印类,验证码类,缩略图类,logo类
文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...
- PHP 图片水印类
<?php /** * 加水印类,支持文字图片水印的透明度设置.水印图片背景透明. * $obj = new WaterMask($imgFileName); //实例化对象 * $obj-&g ...
- 不错.net图片水印类
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...
- C#图片水印类
这个是学习用的呃,主要看一下水印在修改图片中距左边的宽度和高度是杂弄的就哦客了. using System; using System.Collections.Generic; using Syste ...
- PHP学习之图像处理-水印类
<?php $image = new Image(); $image->water('./upload/up_5cf0caca0565b.png','./upload/up_5cf0cb3 ...
- 逆天通用水印支持Winform,WPF,Web,WP,Win10。支持位置选择(9个位置 ==》[X])
常用技能:http://www.cnblogs.com/dunitian/p/4822808.html#skill 逆天博客:http://dnt.dkil.net 逆天通用水印扩展篇~新增剪贴板系列 ...
- PHP加水印代码 支持文字和图片水印
PHP加图片水印.文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置.水印图片背景透明.自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有 ...
- winform工具1-图片去除水印
效果图: 思路: 1.获取图片 2.处理水印 3.保存处理的图片 代码: 获取图片: private void button1_Click(object sender, EventArgs e) { ...
- PHP给图片添加文字水印实例
PHP给图片添加文字水印实例,支持中文文字水印,是否覆盖原图,自定义设置水印背景色.文字颜色.字体等. 水印类water.class.php var $Path = "./"; / ...
随机推荐
- 基于吉日嘎底层架构的Web端权限管理操作演示-用户管理
相信博客园的朋友对吉日嘎拉都不陌生,相信很多人也买了他的源码,应用于自己的项目. 但是你有没有过一个困惑? 那就是:没有一个基于网页的权限管理界面. 今天,这一切都不再是问题,我花了3年时间研究学习并 ...
- 【jQuery基础学习】05 jQuery与Ajax以及序列化
好吧,这章不像上章那么水了,总是炒剩饭也不好. 关于AJAX 所谓Ajax,全名Asynchronous JavaScript and XML.(也就异步的JS和XML) 简单点来讲就是不刷新页面来发 ...
- 与众不同 windows phone 8.0 & 8.1 系列文章索引
[源码下载] [与众不同 windows phone 7.5 (sdk 7.1) 系列文章索引] 与众不同 windows phone 8.0 & 8.1 系列文章索引 作者:webabcd ...
- Oracle备份表结构和数据
--创建一份表结构 create table BASE_GOODSPAYMENT_SETTING_BAK as select * from BASE_GOODSPAYMENT_SETTING ; -- ...
- yii2.0用户登录,退出判断(摘录)
文章来源:http://blog.sina.com.cn/s/blog_88a65c1b0101ix13.html 判断用户是否登录 在 Yii2.0 里面,判断用户是否已经登录,我们用下面的代码即可 ...
- [翻译]:SQL死锁-为什么会出现死锁
下面这篇对理解死锁非常重要,首先死锁是如何产生的我们要清楚. We already know why blocking occurs in the system and howto detect an ...
- [TypeScript] TypeScript对象转JSON字符串范例
[TypeScript] TypeScript对象转JSON字符串范例 Playground http://tinyurl.com/njbrnrv Samples class DataTable { ...
- [Tool] 使用CodeMaid自动程序排版
[Tool] 使用CodeMaid自动程序排版 前言 「使用StyleCop验证命名规则」这篇文章,指引开发人员透过StyleCop这个工具,来自动检验项目中产出的程序代码是否合乎命名规则. [Too ...
- Vue自带的过滤器
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05 一 过滤器写法 {{ message | Filter}} 二 ...
- HTML5原生拖放实例分析
HTML5提供了原生拖放功能的JavaScript API,使用起来很方便. 兼容性: 对于PC端浏览器,Firefox.Chrome.Safari支持良好,而IE和Edge浏览器有些特性不支持,如I ...