上传图片并给图片添加文字水印

1.给图片添加文字水印,首先给一个添加文字水印的类:


   
   getImageRes($this->getExtendsName($imageUrl), $imageUrl);  //获取被操作的图像标识  
        $textcolor=imagecolorallocate($img,0,0,0);            //设置字体颜色为蓝色,值为RGB颜色值  
        //imagestring($img, 20, 30, 100, "$watherImageUrl", $textcolor);  
        $fnt="c:/windows/fonts/simhei.ttf";                       //定义字体  
        $text =iconv("gb2312", "utf-8", $watherImageUrl);         //将中文转换为UTF-8格式  
        imagettftext($img,20,0,30,100,$textcolor,$fnt,$text);     //写TTF文字到图中  
        //根据图像标识符、后缀和路径,执行outputImage方法,输出图像  
        $this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl);  
        imagedestroy($img);                                 //销毁图像  
    }  
    function outputImage($img, $extendsName, $imageUrl){    //根据图像标识、图片后缀和路径输出图像  
         switch($extendsName){                              //判断图像后缀  
             case '.gif':                                   //如果后缀为gif  
                 imagegif($img, $imageUrl);                 //则输出img图像  
                 break;  
             case '.jpg':  
                 imagejpeg($img, $imageUrl);  
                 break;  
             case '.png':   
                 imagepng($img, $imageUrl);  
                 break;  
             case '.bmp':  
                 imagewbmp($img, $imageUrl);  
                 break;  
        }                 
    }  
}  
2.上传图片的模板文件:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>上传图片添加文字水印</title>
    <style type="text/css">
        .STYLE1 {
            color: #990100;
            font-size: 18px;
            font-weight: bold;
        }
    </style>
</head>
<body>
<form name="form1" method="post" action="server.php" enctype="multipart/form-data">
    <span class="STYLE1">上传图片:</span>
    <input type="file" name="file" class="input">
    <input type="hidden" name="flag" value="1">
    <input type="button" value="提交" name="imageField"  />
</form>
</body>
</html>

3.处理上传图片的php页面server.php

<?php
if ($_FILES["file"]["name"]!="" && $_POST['flag']=='1'){        //判断提交内容是否为空
    $type = strstr($_FILES["file"]["name"], '.');       //获取上传图片后缀
    if($_FILES["file"]["name"]==''){                    //判断上传图片名称是否为空
        echo "<script>alert('图片不能为空!');</script>";
        exit();
    }else if(!($type=='.gif' || $type == '.png' || $type == '.bmp' || $type == '.jpg')){    //判断上传图片格式是否正确
        echo "<script>alert('图片格式不正确!');</script>";
        exit();
    }
    function getUpfileName($fileName){                  //定义上传文件在服务器中存储的名称
        return 'waterpress'.strstr($fileName, ".");     //使用固定名称(也可以通过时间戳、随机数定义)
    }
    if(!is_dir('upfiles')){                             //判断上传文件存储文件夹是否存在
        mkdir('upfiles');                               //如果不存在,则创建文件夹
    }
    $saveDir = "upfiles/" . getUpfileName($_FILES["file"]["name"]);         //定义上传文件存储路径
    if(move_uploaded_file($_FILES["file"]["tmp_name"], $saveDir)){          //执行文件上传操作
        require_once 'AddWaterPress.php';                                   //包含添加水印操作的文件
        $addWaterPress = new AddWaterPress();                               //类的实例化
        $addWaterPress->add($saveDir, "WWW.MRBCCD.CN");  //执行添加方法,传递参数,指定水印文字
        echo "<script>alert('图片添加成功');</script>";
    }
}
?>

还有在图片上添加图片水印的类,看这里:http://blog.csdn.net/manbujingxin/article/details/6723992

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值