ImageWatermark.php


<?php
/***********************************************************
类名:ImageWatermark
功能:用于生成图片或文字水印
WDPHP素材源码 http://www.wdphp.com
************************************************************
合成水印:
1、图像水印appendImageMark(暂不可旋转)
2、文字水印appendTextMark(汉字水印需要设置汉字字体)(可旋转)
输出水印图像:write($filename=null)
1、输出到文件:指定$filename参数为输出的文件名。
2、输出到浏览器:不指定输出文件名,则输出到浏览器.
指定水印位置:
1、指定位置类型$markPosType:(default-0)
1-top left 2-top center 3-top right
4-middle left 5-middle center 6-middle right
7-bottom left 8-bottom center 9-bottom right
0-random
2、设置具体位置setMarkPos($x,$y),若指定具体位置,则上面的位置类型无效。
************************************************************
*/
class ImageWatermark {
public $markPosType = 0; //水印位置,缺省为随机位置输出水印
public $fontFile = 'arial.ttf'; //字体文件名
public $color = '#CCCCCC'; //水印字体的颜色
public $fontSize = 12; //水印字体大小
public $angle = 0; //水印文字旋转的角度
private $markPos = array();
private $markImageFile = null, $destImageFile = null;
private $mark_res = null, $mark_width = 0, $mark_height = 0, $mark_type = null;
private $dest_res = null, $dest_width = 0, $dest_height = 0, $dest_type = null;
//用目标图片作为构造函数的参数
public function __construct($destImage) {
if (!file_exists($destImage)) return false;
$this->destImageFile = $destImage;
//获取图片大小、类型
$imageInfo = getimagesize($this->destImageFile);
$this->dest_width = $imageInfo[0];
$this->dest_height = $imageInfo[1];
$this->dest_type = $imageInfo[2];
//得到图片资源句柄
$this->dest_res = $this->getImageResource($this->destImageFile, $this->dest_type);
}
public function __destruct() {
imagedestroy($this->dest_res);
}
//添加文字水印
public function appendTextMark($markText) {
if ($markText == null) return false;
//计算水印文本的大小
$box = imagettfbbox($this->fontSize, $this->angle, $this->fontFile, $markText);
$this->mark_width = $box[2] - $box[6];
$this->mark_height = $box[3] - $box[7];
//计算水印位置
$pos = ($this->markPos != null) ? $this->markPos : $this->getMarkPosition($this->markPosType);
$pos[1]+= $this->mark_height;
//将文字打印到图片上
$RGB = $this->colorHexRgb($this->color);
$imageColor = imagecolorallocate($this->dest_res, $RGB[0], $RGB[1], $RGB[2]);
imagettftext($this->dest_res, $this->fontSize, $this->angle, $pos[0], $pos[1], $imageColor, $this->fontFile, $markText);
}
//添加图片水印
public function appendImageMark($markImage) {
if (!file_exists($markImage)) return false;
$this->markImageFile = $markImage;
//获取水印图片大小、类型
$imageInfo = getimagesize($this->markImageFile);
$this->mark_width = $imageInfo[0];
$this->mark_height = $imageInfo[1];
$this->mark_type = $imageInfo[2];
//得到图片资源句柄
$this->mark_res = $this->getImageResource($this->markImageFile, $this->mark_type);
//计算水印位置
$pos = ($this->markPos != null) ? $this->markPos : $this->getMarkPosition($this->markPosType);
//设置图像混色模式
imagealphablending($this->dest_res, true);
//复制叠加图像
imagecopy($this->dest_res, $this->mark_res, $pos[0], $pos[1], 0, 0, $this->mark_width, $this->mark_height);
imagedestroy($this->mark_res);
}
//将叠加水印后的图片写入指定文件,若不定文件名,则输出到浏览器
public function write($filename = null) {
$this->writeImage($this->dest_res, $filename, $this->dest_type);
}
//设置水印x,y坐标
public function setMarkPos($x, $y) {
$this->markPos[0] = $x;
$this->markPos[1] = $y;
}
//将十六进制的颜色值分解成RGB形式
private function colorHexRgb($color) {
$color = preg_replace('/#/', '', $color);
$R = hexdec($color[0] . $color[1]);
$G = hexdec($color[2] . $color[3]);
$B = hexdec($color[4] . $color[5]);
return array(
$R,
$G,
$B
);
}
//计算水印位置
private function getMarkPosition($type = 0) {
switch ($type) {
case 0:
$x = rand(0, $this->dest_width - $this->mark_width);
$y = rand(0, $this->dest_height - $this->mark_height);
break; //random case 1:
$x = 0;
$y = 0;
break; //topleft case 2:
$x = ($this->dest_width - $this->mark_width) / 2;
$y = 0;
break; //topcenter case 3:
$x = $this->dest_width - $this->mark_width;
$y = 0;
break; // topright case 4:
$x = 0;
$y = ($this->dest_height - $this->mark_height) / 2;
break; //middleleft case 5:
$x = ($this->dest_width - $this->mark_width) / 2;
$y = ($this->dest_height - $this->mark_height) / 2;
break; //middlecenter case 6:
$x = $this->dest_width - $this->mark_width;
$y = ($this->dest_height - $this->mark_height) / 2;
break; //middleright case 7:
$x = 0;
$y = $this->dest_height - $this->mark_height;
break; //bottomleft case 8:
$x = ($this->dest_width - $this->mark_width) / 2;
$y = $this->dest_height - $this->mark_height;
break; //bottomcenter case 9:
$x = $this->dest_width - $this->mark_width;
$y = $this->dest_height - $this->mark_height;
break; //bottomright default:
$x = rand(0, $this->dest_width - $this->mark_width);
$y = rand(0, $this->dest_height - $this->mark_height);
break; //random }
return array(
$x,
$y
);
}
//从一个图像文件中取得图片资源标识符
private function getImageResource($filename, $type = 0) {
switch ($type) {
case 1:
return imagecreatefromgif($filename);
break; case 2:
return imagecreatefromjpeg($filename);
break; case 3:
return imagecreatefrompng($filename);
break;
// 以后可添加其它格式 default:
return null;
}
}
//将图像写入文件或输出到浏览器
private function writeImage($ImageRes, $filename = null, $type = 0) {
switch ($type) {
case 1:
imagegif($ImageRes, $filename);
break; case 2:
imagejpeg($ImageRes, $filename);
break; case 3:
imagepng($ImageRes, $filename);
break; default:
return null;
}
return true;
}
}
//使用示例
$markimg = new ImageWatermark('c_si.jpg');
//$markimg->setMarkPos(100,200);//如何设置setMarkPos,则markPosType无效。
$markimg->markPosType = 5;
$markimg->appendImageMark('mark.png');
$markimg->fontFile = 'STCAIYUN.TTF';
$markimg->color = '#FFFFFF';
$markimg->fontSize = 24;
$markimg->angle = 45; //设置角度时,注意水印可能旋转出目标图片之外。
$markimg->appendTextMark('汉字水印');
$markimg->write();
$markimg = null;
?>

PHP的生成图片或文字水印的类的更多相关文章

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

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

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

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

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

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

  4. asp .net 为图片添加文字水印(内包含有加图片水印的方法) .

    在项目中先创建一个Imag_writer 类库 在该类库下分别创建两个枚举类型WaterMarkType (水印的类型).WaterMarkPosition (水印的位置).代码如下: using S ...

  5. 图片水印工具类java

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

  6. java创建透明背景的PNG图片加自定义文字水印

    人在码上走,需求天天有.这不,今天前端让我返回一个带自定义水印的背景图片.一通google,有现成的代码,但是基本是直接在源图上添加水印,生成出来的文字样式也没有控制好,看来又只有自己造轮子了. 过程 ...

  7. Asp.net 上传图片添加半透明图片或者文字水印的方法

    主要用到System.Drawing 命名空间下的相关类,如Brush.Image.Bitmap.Graphics等等类 Image类可以从图片文件创建Image的实例,Bitmap可以从文件也可以从 ...

  8. 利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现

    1: 面向过程的编写方法 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image ...

  9. Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)

    想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...

随机推荐

  1. 语义分割--全卷积网络FCN详解

    语义分割--全卷积网络FCN详解   1.FCN概述 CNN做图像分类甚至做目标检测的效果已经被证明并广泛应用,图像语义分割本质上也可以认为是稠密的目标识别(需要预测每个像素点的类别). 传统的基于C ...

  2. win10 安装face_recongnition

    1.安装dlib https://stackoverflow.com/questions/41912372/dlib-installation-on-windows-10 2.安装face_recon ...

  3. 系统性能信息模块psutil

    目录 前言 获取系统性能信息 CPU 内存 磁盘 网络信息 其他系统信息 系统进程管理方法 进程信息 popen类 查看系统硬件的小脚本 前言 psutil 是一个跨平台库,能够轻松实现获取系统运行的 ...

  4. 在ubuntu中安装phpstorm

    安装参考网址 https://blog.csdn.net/mrgong_/article/details/77200225 注意:不需要安装java相关 激活phpstorm 2018 参考网址:ht ...

  5. extern “C”的用法

    引言 由于不同的代码互相调用起来很容易出错,甚至同一种代码但由不同的编译器编译,为实现C++代码调用其他C语言代码,会在C语言代码的部分加上extern "C",表明这段代码需要按 ...

  6. Activiti实战04_简单流程

    在Activiti实战03_Hello World中我们介绍了一个中间没有任何任务的流程,实现了流程的部署与查阅,而在本章中,将会为流程添加任务节点,是流程能够像个流程,变得更加丰满起来. 在上一节的 ...

  7. java 异常捕获小记

    java 中异常捕获常用的为: try{ //业务代码 }catch(Exception e){ //异常捕获 }finally{ // 不管有无异常, 最后都会执行到这里 } 在方法体内如果想要把异 ...

  8. IO流17 --- 对象流操作自定义对象 --- 技术搬运工(尚硅谷)

    序列化 @Test public void test14() throws IOException { ObjectOutputStream oos = new ObjectOutputStream( ...

  9. i\'ll make a man out of you

    Let's get down to business To defeat the Huns Did they send me daughters When I asked for sons? You' ...

  10. JavaScript对象继承方式

    一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Parent 构造函数 成为 Children 的方法, ...