News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      乐道系统FAQ      PHP图片水印函数

PHP图片水印函数

发布日期:2014-05-17 00:00:00 910

此函数为PHP增加图片水印函数,有需要可借鉴。因包含在类中,一些函数有调用,去掉即可。

public function watermark(){
$this->picssize($this->watermark['file']); 
if($this->info !== true) return false; 
if($this->Size[0]<$this->watermark['min_width'] || $this->Size[1]<$this->watermark['min_height']){ 
$this->info = $this->Language[4];
return false;
}
$nzpics = $this->picscreate($this->watermark['file']);
if($this->watermark['type'] == 'pics'){
$wapics = $this->watermark['pics']; 
if(!is_file($wapics)){
$this->info = $this->Language[6] . $wapics;
return false;
}
$src = getimagesize($wapics); 
$wapics = $this->picscreate($wapics); 
}else { 
$src[0] = 300;
$src[1] = 30;
if(!is_file($this->watermark['font_file'])){
$this->info = $this->Language[5] . $this->watermark['font_file'];
return false;
}
}
switch($this->watermark['alignment']){
case 1 : $src_x = 0; $src_y = 0; break;
case 2 : $src_x = ($this->Size[0]/2)-($src[0]/2); $src_y = 0; break;
case 3 : $src_x = $this->Size[0]-$src[0]; $src_y = 0; break;
case 4 : $src_x = 0; $src_y = ($this->Size[1]/2)-($src[1]/2); break;
case 5 : $src_x = ($this->Size[0]/2)-($src[0]/2); $src_y = ($this->Size[1]/2)-($src[1]/2); break;
case 6 : $src_x = $this->Size[0]-$src[0]; $src_y = ($this->Size[1]/2)-($src[1]/2); break;
case 7 : $src_x = 0; $src_y = $this->Size[1]-$src[1]; break;
case 8 : $src_x = ($this->Size[0]/2)-($src[0]/2); $src_y = ($this->Size[1])-($src[1]); break;
case 9 : $src_x = ($this->Size[0])-($src[0]); $src_y = ($this->Size[1])-($src[1]); break;
}
$src_x += $this->watermark['x'];
$src_y += $this->watermark['y'];
if($this->watermark['type'] == 'pics'){ 
imagecopyresampled($nzpics,$wapics,$src_x,$src_y,0,0,$src[0],$src[1],$src[0],$src[1]); 
} else { 
$src_x += 0;
$src_y += 20;
$rgb = $this->hex2rgb($this->watermark['color']);
$color = imagecolorallocate($nzpics,$rgb[0],$rgb[1],$rgb[2]);
$result = @imagettftext($nzpics,$this->watermark['font_size'],$this->watermark['angle'],$src_x,$src_y,$color,$this->watermark['font_file'],$this->watermark['text']);
if(!is_numeric($result[0])){ 
$this->info = $this->Language[7];
return false;
}
unset($color,$rgb);
}
$newPics = $this->watermark['rename'] == 'auto' ? $this->watermark['file'] : $this->watermark['rename'];
switch($this->Size[4]){
case 'jpg' : $result = @imagejpeg($nzpics,$newPics,100); break; 
case 'jpeg' : $result = @imagejpeg($nzpics,$newPics,100); break; 
case 'png' : $result = @imagepng($nzpics,$newPics); break; 
case 'gif' : $result = @imagegif($nzpics,$newPics); break;
}
if($result){
@chmod ($newPics,$this->chmod_files); 
@imagedestroy($nzpics); @imagedestroy($wapics); 
unset($wapics,$nzpics,$src,$src_x,$src_y,$newPics);
return true;
} else {
$this->info = $this->Language[8].$newPics; 
return false;
}
}