前端时分由于义务需求,誊写了一个支撑gif动画的水印类,类为php代码,忘盛意的朋侪可以转播共享,可是不要忘了附上版权,来自徐州亿优科技
以下为类的代码片断,我在截取了类中的一小段函数没显示,由于要确保全部的版权,有需求的朋侪可以加我义务QQ讨取:499375381
<?php
/**
* 水印类,支撑GIF动画 需求 MagickWandForPHP 扩展
* 作者:张虹亮 icyzhl#yeah.net
* $imagefile 源图画途径
* $string 水印文字
* $options array类型,水印的样式设置
ta 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['ta'] as $key=>$option) {
if(!empty($option['str'])) {
$str = trim($option['str']);
if(!empty($str)) {
$this->ta[$key]['water_file'] = false;
$this->ta[$key]['water_mode'] = 'font';
if($option['direction']) {
$this->ta[$key]['str_arr'] = $this->str_nl($option['str']);
}
$this->ta[$key]['str'] = $option['str'];
} else {
$this->ta[$key]['str'] = false;
}
} else {
$this->ta[$key]['str'] = false;
}
if($this->ta[$key]['str']=== false) {
if(!empty($option['water_file'])) {
$this->ta[$key]['water_file'] = $option['water_file'];
$this->ta[$key]['water_mode'] = 'file';
} else {
$this->ta[$key]['water_file'] = false;
}
}
if($this->ta[$key]['str'] === false && $this->ta[$key]['water_file'] === false ) $this->error("kill","water set error no fonts and no file");
else {
$this->dwand[$key] = NewDrawingWand();
if($this->ta[$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->ta[$key]['water_file'] )) $this->error("kill","water image not finded");
}
}
}
}
/**
*
* 设置水印字体样式
*
**/
function set_style() {
foreach($this->options['ta'] as $key=>$option) {
if($this->ta[$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->ta[$key]['angle'] = $option['angle'];
} else $this->ta[$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['ta'] as $key=>$option) {
//取得水印的高度和宽度
if($this->ta[$key]['water_mode']=='font') {
//$this->water_height = MagickGetStringHeight($this->wnd,$this->dwand,$this->str);
$this->ta[$key]['water_height'] = $option['size'];
if(empty($this->ta[$key]['water_height'])) $this->ta[$key]['water_height'] = MagickGetStringHeight($this->wnd,$this->dwand[$key],$this->ta[$key]['str']);
$this->ta[$key]['water_width'] = MagickGetStringWidth($this->wnd,$this->dwand[$key],$this->ta[$key]['str']);
} elseif($this->water_mode=='file') {
$this->ta[$key]['water_height'] = MagickGetImageHeight($this->water_wnd[$key]);
$this->ta[$key]['water_width'] = MagickGetImageWidth($this->water_wnd[$key]);
}
//边距
list($x,$y) = explode(',',$option['position']);
if(is_numeric($x) && is_numeric($y)) {
$this->ta[$key]['left'] = $x;
$this->ta[$key]['top'] = $y;
} else {
$this->ta[$key]['left'] = 0;
$this->ta[$key]['top'] = 0;
}
if(!$option['direction']) $this->ta[$key]['top'] += $this->ta[$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['ta'] as $key=>$option) {
//$this->dwand[$key] = NewDrawingWand();
if($this->ta[$key]['water_mode'] == 'font') {
MagickSetImageIndex($this->wnd,0);
$this->annotation($key);
if($this->options['gif']) {
while(MagickNextImage($this->wnd)) {
$this->annotation($key);
}
}
} elseif ($this->ta[$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->ta[$key]['left'],$this->ta[$key]['top']);
while(MagickNextImage($this->wnd)) {
MagickCompositeImage($this->wnd ,$this->water_wnd[$key] ,MW_AtopCompositeOp,$this->ta[$key]['left'],$this->ta[$key]['top']);
}
} else MagickCompositeImage($this->wnd ,$this->water_wnd[$key] ,MW_AtopCompositeOp,$this->ta[$key]['left'],$this->ta[$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动画的类水印就写到这里