分享一個支持gif動畫的水印函數類
摘要:分享一個支持gif動畫的水印函數類,支持gif動畫水印的php類,最近由于工作和項目需要,書寫了一個支持gif動畫的php自動增加水印的類,能很好的兼容文字和圖片,包括gif動畫圖片,如果大家對這個類有問題,可以聯系我。
前端時間因為工作需要,書寫了一個支持gif動畫的水印類,類為php代碼,忘好心的朋友可以轉播分享,但是不要忘記附上版權,來自合肥謹宸科技
以下為類的代碼片段,我在截取了類中的一小段函數沒顯示,因為要保證完整的版權,有需要的朋友可以加我工作QQ索取:499375381
<?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動畫的類水印就寫到這里