写了一个给图片加水印的php类。
时间:2007-01-18
来源:互联网
代码:
<?/**
* 为图片加水印 .
* 本类只能给图片添加文字水印.
* 本类的开发环境为php5,gd2库.如果低于php5和gd2库的.可能导致本程序无法执行.
* 另外,为了使用 exif_imagetype 函数来判断图片类型,请打开php.ini中的 extension=php_mbstring.dll 和 extension=php_exif.dll
* 并保证, extension=php_mbstring.dll 在 extension=php_exif.dll 之前.....不知道为什么..
* @author [email protected]
* @version 1.0
*/
class TextWatermarkTool {
//在图片上面加文字时,采用的字体的路径(只能使用truetype字体。后缀名为TTF的字体)
var $f;
//字体大小
var $fontSize = 12;
//文字的角度
var $textAngle = -45;
//需要加文字的图片的宽度和高度。根据读取出来的图象自动获取
var $imgWidth = 1;
var $imgHeight = 1;
//当从imgUrl读取图象失败的时候,默认创建图象的长度和宽度
var $DefImgWidth = 250;
var $DefImgHeight = 150;
//输出的图象的长度和宽度
var $outWidth = 0;
var $outHeight = 0;
//输出图象的质量,0 - 100
var $outImgQuality = 75;
//需要处理的图象的地址
var $imgUrl = "";
//需要往上述图象上添加的文字
var $text = "";
//上述文字的颜色
var $textColor = "0,0,0";
//文字的底色:注如果使用底色的话。
var $textBarColor = "255,255,255";
//标识是否为文字绘制背景栏
var $paintBar = true;
//文字背景栏的高度
var $barHeight = 1;
//文字背景栏的宽度,默认为图片宽度
var $barWidth = 1;//$imgWidth;
//设置文字背景栏的起始x坐标
var $barX = 0;
//设置文字背景的起始Y坐标
var $barY = 0;
//设置文字背景栏是否渐变
var $barGradual = true;
//背景栏的透明度
var $barAlpla = 75;
//上述文字的透明度,0为不透明,127为完全透明
var $alpha = 75;
//上述文字的x轴的位置
var $textX = 10;
//上述文字y轴位置
var $textY = 10;
//当加载由$fontName指定的字体文件失败时,使用的默认字体
var $defFont = 5;
//需要处理的图片文件的格式
var $imgFormat = "jpeg";
//输出的图片格式
var $outFormat = "jpeg";
//当把图片保存到磁盘时的文件路径
//var $imgSavePath = ".";
//当把图片保存到磁盘时,所使用的文件名
var $imgSaveName = "Nonam";//.$outFormat;
//根据$imgUrl 读取出来的image数据文件,类型为php的resource类
var $image = NULL;
//输出类型:文件:file或者浏览器:browser
var $outType = "browser";
var $JPEG = "jpeg";
var $JPG = "jpg";
var $PNG = "png";
var $GIF = "gif";
var $BMP = "bmp";
var $WBMP = "wbmp";
var $XBM = "xbm";
var $XPM = "xpm";
var $OUTTOFILE = "file";
var $OUTTOBROWSER = "borwser";
/**
* 设置水印文字所采用的字体路径
* @param $path 一个ttf字体所在的路径(包含字体名字)
*
*/
function setFontName ($path) {
$this->fontName = $path;
}
/**
* 获取水印文字的字体
* @return String - 字体路径,包含字体文件名
*
*/
function getFontName () {
return $this->fontName;
}
/**
* 设置输出文件的类型,分 file 和 browser 两种
* 当设置为file时,文件会保存到本地磁盘,设置为browser时,会直接输出到浏览器
* @param $o 输出的类型.只有两种值:file和browser
*
*/
function setOutType ($o) {
if (strtolower($o) !== $this->OUTTOFILE && strtolower($o) != $this->OUTTOBROWSER)
$o = $this->OUTTOBROWSER;
$this->outType = $o;
}
/**
* 获取输出文件类型
* @return String - 输出图片文件的类型.为file和browser之一
*
*/
function getOutType () {
return $this->outType;
}
/**
* 设置文字底色栏的透明度
* @param $alpha int , 文字底色栏的透明度.取值为 0 - 127
* 0表示完全不透明,127表示完全透明
*
*/
function setBarAlpha ($alpha) {
if ($alpha < 0 || $alpha > 127)
$alpha = 75;
$this->barAlpha = $alpha;
}
/**
* 获取文字底色栏的透明度
* @return int - 文字底色栏的透明度
*/
function getBarAlpha () {
return $this->barAlpha;
}
/**
* 设置输出图片文件的质量. 只有当输出图片格式为jpg的时候有效.
* @param $i , int 输出的图片文件的质量.取值为 0 - 100
* 数字越大,质量越高
*
*/
function setOutImgQuality ($i) {
if ($i < 0 || $i > 100)
$i = 75;
$this->outImgQuality = $i;
}
/**
* 获取输出图片的质量
* @return int 图片的质量
*
*/
function getOutImgQuality () {
return $this->outImgQuality;
}
/**
* 设置文字的倾斜角度
* @param $a , int 文字的旋转角度
*
*/
function setTextAngle ($a) {
$this->textAngle = $a;
}
/**
* 获取文本的旋转角度
* @return int , 文本的旋转角度
*
*/
function getTextAngle () {
return $this->textAngle;
}
/**
* 设置文本所采用的字体大小
* @param $size - int , 文本所采用的字体大小
*
*/
function setFontSize ($size) {
if ($size <= 0 )
$size = 12;
$this->fontSize = $size;
}
/*
* 获取文本所采用的字体大小
* @return int , 文本所采用的字体大小
*/
function getFontSize () {
return $this->fontSize;
}
/**
* 设置输出图片的宽度
* @param $w , int 输出图片的宽度
*
*/
function setOutWidth ($w){
if ($w < 0)
$w = $this->getImgWidth();
$this->outWidth = $w;
}
作者: aceyin 发布时间: 2007-01-18
代码:
/*** 获取输出图片的宽度
* @return 输出图片的宽度
*
*/
function getOutWidth () {
return $this->outWidth;
}
/**
* 设置输出图片的高度
* @param $h , int 输出图片的高度
*
*/
function setOutHeight ($h) {
if ($h <= 0 )
$h = $this->getImgHeight();
$this->outHeight = $h;
}
/**
* 获取输出图片的高度
* @return 输出图片的宽度
*
*/
function getOutHeight () {
return $this->outHeight;
}
/**
* 设置要加水印的图片的地址.可以为硬盘图片文件的地址,也可以为网络图片的网址
* @param $url String , 图片的地址.
*
*/
function setImgUrl ($url) {
$this->imgUrl = $url;
}
/**
* 获取图片的地址
* @return String 图片的地址
*
*/
function getImgUrl () {
return $this->imgUrl;
}
/**
* 设置要加到图片上的水印文字内容
* @param $t String 要加到图片上的水印文字内容
*
*/
function setText ($t) {
$this->text = $t;
}
/**
* 获取要加到图片上的水印文字内容
* @return String 要加到图片上的水印文字内容
*
*/
function getText () {
return $this->text;
}
function getImgWidth () {
return $this->imgWidth;
}
function getImgHeight () {
return $this->imgHeigh;
}
/**
* 设置文字底色栏的起始的x坐标
* @param $t int
*
*/
function setBarX ($t) {
//echo "<BR>barX: *** ".$this->getImgWidth()."-".$this->getBarWidth()."=".($this->getImgWidth() - $this->getBarWidth());
if ($t <= 0 || $t >= $this->getImgWidth() - $this->getBarWidth())
$t = 0;
$this->barX = $t;
//echo $this->barX;
}
/**
* 获取文字底色栏的起始X坐标
* @return int - 文字底色栏的起始X坐标
*/
function getBarX () {
return $this->barX;
}
/**
* 设置文字底色栏的起始Y坐标
* @param $t int
*
*/
function setBarY ($t) {
//echo "<BR>barY: *** ".($this->getImgHeight() - $this->getBarHeight());
if ($t <= 0 || $t >= $this->getImgHeight() - $this->getBarHeight())
$t = 0;
$this->barY = $t;
//echo $this->barY;
}
/**
* 获取文字底色栏的起始Y坐标
* @return int - 文字底色栏的起始Y坐标
*/
function getBarY () {
return $this->barY;
}
/**
* 设置文字底色栏的颜色
* @param $c String
*
*/
function setTextBarColor ($c) {
$this->textBarColor = $c;
}
/**
* 获取文字底色栏的颜色
* @return String - 文字底色栏的颜色
*/
function getTextBarColor () {
return $this->textBarColor;
}
/**
* 设置是否需要绘制文字底色栏
* @param $boolean boolean true:需要绘制底色栏,反之 false
*
*/
function setPaintBar ($boolean) {
$this->paintBar = $boolean;
}
/**
* 获取是否需要绘制文字底色栏
* @return boolean true:需要绘制底色栏,反之 false
*/
function getPaintBar () {
return $this->paintBar;
}
/**
* 设置文字底色栏的高度
* @param $h int
*
*/
function setBarHeight ($h) {
if ($h < 0 )
$h = 1;
$this->barHeight = $h;
}
/**
* 获取文字底色栏的高度
* @return int - 文字底色栏的高度
*/
function getBarHeight () {
return $this->barHeight ;
}
/**
* 设置文字底色栏的宽度
* @param $width int
*
*/
function setBarWidth ($width){
if ($width < 0)
$width = $this->getImgWidth();
$this->barWidth = $width;
}
/**
* 获取文字底色栏的宽度
* @return int - 文字底色栏的宽度
*/
function getBarWidth () {
return $this->barWidth;
}
/**
* 设置文字底色栏是否采用渐变.true:采用渐变,反之,false
* @param $boolean boolean true:采用渐变,反之,false
*
*/
function setBarGradual ($boolean){
$this->barGradual = $boolean;
}
/**
* 获取文字底色栏是否采用渐变
* @return boolean - true:采用渐变,反之,false
*/
function getBarGradual () {
return $this->barGradual;
}
/**
* 设置文字颜色字符串.采用rgb格式的10进制.rgb用,隔开,比如 255,255,0
* @param $c String
*
*/
function setTextColor ($c) {
$this->textColor = $c;
}
/**
* 获取文字颜色
* @return string - 文字颜色字符串.采用rgb格式的10进制.rgb用,隔开,比如 255,255,0
*/
function getTextColor () {
return $this->textColor;
}
/**
* 设置文字文字的透明度
* @param $a int
*
*/
function setAlpha ($a) {
if ($a > 127 || $a < 0){
$a = 75;
}
$this->alpha = $a;
}
/**
* 获取文字的透明度
* @return int - 文字的透明度
*/
function getAlpha () {
return $this->alpha;
}
/**
* 设置文字底色栏的起始的x坐标
* @param $x int
*
*/
function setTextX ($x) {
$this->textX = $x;
}
/**
* 获取水印文字的起始X坐标
* @return int - 水印文字的起始X坐标
*/
function getTextX () {
return $this->textX;
}
/**
* 设置水印文字的起始Y坐标
* @param $y int
*
*/
function setTextY ($y) {
$this->textY = $y;
}
/**
* 获取水印文字的起始Y坐标
* @return int - 水印文字的起始Y坐标
*/
function getTextY () {
return $this->textY;
}
/**
* 获取要被加水印的图片的格式
* 注:这里可以修改一下,采用php库的图象函数exif_imagetype,判断图片的格式,而不是根据输入文件的后缀名来判断
* @return int - 要被加水印的图片的格式
*/
function getImgFormat () {
//return $this->imgFormat;
if (function_exists('exif_imagetype')) {
$tpn = exif_imagetype($this->getImgUrl());
switch ($tpn) {
case 1 :
$tp = "gif";
break;
case 2 :
$tp = "jpeg";
break;
case 3 :
$tp = "png";
break;
case 6 :
$tp = "bmp";
break;
case 15 :
$tp = "wbmp";
break;
case 16 :
$tp = "xbm";
break;
}//end switch
return $tp;
}else{
return $this->getImageType($this->getImgUrl());
}
}
/**
* 获取图片类型
* 这个函数是当php.ini中没有打开php_exif.dll扩展时调用的
* 如果已经打开了这个扩展,可以使用php自带的exif_imagetype函数达到相同效果
* @param $imgUrl String 图片地址
* @return String 图片类型,如jpg,gif等
*/
function getImageType ($imgUrl) {
//echo ":::::::::::::<BR>";
$imghandle = fopen($imgUrl,"r");
$byte1 = fread($imghandle ,1);
$hex = base_convert(ord($byte1),10,16);
fclose($imghandle);
$res = "";
switch ($hex) {
case 47:
$res = "gif";
break;
case ff:
$res = "jpg";
break;
case 42:
$res = "bmp";
break;
case 89:
$res = "png";
break;
case 00:
$res = "wbmp";
break;
}//end switch
//echo "RES = ".$res." , HEX=".$hex."<BR>";
return $res;
}
/**
* 设置输出图片的类型.如jpg,gif等
* @param $o string
*
*/
function setOutFormat ($o) {
$this->outFormat = $o;
}
/**
* 获取输出图片的类型.如jpg,gif等
* @return string - 输出图片的类型.如jpg,gif等
*/
function getOutFormat () {
return $this->outFormat;
}
作者: aceyin 发布时间: 2007-01-18
代码:
/**
* 设置输出图片的名称
* @param $n String
*
*/
function setImgSaveName ($n) {
$this->imgSaveName = $n;//.$this->getOutFormat();
}
/**
* 获取输出图片的名称.
* @return int - 输出图片的名称
*/
function getImgSaveName () {
return $this->imgSaveName;
}
/**
* 构造函数
* @param $backImgUrl 要被加水印的图片地址
* @param $text 水印文字内容
*
*/
function TextWatermarkTool ($backImgUrl , $text ) {
$this->imgUrl = $backImgUrl ;
$this->text = $text;
$this->init();
}
/**
* 初始化 . 主要做以下工作:
* 1.根据设置的图片格式,采用不同的方法读取图片数据,保存到该类实例的image对象中,以便被后面的方法调用.
* 2.初始化一些宽度和高度值
*
*/
function init () {
//echo getType($this->image)."<BR>";
//echo $this->JPEG."<BR>";
if (strtolower($this->getImgFormat()) == strtolower($this->JPEG) ||
strtolower($this->getImgFormat()) == strtolower($this->JPG)){
$this->image = imagecreatefromjpeg ($this->getImgUrl());
}else if (strtolower($this->getImgFormat()) == strtolower($this->GIF)){
$this->image = imagecreatefromgif ($this->getImgUrl());
}else if (strtolower($this->getImgFormat()) == strtolower($this->PNG)){
$this->image = imagecreatefrompng ($this->getImgUrl());
}else if (strtolower($this->getImgFormat()) == strtolower($this->WBMP)){
$this->image = imagecreatefromwbmp ($this->getImgUrl());
}else if (strtolower($this->getImgFormat()) == strtolower($this->XBM)){
$this->image = imagecreatefromxbm ($this->getImgUrl());
}else {
$this->image = imagecreatetruecolor (250,150);
}
//echo getType($this->image)."<BR>";
if ($this->image !== NULL){
ImageAlphaBlending($this->image, true);
$this->imgWidth = imagesx($this->image);
$this->imgHeight = imagesy($this->image);
//$this->setBarWidth($this->imgWidth);
$this->setOutWidth($this->imgWidth);
$this->setOutHeight($this->imgHeight);
//echo $w." , ".$h."<BR>";
}
}
/**
* 绘制文字底栏
*
*/
function paintBar () {
$barImg = NULL;
if ($this->getPaintBar()){
$colors = explode(",",$this->getTextBarColor());
$r = $colors[0]?$colors[0]:0;
$g = $colors[1]?$colors[1]:0;
$b = $colors[2]?$colors[2]:0;
$_alpha = 0;
$x1 = $this->getBarX();
$y1 = $this->getBarY();
if ($this->getBarGradual()){
if ($this->getBarWidth() <= 0)
$this->setBarWidth(1);
$brw = $this->getBarWidth();
//echo "brw=".$brw."<BR>";
$_deltan = 127/$brw;
$y2 = $this->getBarHeight();
for ($i = 0 ; $i < $brw ; $i++) {
$_alpha += $_deltan;
$color = imagecolorallocatealpha($this->image,$r,$g,$b,$_alpha);
$x2 = $x1+1;
imagefilledrectangle ($this->image, $x1, $y1, $x2, $y2, $color );
$x1++;
}//end for
}else{
//echo "BarAlpha = ".$this->getBarAlpha();
$color = imagecolorallocatealpha($this->image,$r,$g,$b,$this->getBarAlpha());
$x2 = $x1+$this->getBarWidth();
$y2 = $y1+$this->getBarHeight();
imagefilledrectangle ($this->image, $x1, $y1, $x2, $y2, $color );
}//end if else
}//end if
return $this->image;
}//end function paintBar
/**
* 绘制文字
*
*/
function paintText () {
$colors = explode(",",$this->getTextColor());
$r = $colors[0]?$colors[0]:0;
$g = $colors[1]?$colors[1]:0;
$b = $colors[2]?$colors[2]:0;
if ($this->getFontName() == "" || !file_exists($this->getFontName()))
$font = $this->defFont;
else
$font = $this->getFontName();
//如果是中文,得先转换成utf-8.当然,如果是中文,字体就必须设置成能正常显示中文的字体了.
$txt = $this->getText();
$txt = iconv('GB2312','UTF-8',$txt);
//echo "TextAlpha:".$this->getAlpha();
$textcolor = imagecolorallocatealpha($this->image,$r,$g,$b,$this->getAlpha());
//注:imagettftext 的 x 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0。 所以这里的Y需要加上字体的大小,另外再加一个偏移量
imagettftext ($this->image, $this->getFontSize(),$this->getTextAngle(), $this->getTextX(), $this->getTextY()+$this->getFontSize()+5,$textcolor,$font, $txt);
//imagestring($this->image, 5, $this->getTextX(), $this->getTextY(), "Hello world!", $textcolor);
/*
$txt = $this->getText();
$textcolor = imagecolorallocatealpha($this->image,255,255,255,90);
//注:imagettftext 的 x 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0。 所以这里的Y需要加上字体的大小,另外再加一个偏移量
imagettftext ($this->image, 20,35, 80, 325,$textcolor,"d:\\windows\\fonts\\verdana.ttf", $txt);
*/
}//end func
/**
* 输出图象
*
*/
function output () {
//echo "(***) ".$this->getOutType();
if ($this->getOutType() == $this->OUTTOFILE){
//echo "tofile";
switch (strtolower($this->getOutFormat())) {
case $this->JPEG:
ImageJPEG($this->image,$this->getImgSaveName(),$this->getOutImgQuality());
break;
case $this->GIF:
ImageGIF($this->image,$this->getImgSaveName());
break;
case $this->PNG:
ImagePNG($this->image,$this->getImgSaveName());
break;
case $this->WBMP:
Imagewbmp($this->image,$this->getImgSaveName());
break;
default:
ImageJPEG($this->image,$this->getImgSaveName(),$this->getOutImgQuality());
break;
}//end switch
}else{
//echo "to browser";
$header = "Content-type: image/";
switch (strtolower($this->getOutFormat())) {
case $this->JPEG:
$header .= $this->JPEG;
break;
case $this->GIF:
$header .= $this->GIF;
break;
case $this->PNG:
$header .= $this->PNG;
break;
case $this->WBMP:
$header .= $this->WBMP;
break;
default:
$header .= $this->JPEG;
break;
}//end switch
//echo "*********".$header;
header ($header);
ImageJPEG($this->image);
}
ImageDestroy($this->image);
}//end func
/**
* 主要方法.供别的类调用的.
*
*/
function makeWatermark () {
$this->paintBar();
$this->paintText();
$this->output();
}
}//end class
//for test
$cls = new TextWatermarkTool("c:\\img\\mozilla.jpg","www.xgame8.com");
//$cls->setFontName("d:\\windows\\fonts\\simhei.ttf");
$cls->setOutType("browser");
//$cls->setText($cls->getOutType()." ".$cls->getImgFormat()." \n ".$cls->getImgUrl());
//$cls->setImgFormat("gif");
$cls->setAlpha(60);
$cls->setTextAngle(-45);
$cls->setTextX(8);
$cls->setTextY(8);
$cls->setBarGradual(true);
$cls->setPaintBar(false);
$cls->setBarX(8);
$cls->setBarY(8);
$cls->setBarAlpha(75);
$cls->setBarHeight(25);
$cls->setTextBarColor("255,255,255");
$cls->setBarWidth(120);
$cls->setFontSize(20);
$cls->setTextColor("255,255,255");
$cls->setFontName("d:\\windows\\fonts\\verdana.ttf");
$cls->setOutFormat("png");
$cls->setOutImgQuality("100");
$cls->setImgSaveName("c:\\img\\mozillaw.png");
$cls->makeWatermark();
//end for test
?>
作者: aceyin 发布时间: 2007-01-18
<?
require_once("TextWatermarkTool.class.php");
//echo "HELLO";
$cls = new TextWatermarkTool("http://127.0.0.1/phpq/templates/xgame8/images/top.jpg","极限吧 -- www.xgame8.com");
$cls->setFontName("d:\\windows\\fonts\\simhei.ttf");
$cls->setOutType("file");
$cls->setAlpha(80);
$cls->setTextX(0);
$cls->setTextY(0);
$cls->setBarGradual(false);
$cls->setPaintBar(true);
$cls->setTextBarColor("161,191,247");
$cls->setBarWidth(120);
$cls->setFontSize(20);
$cls->setTextColor("255,255,255");
$cls->setOutFormat("png");
$cls->setBarAlpha(100);
$cls->setImgSaveName("c:\\byclass.jpg");
$cls->makeWatermark();
?>
作者: aceyin 发布时间: 2007-01-18
在网上搜php加水印的,搜了很多,都是三言两语就搞定了的。。。
不能达到自己的效果。所以写了一个。。
由于学php不久,所以中间可能有些不对的地方,请大家指正 :)
另外。只测试了jpg的。似乎可以正常工作。对于gif之类的还未测试。。

另外就是想问:为啥我php5.0,和他自己自带的gd库环境下
exif_imagetype
这个函数,会提示 Call to undefined function exif_imagetype() 呢??
[ 本帖最后由 aceyin 于 2007-1-18 01:37 编辑 ]
作者: aceyin 发布时间: 2007-01-18
试一下
作者: bigbigant 发布时间: 2007-01-18
作者: cator 发布时间: 2007-01-18
exif_imagetype
必须在php.ini里面打开extension=php_mbstring.dll 和 extension=php_exif.dll
并保证, extension=php_mbstring.dll 在 extension=php_exif.dll 之前.....不知道为什么..
作者: aceyin 发布时间: 2007-01-18
引用:
原帖由 cator 于 2007-1-18 11:36 发表不错,就是有点小复杂
把那一系列setXXX和getXXX方法去掉就简单了 :$
作者: aceyin 发布时间: 2007-01-18
几种格式都测试过了.没啥问题
有一个疑问.我处理图片的时候,发现,
如果是给jpg格式的文件加水印,水印效果可以半透明.
但是给其他格式的图片,如gif,png等加水印时,
却发现,即使设置了文字透明度,加到图片上去的时候,却是不透明的....
请教大家了.
作者: aceyin 发布时间: 2007-01-18
作者: Poon 发布时间: 2007-01-19
作者: MySQL 发布时间: 2007-01-19
作者: goshawk 发布时间: 2007-01-19
作者: suturn-ly 发布时间: 2007-01-20
作者: quiant 发布时间: 2007-01-28
:)
作者: luzhou 发布时间: 2007-01-28
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28