29 03 2016
//添加水印
       
        $newname=implode(".",$filename);
        $dst_path = $uploadfile;    //上传后的路径
        $src_path = $_SERVER['DOCUMENT_ROOT']."/data/upload/src.jpg";  水印路径
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //获取水印图片的宽高
        list($src_w, $src_h) = getimagesize($src_path);
        //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
        $ground_info = getimagesize($dst_path);
        $ground_w = $ground_info[0];//取得背景图片的宽
        $ground_h = $ground_info[1];//取得背景图片的高
        $posX = $ground_w - $src_w - 10;   
        $posY = $ground_h - $src_h - 10;
        imagecopymerge($dst, $src, $posX, $posY, 0, 0, $src_w, $src_h, 80);
        //如果水印图片本身带透明色,则使用imagecopy方法

        //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);

//输出图片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        switch ($dst_type) {
        case 1://GIF
        //header('Content-Type: image/gif');   //打开就会输出到浏览器
        imagegif($dst,$uploadfile);
         
        break;
        case 2://JPG
        //header('Content-Type: image/jpeg');
        imagejpeg($dst,$uploadfile);
       
        break;
        case 3://PNG
        //header('Content-Type: image/png');
        imagepng($dst,$uploadfile);
       
        break;
        default:
        break;
        }
        imagedestroy($dst);
        imagedestroy($src);

发表评论