PHP 图片水印类
- <?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 图片水印类的更多相关文章
- 不错.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 ...
- 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- java常用开发工具类之 图片水印,文字水印,缩放,补白工具类
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- php使用GD库实现图片水印和缩略图——封装成类
学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...
- PHP的图片处理类(缩放、加图片水印和剪裁)
<!--test.php文件内容--> <?php //包含这个类image.class.php include "image.class.php"; $img ...
- 图片水印工具类java
关于jar包的导入我就不多说了,我会把引入的jar包显示出来,大家自行Google package com.net.util; import java.awt.AlphaComposite; impo ...
- pdo文字水印类,验证码类,缩略图类,logo类
文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...
- PHP水印类
<?php /** * 水印类 * @author zhaoyingnan 2015/07/16 **/ include_once('extend.php'); class Watermark_ ...
随机推荐
- hdu 5747 Aaronson
T : 1 n m: 10 2 题解:20 * 0 + 21* 1 + 22* 2 = 10 输出:3 <-- 0+1+2=3 AC 代码: #include<stdio ...
- java gc的工作原理、如何优化GC的性能、如何和GC进行有效的交互
java gc的工作原理.如何优化GC的性能.如何和GC进行有效的交互 一个优秀的Java 程序员必须了解GC 的工作原理.如何优化GC的性能.如何和GC进行有效的交互,因为有一些应用程序对性能要求较 ...
- matlab怎么定义一个数组
A=[];n=input('n=');%数组的长度for i=1:n fprintf('a%.0f=',i); x=input('');%分别输入各个数的值 A=[A,x];endA就可以得到长度为n ...
- 畅通工程再续(MST)
畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Android--UI之EditText
前言 上一篇博客介绍了Android的TextView控件,这篇博客来说一下EditText控件.EditText为一个文本控件,提供了文本输入的功能,而且继承自TextView,可以理解为可以输入的 ...
- Android控件之圆形Button
bg_circle.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns: ...
- 反正切函数求圆周率 atan
#define PI atan(1.0)*4 原理:tan ∏/4=1; atan2: 返回给定的 X 及 Y 坐标值的反正切值.反正切的角度值等于 X 轴正方向与通过原点和给定坐标点 (Y坐标, X ...
- C语言 单引号和双引号的区别
最近的C语言课在教字符串,貌似N多同学搞不清楚单引号和双引号的区别,有人还以为在C语言里用哪个都可以...其实C语言中的单引号和双引号含义是一点也不一样滴... 1.含义不同. 用单引号引起的一个字符 ...
- Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- hadoop(一):深度剖析hdfs原理
在配置hbase集群将 hdfs 挂接到其它镜像盘时,有不少困惑的地方,结合以前的资料再次学习; 大数据底层技术的三大基石起源于Google在2006年之前的三篇论文GFS.Map-Reduce. ...