分享一个支持gif动画的水印函数类

2023-05-14    分类: 网站建设

前端时间因为工作需要,书写了一个支持gif动画的水印类,类为php代码,忘好心的朋友可以转播分享,但是不要忘记附上版权,来自成都创新互联科技

以下为类的代码片段,我在截取了类中的一小段函数没显示,因为要保证完整的版权,有需要的朋友可以加我工作QQ索取:631063699

<?php
/**
* 水印类,支持GIF动画 需要 MagickWandForPHP 扩展
* 作者:张虹亮 icyzhl#yeah.net
* $imagefile 源图片路径
* $string 水印文字
* $options array类型,水印的样式设置
data array
str(string) 水印文字(需要UTF8格式)
size:文字大小
position:
int,int 数字确定准确位置

color:字体颜色 只能用网页16进制模式表示 (如:#ff0000)或者:auto,根据背景颜色自动调整文字颜色(gif动画无效)
blod:是否粗体(true or false 目前不可用)
water_file(string) 水印图片文件
font (string) 字体文件
gif(bool) true 支持GIF动画,否则不支持
format(string) 输出图片的格式,只能是 jpeg gif 和 png 中的一个

**/开始定义支持gif动画水印的类名称
class watermark {
//设置构造函数
function watermark($imagefile,$options) {
$this->wnd = NewMagickWand();
$this->imagefile = $imagefile;
if(!MagickReadImage( $this->wnd, $imagefile )) $this->error("kill","source image not finded");
$this->width = MagickGetImageWidth($this->wnd);
$this->height = MagickGetImageHeight($this->wnd);
$this->mime = MagickGetImageMimeType($this->wnd);
$this->options = $options;
$this->image_number = MagickGetNumberImages($this->wnd);
}

/**
*
* 设置gif动画水印是文字还是图片,当然了,我这里不光光支持gif动画,也支持文字,在类函数调用的时候说明即可
*
***/

function set_water_mode() {
foreach($this->options['data'] as $key=>$option) {
if(!empty($option['str'])) {
$str = trim($option['str']);
if(!empty($str)) {
$this->data[$key]['water_file'] = false;
$this->data[$key]['water_mode'] = 'font';
if($option['direction']) {
$this->data[$key]['str_arr'] = $this->str_nl($option['str']);
}
$this->data[$key]['str'] = $option['str'];
} else {
$this->data[$key]['str'] = false;
}
} else {
$this->data[$key]['str'] = false;
}

if($this->data[$key]['str']=== false) {
if(!empty($option['water_file'])) {
$this->data[$key]['water_file'] = $option['water_file'];
$this->data[$key]['water_mode'] = 'file';
} else {
$this->data[$key]['water_file'] = false;
}
}

if($this->data[$key]['str'] === false && $this->data[$key]['water_file'] === false ) $this->error("kill","water set error no fonts and no file");
else {
$this->dwand[$key] = NewDrawingWand();
if($this->data[$key]['water_mode']=='font') {
if(!is_object($this->pwand[$key])) {$this->pwand[$key] = NewPixelWand();}

}else {
if(!is_object($this->water_wnd[$key])) $this->water_wnd[$key] = NewMagickWand();
if(!MagickReadImage( $this->water_wnd[$key], $this->data[$key]['water_file'] )) $this->error("kill","water image not finded");
}
}
}

}

/**
*
* 设置水印字体样式
*
**/
function set_style() {
foreach($this->options['data'] as $key=>$option) {

if($this->data[$key]['water_mode']=='font') {
if($option['color']) {
if(!is_object($this->pwand[$key])) {$this->pwand[$key] = NewPixelWand();}
PixelSetColor($this->pwand[$key], $option['color']);
}

if($option['angle']) {
$this->data[$key]['angle'] = $option['angle'];
} else $this->data[$key]['angle'] = 0;
if(file_exists($option['font'])) DrawSetFont($this->dwand[$key], $option['font']);
if($option['blod']) DrawSetFontWeight($this->dwand[$key],600);
DrawSetFontSize($this->dwand[$key], $option['size']?$option['size']:12);
DrawSetFillColor($this->dwand[$key], $this->pwand[$key]);
}

}
}

/**
*
* 设置gif图片水印的位置
*
***/
function set_position() {
foreach($this->options['data'] as $key=>$option) {

//获取水印的高度和宽度
if($this->data[$key]['water_mode']=='font') {
//$this->water_height = MagickGetStringHeight($this->wnd,$this->dwand,$this->str);
$this->data[$key]['water_height'] = $option['size'];
if(empty($this->data[$key]['water_height'])) $this->data[$key]['water_height'] = MagickGetStringHeight($this->wnd,$this->dwand[$key],$this->data[$key]['str']);
$this->data[$key]['water_width'] = MagickGetStringWidth($this->wnd,$this->dwand[$key],$this->data[$key]['str']);
} elseif($this->water_mode=='file') {
$this->data[$key]['water_height'] = MagickGetImageHeight($this->water_wnd[$key]);
$this->data[$key]['water_width'] = MagickGetImageWidth($this->water_wnd[$key]);
}
//边距

list($x,$y) = explode(',',$option['position']);
if(is_numeric($x) && is_numeric($y)) {
$this->data[$key]['left'] = $x;
$this->data[$key]['top'] = $y;
} else {
$this->data[$key]['left'] = 0;
$this->data[$key]['top'] = 0;
}
if(!$option['direction']) $this->data[$key]['top'] += $this->data[$key]['water_height'];
}

}

/**
*
* 写gif动画水印到图片上
*
***/
function write() {
$this->set_water_mode();//确定是使用文字还是图片水印
$this->set_style();//如果是文字设置文字的样式
$this->set_position(); //确定水印的位置
$this->set_format();
MagickSetWandSize($this->wnd,$this->width,$this->height);
MagickSetFormat( $this->wnd, $this->format);
$this->wnd = MagickCoalesceImages($this->wnd);

foreach($this->options['data'] as $key=>$option) {

//$this->dwand[$key] = NewDrawingWand();
if($this->data[$key]['water_mode'] == 'font') {

MagickSetImageIndex($this->wnd,0);

$this->annotation($key);

if($this->options['gif']) {
while(MagickNextImage($this->wnd)) {
$this->annotation($key);
}
}
} elseif ($this->data[$key]['water_mode'] == 'file') {
if($this->options['gif']) {
$this->wnd = MagickCoalesceImages($this->wnd);
MagickSetImageIndex($this->wnd,0);
MagickCompositeImage($this->wnd ,$this->water_wnd[$key] ,MW_AtopCompositeOp,$this->data[$key]['left'],$this->data[$key]['top']);
while(MagickNextImage($this->wnd)) {
MagickCompositeImage($this->wnd ,$this->water_wnd[$key] ,MW_AtopCompositeOp,$this->data[$key]['left'],$this->data[$key]['top']);
}
} else MagickCompositeImage($this->wnd ,$this->water_wnd[$key] ,MW_AtopCompositeOp,$this->data[$key]['left'],$this->data[$key]['top']);

}
}
$this->is_write = true;
}

/**
*
*输出图片
*
***/
function putout($filenam='') {
if(!$this->is_write) $this->write();
header( "Content-Type: image/{$this->format}" );
if($this->options['gif']) MagickEchoImagesBlob( $this->wnd );
else MagickEchoImageBlob( $this->wnd );
}

function error($error_flag,$info){
if($error_flag == 'kill') die("$info");
}

/**
*
* 设置输出图片格式gif
*
**/
function set_format() {
$format_list = array('gif','jpeg','png');
if($this->options['gif'] && $this->image_number>1) {
$this->format = 'gif';
return;
}
if(in_array($this->options['format'],$format_list)) $this->format = $this->options['format'];
else {
if($this->mime) {
foreach ($format_list as $v) {
//echo $this->format.$v.'|'.$this->mime;
if(strpos($this->mime,$v)!==false) {$this->format = $v; break;}
}
} else $this->format = 'jpeg';

}
if(empty($this->format)) $this->format = 'jpeg';
}

function str_nl($str) {

$strlen = mb_strlen($str,"UTF-8");

for($i=0;$i<$strlen;$i++) {
$v = mb_substr($str,$i,1,"UTF-8");
if(!preg_match("/^[a-zA-Z0-9]+$/",$v) && $v!=' ') $word .= " ".$v." ";
else $word .= $v;
}

$reword = str_replace(" "," ",$word);
while($word != $reword){
$word = $reword;
$reword = str_replace(" "," ",$word);
}

$words = explode(" ",$word);
foreach($words as $k=>$v) {
$v = trim($v);
if(empty($v)) unset($words[$k]);
}
return $words;
}
//这里省略了一个函数,也是该类的重要函数之一
}
?>

再次说明:以上类文件中我减少了一个函数,如果有需要请联系我本人索取完整类的内容,支持gif动画的类水印就写到这里

当前题目:分享一个支持gif动画的水印函数类
本文路径:https://www.cdcxhl.com/news35/259135.html

成都网站建设公司_创新互联,为您提供网站营销网站制作定制开发做网站标签优化网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站