php函数给目标图片添加水印

来源:赵克立博客 分类: PHP 标签:GDPHP发布时间:2014-09-14 10:09:00最后更新:2014-09-14 12:31:02浏览:2814
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2014-09-14 12:31:02
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
/**
 *给目标图片添加水印
//使用示例
markimg(array(
	'dst'=>'./images/1.jpg',//原始图像
	'src'=>'./images/ico.png',//水印图像
	'pos'=>'center'//水印位置('left,right,center')
));
 */
function markimg($info=array(
					'det'=>null,
					'src'=>null,
					'pos'=>'right'
					)){
		//原始图像 
       $dst =$info['dst'] ;//"./images/tu.png"; //注意图片路径要正确 
	   //水印图像 
       $src = $info['src'];//"./images/ico.png"; //注意路径要写对
		//水印在原图的位置比例
		$pos=$info['pos'];
       //得到原始图片信息 
       $dst_info = getimagesize($dst);  
       switch ($dst_info[2]) 
       { 
        case 1: $dst_im =imagecreatefromgif($dst);break;
        case 2: $dst_im =imagecreatefromjpeg($dst);break;
        case 3: $dst_im =imagecreatefrompng($dst);break; 
        case 6: $dst_im =imagecreatefromwbmp($dst);break; 
        default: die("不支持的文件类型1");exit; 
       } 
 
       $src_info = getimagesize($src); 
       switch ($src_info[2]) 
       { 
        case 1: $src_im =imagecreatefromgif($src);break;    
        case 2: $src_im =imagecreatefromjpeg($src);break;   
        case 3: $src_im =imagecreatefrompng($src);break;
        case 6: $src_im =imagecreatefromwbmp($src);break; 
        default: die("不支持的文件类型1");exit; 
       } 
       //支持png本身透明度的方式
	   switch($pos){
		   case 'right':
		   //右下角
	  	   imagecopy($dst_im,$src_im,$dst_info[0]-$src_info[0]-10,$dst_info[1]-$src_info[1]-10,0,0,$src_info[0],$src_info[1]);
		   break;
		   case 'center':
		   //正中间
			imagecopy($dst_im,$src_im,($dst_info[0]-$src_info[0])/2,($dst_info[1]-$src_info[1])/2,0,0,$src_info[0],$src_info[1]);
			break; 
		   default :
		   //左下角
			imagecopy($dst_im,$src_im,10,$dst_info[1]-$src_info[1]-10,0,0,$src_info[0],$src_info[1]);
		   }
	   //保存图片 
       switch ($dst_info[2]){ 
        case 1: imagegif($dst_im,$dst);break;
        case 2: imagejpeg($dst_im,$dst);break; 
        case 3: imagepng($dst_im,$dst);break;    
        case 6: imagewbmp($dst_im,$dst);break; 
        default: 
        die("不支持的文件类型2");exit; 
       } 
       imagedestroy($dst_im); 
       imagedestroy($src_im);   	
	}

微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python