<?php
/**
* 加水印类,支持文字图片水印的透明度设置、水印图片背景透明。
* $obj = new WaterMask($imgFileName); //实例化对象
* $obj->$waterType = 1; //类型:0为文字水印、1为图片水印
* $obj->$transparent = 45; //水印透明度
* $obj->$waterStr = 'www.itwhy.org'; //水印文字
* $obj->$fontSize = 16; //文字字体大小
* $obj->$fontColor = array(255,0255); //水印文字颜色(RGB)
* $obj->$fontFile = = 'AHGBold.ttf'; //字体文件
* $obj->output(); //输出水印图片文件覆盖到输入的图片文件
*/
class WaterMask{
public $waterType = ; //水印类型:0为文字水印、1为图片水印
public $pos = ; //水印位置
public $transparent = ; //水印透明度 public $waterStr = 'www.itwhy.org'; //水印文字
public $fontSize = ; //文字字体大小
public $fontColor = array(,,); //水印文字颜色(RGB)
public $fontFile = 'AHGBold.ttf'; //字体文件 public $waterImg = 'logo.png'; //水印图片 private $srcImg = ''; //需要添加水印的图片
private $im = ''; //图片句柄
private $water_im = ''; //水印图片句柄
private $srcImg_info = ''; //图片信息
private $waterImg_info = ''; //水印图片信息
private $str_w = ''; //水印文字宽度
private $str_h = ''; //水印文字高度
private $x = ''; //水印X坐标
private $y = ''; //水印y坐标 function __construct($img) { //析构函数
$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
}
private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。
$this->srcImg_info = getimagesize($this->srcImg);
switch ($this->srcImg_info[]) {
case :
$this->im = imagecreatefrompng($this->srcImg);
break ;
case :
$this->im = imagecreatefromjpeg($this->srcImg);
break ;
case :
$this->im = imagecreatefromgif($this->srcImg);
break ;
default:
die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
}
}
private function waterimginfo() { //获取水印图片的信息,并载入图片。
$this->waterImg_info = getimagesize($this->waterImg);
switch ($this->waterImg_info[]) {
case :
$this->water_im = imagecreatefrompng($this->waterImg);
break ;
case :
$this->water_im = imagecreatefromjpeg($this->waterImg);
break ;
case :
$this->water_im = imagecreatefromgif($this->waterImg);
break ;
default:
die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
}
}
private function waterpos() { //水印位置算法
switch ($this->pos) {
case : //随机位置
$this->x = rand(,$this->srcImg_info[]-$this->waterImg_info[]);
$this->y = rand(,$this->srcImg_info[]-$this->waterImg_info[]);
break ;
case : //上左
$this->x = ;
$this->y = ;
break ;
case : //上中
$this->x = ($this->srcImg_info[]-$this->waterImg_info[])/;
$this->y = ;
break ;
case : //上右
$this->x = $this->srcImg_info[]-$this->waterImg_info[];
$this->y = ;
break ;
case : //中左
$this->x = ;
$this->y = ($this->srcImg_info[]-$this->waterImg_info[])/;
break ;
case : //中中
$this->x = ($this->srcImg_info[]-$this->waterImg_info[])/;
$this->y = ($this->srcImg_info[]-$this->waterImg_info[])/;
break ;
case : //中右
$this->x = $this->srcImg_info[]-$this->waterImg_info[];
$this->y = ($this->srcImg_info[]-$this->waterImg_info[])/;
break ;
case : //下左
$this->x = ;
$this->y = $this->srcImg_info[]-$this->waterImg_info[];
break ;
case : //下中
$this->x = ($this->srcImg_info[]-$this->waterImg_info[])/;
$this->y = $this->srcImg_info[]-$this->waterImg_info[];
break ;
default: //下右
$this->x = $this->srcImg_info[]-$this->waterImg_info[];
$this->y = $this->srcImg_info[]-$this->waterImg_info[];
break ;
}
}
private function waterimg() {
if ($this->srcImg_info[] <= $this->waterImg_info[] || $this->srcImg_info[] <= $this->waterImg_info[]){
die('水印比原图大!');
}
$this->waterpos();
$cut = imagecreatetruecolor($this->waterImg_info[],$this->waterImg_info[]);
imagecopy($cut,$this->im,,,$this->x,$this->y,$this->waterImg_info[],$this->waterImg_info[]);
$pct = $this->transparent;
imagecopy($cut,$this->water_im,,,,,$this->waterImg_info[],$this->waterImg_info[]);
imagecopymerge($this->im,$cut,$this->x,$this->y,,,$this->waterImg_info[],$this->waterImg_info[],$pct);
}
private function waterstr() {
$rect = imagettfbbox($this->fontSize,,$this->fontFile,$this->waterStr);
$w = abs($rect[]-$rect[]);
$h = abs($rect[]-$rect[]);
$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,,,,);
imagefill($this->water_im,,,$white_alpha);
$color = imagecolorallocate($this->water_im,$this->fontColor[],$this->fontColor[],$this->fontColor[]);
imagettftext($this->water_im,$this->fontSize,,,$this->fontSize,$color,$this->fontFile,$this->waterStr);
$this->waterImg_info = array(=>$w,=>$h);
$this->waterimg();
}
function output() {
$this->imginfo();
if ($this->waterType == ) {
$this->waterstr();
}else {
$this->waterimginfo();
$this->waterimg();
}
switch ($this->srcImg_info[]) {
case :
imagepng($this->im,$this->srcImg);
break ;
case :
imagejpeg($this->im,$this->srcImg);
break ;
case :
imagegif($this->im,$this->srcImg);
break ;
default:
die('添加水印失败!');
break;
}
imagedestroy($this->im);
imagedestroy($this->water_im);
}
}
?>

PHP 图片水印类的更多相关文章

  1. 不错.net图片水印类

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...

  2. C#图片水印类

    这个是学习用的呃,主要看一下水印在修改图片中距左边的宽度和高度是杂弄的就哦客了. using System; using System.Collections.Generic; using Syste ...

  3. 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  4. java常用开发工具类之 图片水印,文字水印,缩放,补白工具类

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  5. php使用GD库实现图片水印和缩略图——封装成类

    学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...

  6. PHP的图片处理类(缩放、加图片水印和剪裁)

    <!--test.php文件内容--> <?php //包含这个类image.class.php include "image.class.php"; $img ...

  7. 图片水印工具类java

    关于jar包的导入我就不多说了,我会把引入的jar包显示出来,大家自行Google package com.net.util; import java.awt.AlphaComposite; impo ...

  8. pdo文字水印类,验证码类,缩略图类,logo类

    文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...

  9. PHP水印类

    <?php /** * 水印类 * @author zhaoyingnan 2015/07/16 **/ include_once('extend.php'); class Watermark_ ...

随机推荐

  1. --hdu 2124 Repair the Wall(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...

  2. centos7 nginx用systemctl方式管理的脚本

    vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Doc ...

  3. linux 优化&安全运维&黑客攻防

    优化: 可删除用户:adm,lp,sync,shutdown,halt,news,uucp,operator,games,gopher.   :userdel games 可删除组:adm,lp,ne ...

  4. 使用SqlSessionTemplate实现数据库的操作

    EmployeeMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE map ...

  5. 转:Java NIO系列教程(一)Java NIO 概述

    Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Sel ...

  6. VS自带WCF测试客户端简单介绍

    在目前的二次开发项目中,一些信息是放在客户那里的,只给你一个服务地址,不知道具体有什么方法,每次想调用一个服务不知道能不能实现目前的需求,只能测试.写个测试程序真的划不来,占用时间不说,而且你忙了一上 ...

  7. 使用Fabric进行crash收集统计

    主要是帮助自己记一下地址. 1 申请Crashlytics服务:http://try.crashlytics.com 2 下载Fabric客户端,帮助集成Crashlytics到自己的项目中:http ...

  8. Sqlserver日期函数应用

    1.获取当前时间 SELECT  GETDATE() AS '当前日期' ,         DATENAME(year, GETDATE()) AS '年' ,         DATENAME(m ...

  9. Windows下Cygwin添加右键菜单

    在http://herry2013git.blog.163.com/blog/static/2195680112013437139447/看到一篇文章,将Cypwin加入右键菜单,方便使用. 为了更傻 ...

  10. 在VMware上面安装Solaris 10

    导读 Oracle Solaris 11 是世界上最先进的企业操作系统,提供安全.速度.简单的企业云环境和DevOps.在这篇文章中我们将使用Solaris 10版本,但您可以按照同样的步骤,来安装刚 ...