PHP中上传图片增加水印小案例

来源:互联网 发布:linux 增加用户组 编辑:程序博客网 时间:2024/05/21 07:48

1.文档目录如上图所示。

2.前台代码:’

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form name='frm' method='post' action='upload.php' enctype="multipart/form-data">
        <table width="50%" border="2" align="center" rules="all" cellpadding="10">
        <tr>
            <th colspan="2">请输入信息</th>
        </tr>
        <tr>
            <th>选择上传图片</th>
            <th><input type="file" name="upload"></input></th>
        </tr>
        <tr>
            <th colspan="2"><input type="submit" name="submit" value="提交"></input></th>
        </tr>
    </table>
    </form>
</body>
</html>

php代码:

<?php
header('content-type:text/html;charset=utf-8');
if (isset($_POST['submit'])) {
    //临时文件
    $filename = $_FILES['upload']['tmp_name'];
    //目标文件路径及文件名
    $path = './upload';
    @mkdir($path);
    $destination = $path.'/'.date('YmdHis').'.jpg';
    if(move_uploaded_file($filename, $destination)){

        //水印文字开始
        $image = imagecreatefromjpeg($destination);
        //第二步
        $color = imagecolorallocatealpha($image, 200, 100, 0,mt_rand(10,100));  //0-127
        $size = 30;
        $angle = 45;
        $x = 15;
        $y = 335;
        $fontfile = './sucai/hanyi.ttf';
        $string = '制作图像文字水印效果';

        imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $string);

        //第三步,输出
        imagejpeg($image,$destination);
        //第四步,销毁
        imagedestroy($image);
        //水印文字结束
        echo "上传成功";
    }else{
        echo "上传失败";
    }
} else {
    echo "<script>alert('nono');location.href='跳转页面'</script>";
}

?>