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 = "./"; / ...
随机推荐
- Equeue初识
详细解说: http://www.cnblogs.com/netfocus/p/3595410.html 简单代码用法: Producer 端代码用法实例 和 Customer 端代码用法示例: ht ...
- 静默安装、授权及卸载Microsoft SQL Server、NET Framework、Windows Installer 、ArcGIS License Manager、ArcGIS Engine(Silent install、uninstall and Authorization.. .through Setup Factory)基于Setup Factory
通过Setup Factory写的代码大概有1700行,所以就不整理了.思路如下: 静默安装都是通过去Microsoft 和Esri的官网找到静默安装的命令,然后File.Run(...)或者Shel ...
- git 使用笔记(一)
1. 环境介绍 windows10 2.使用 2.1 安装git for windows 2.2 创建一个文件夹, 开始git管理 2.3 查看该目录,包括隐藏文件 2.4 把testgit.txt添 ...
- html button自动提交表单问题
在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit,所以在chrome中,需要使用<button type="butt ...
- docker 使用
https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-docker-application http ...
- swift学习笔记之-自动引用计数
//自动引用计数 import UIKit /*自动引用计数(Automatic Reference Counting) 防止循环强引用 Swift 使用自动引用计数(ARC)机制来跟踪和管理你的应用 ...
- javascript宿主对象之window.navigator
window.navigator用来反映浏览器及其功能信息的对象. // 检测浏览器版本信息 function getBrowserInfo(){ var Sys = {}; var ua = win ...
- Android 带清除功能的输入框控件EditText
1.效果图 2.源码下载 http://download.csdn.net/detail/yanzi2015/8864603 3.相关博客 http://www.cnblogs.com/to ...
- [leetcode] Reverse Bits
Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...
- Static Cell-静态TableView
使用静态TableView有两个前提,1.要在Storyboard上 2.需要使用TableViewController PS:如果需要设置不同的cell的高度不同的话,还是需要使用tableVie ...