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 = "./"; / ...
随机推荐
- 利用mciSendString播放音频
最近在写音频播放器,不过有点懒散,开发进度很慢,一天只做了一点点东西.其实就是让程序能播放音频.这个在我大二学winform程序开发时书上有说,那是书上教的是用media player的COM组件,而 ...
- 【循序渐进学Python】1. Python基础知识
1. Python安装和配置 首先需要到Python的官方网站(http://www.python.org/getit/) 下载安装包,现在Python的发行版分为兼容之前Python程序的Pytho ...
- 小巧方便的MVC后端验证码,供大家学习借鉴
调用: public ActionResult Vcode()//验证码 { string code = ValidateCode.CreateRandomCode(4); ValidateCode. ...
- Unity3D脚本语言UnityScript初探
译者注: Unity3D中支持三种语言:JavaScript.C#.Boo,很多人不知道如何选择,通过这篇译文,我们可以搞清楚这三者语言的来龙去脉,对选择主语言有一定的借鉴意义. 首先,Unity是基 ...
- java设计模式--工厂模式
所谓工厂,就是要造产品,比如一个小型砖瓦厂,只有一个窑,既能造砖又能造瓦,这种叫简单工厂模式.规模稍大一点呢,我多个窑,有的窑专门造砖,有的窑专门造瓦,想造别的,比如瓷砖,我再用个专门的窑,这种 ...
- 使用PreparedStatement执行SQL语句时占位符(?)的用法
1.Student数据库表 ID name gender 2.Java代码 public static void main(String[] args) { int _id=1; Str ...
- Json Utils
import java.util.List;import java.util.Map; import net.sf.json.JSONArray;import net.sf.json.JSONObje ...
- j2ee log4j集中式日志解决方案logpool-v0.4发布说明
logpool v0.4发布说明: 1.持久化采用mongodb非结构化存储实现,以满足后续调整的灵活需要:
- 【GOF23设计模式】组合模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...
- javascript小知识点
大家对input中的value值研究的透彻么,今天看到一篇博客,很神奇 然后研究了一下input中的value值到底对应的是啥值 1.input中的value,这是大家在开发中进场遇到的一个问题 & ...