<?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. Apache配置默认首页

    操作系统:CentOS 6.5 Apache默认主页为index.html,如果要修改为index.php或其它,需要修改httpd.conf文件 用vim或其它编辑器打开httpd.conf 在上图 ...

  2. Struts2几种传值

    1.url向action传值 url为 http://localhost/txyl/teacher_info?method:teacher_info&teacher_seq=dedafdsf3 ...

  3. C# 检测操作系统是否空闲,实现系统空闲后做一些操作

    public class CheckComputerFreeState { /// <summary> /// 创建结构体用于返回捕获时间 /// </summary> [St ...

  4. heap和stack有什么区别

    1.heap是堆,stack是栈. 2.stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的,heap常用new关键字来分配. 3.stack空间有限,heap的空间是很大的自由 ...

  5. PHP 500 -Invalid command RewriteEngine的解决

    转自:http://blog.csdn.net/wang02011/article/details/8205903 环境:   wampserver-2.1a 系统 :  win8 错误 :  500 ...

  6. $key 的用法

    <?php $attr=array("a","b","c","d"); //$key,默认是主键值,$value, ...

  7. 完善dedecms站内搜索代码,为搜索结果添加第*页

    自那些平凡而伟大的程序猿开发了内容管理系统(cms),为了让看客们更快地找到自己感兴趣的内容,他们不断完善站内搜索代码,形成了一个小型的站内搜索引擎.可能有些网站模板设计师没考虑到seo的问题,很多站 ...

  8. 关于NGUI的动态加载后的刷新显示问题,解决办法!!

    http://momowing.diandian.com/post/2012-09-06/40038001275 最近碰NGUI用到它的动态列表功能(ps:就是加东西,删除东西).我这里用的是UIDr ...

  9. HDOJ 1590

    #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> u ...

  10. jekyll中文乱码问题

    一般编写都是采用utf-8的吧,但是在windows下安装的jekyll,默认是以GBK编码的方式去读取咱们编写的文件,如此便乱码了. 要解决此问题,总不至于要写GBK编码的文件吧,毕竟这个编码不怎么 ...