文章目录

PHP - 图片上传并添加水印

一个的面向过程的 PHP 图片上传并对其添加文字水印的功能。
还有很多待需完善的地方。

index.php

<?php
$conn=@mysql_connect("localhost","root","root")  or die(mysql_error());  
@mysql_select_db('uploadimg',$conn) or die(mysql_error());  
$action = isset($_REQUEST['action'])? $_REQUEST['action'] : '';//判断action
if($action=='add'){//上传图片操作
    if (!is_dir("./upload")){//存储目录判断
        mkdir("./upload");
    } elseif (!is_dir("./afterupload")){
        mkdir("./afterupload");
    }
    $beforeName = $_FILES['photo']['name'];//原始图片文件名
    $afterName = "after".$_FILES['photo']['name'];//水印图片文件名
    $tmpName = $_FILES['photo']['tmp_name'];//缓存文件
    $beforeUrl = "./upload/";//原始图片路径
    $afterUrl = "./afterupload/";//原始图片路径
    if (!file_exists("$beforeUrl"."$beforeName")){//原始图片操作
        move_uploaded_file($tmpName,"$beforeUrl"."$beforeName");
    }
    if (!file_exists("$afterUrl"."$afterName")){//水印图片操作
        $src = "$beforeUrl"."$beforeName";
        //获取图片信息  
        $info = getimagesize($src);  
        //通过编号获取图像类型  
        $type = image_type_to_extension($info[2],false);  
        //在内存中创建和图像类型一样的图像  
        $fun = "imagecreatefrom".$type;  
        //图片复制到内存  
        $image = $fun($src);  
        //设置字体的路径  
        $font = "./font/arial.ttf";  
        //水印内容  
        $content = "shuiyin";  
        //设置字体颜色和透明度  
        $color = imagecolorallocatealpha($image, 0, 0, 0, 0);
        //(文件资源,红,绿,蓝,透明度)
        //写入文字  
        imagettftext($image, 30, 0, 0, 30, $color, $font, $content);
        //(文件资源,字体尺寸,字体角度,x坐标,y坐标,字体颜色,字体类型,内容)
        $fun = "image".$type;  
        //保存水印图片  
        $fun($image,"$afterUrl"."$afterName");
        $name = $beforeName;
        $type = $_FILES['photo']['type'];
        $url = "$beforeUrl"."$beforeName";
        $afterurl = "$afterUrl"."$afterName";
        $sqlstr = "insert into photo(name,type,url,afterurl) values('".$name."','".$type."','".$url."','".$afterurl."')";  
        @mysql_query($sqlstr) or die(mysql_error());
        header('location:index.php');
    }
}
else{  
//显示图片列表及上传表单
?>  
<!DOCTYPE>  
<html>  
<head>  
<meta http-equiv="content-type" content="text/html; charset=utf-8">  
<title>图片上传功能</title>
<style>
body {
    text-align: center;
}
.submit {
    position: fixed;
    right: 10px;
    top: 10px;
    padding: 10px;
    margin: 0;
    border: 1px solid black;
}
.list {
    border: 0px solid black;
    padding: 0;
}
table,tr,td {
    border: 1px solid black;
}
table {
    width: 800px;
}
.one,.two {
    width: 50%;
}
</style>
</head>
<body>
<div class="submit">   
<form name="form" method="post" action="index.php" enctype="multipart/form-data">  
    <p>图片:<input type="file" name="photo"></p>  
    <p>
        <input type="hidden" name="action" value="add">
        <input type="submit" name="submit" value="提交">
    </p>  
</form>
</div>
<?php  
    $sqlstr = "select * from photo order by id asc";  
    $query = mysql_query($sqlstr) or die(mysql_error());  
    $result = array();  
    while($thread = mysql_fetch_assoc($query)){  
        $result[] = $thread;
    }
    if(empty($result)){
        echo "<h1>没有数据</h1>";
    }else{
foreach($result as $key => $val){
$key = $key+1;
echo <<<EOF
<div class="list">
    <table>
        <tr>
            <td class="one">序号:{$key}</td>
            <td class="two">文件名:{$val['name']}</td>
        </tr>
        <tr>
            <td>原始图</td>
            <td>水印图</td>
        </tr>
        <tr>
            <td><img src="{$val['url']}"/></td>
            <td><img src="{$val['afterurl']}"/></td>
        </tr>
    </table>
</div>
EOF;
    }
}
?>
</body>  
</html>  
<?php  
}  
?>

mysql

DROP TABLE IF EXISTS `photo`;
CREATE TABLE `photo` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `type` varchar(100) NOT NULL,
  `url` varchar(100) NOT NULL,
  `afterurl` varchar(100) NOT NULL,
  `binarydata` mediumblob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码
期待你的评论