PHP:实现给上传图片加水印的程序代码

时间:2007-10-28 23:04:00  来源:  作者:

  用PHP给上传图片加水印的程序是通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面。如果你对PHP的GD库比较熟悉,看懂这篇文章一点都不难了!qKY第一天空网络

/***************************************************** qKY第一天空网络
参数说明: qKY第一天空网络
$max_file_size  : 上传文件大小限制, 单位BYTE qKY第一天空网络
$destination_folder : 上传文件路径 qKY第一天空网络
$watermark   : 是否附加水印(1为加水印,其他为不加水印); qKY第一天空网络
使用说明: qKY第一天空网络
1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; qKY第一天空网络
2. 将extension_dir =改为你的php_gd2.dll所在目录; qKY第一天空网络
****************************************************/ qKY第一天空网络
//上传文件类型列表 qKY第一天空网络
$uptypes=array( qKY第一天空网络
    'image/jpg',  qKY第一天空网络
    'image/jpeg', qKY第一天空网络
    'image/png', qKY第一天空网络
    'image/pjpeg', qKY第一天空网络
    'image/gif', qKY第一天空网络
    'image/bmp', qKY第一天空网络
    'image/x-png' qKY第一天空网络
); qKY第一天空网络
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE qKY第一天空网络
$destination_folder="uploadimg/"; //上传文件路径 qKY第一天空网络
$watermark=1;      //是否附加水印(1为加水印,其他为不加水印); qKY第一天空网络
$watertype=1;      //水印类型(1为文字,2为图片) qKY第一天空网络
$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); qKY第一天空网络
$waterstring="http://www.webjx.com/";  //水印字符串 qKY第一天空网络
$waterimg="xplore.gif";    //水印图片 qKY第一天空网络
$imgpreview=1;      //是否生成预览图(1为生成,其他为不生成); qKY第一天空网络
$imgpreviewsize=1/2;    //缩略图比例 qKY第一天空网络
?> qKY第一天空网络
<html> qKY第一天空网络
<head> qKY第一天空网络
<title>ZwelL图片上传程序</title> qKY第一天空网络
<style type="text/css"> qKY第一天空网络
<!-- qKY第一天空网络
body qKY第一天空网络
{ qKY第一天空网络
     font-size: 9pt; qKY第一天空网络
} qKY第一天空网络
input qKY第一天空网络
{ qKY第一天空网络
     background-color: #66CCFF; qKY第一天空网络
     border: 1px inset #CCCCCC; qKY第一天空网络
} qKY第一天空网络
--> qKY第一天空网络
</style> qKY第一天空网络
</head> qKY第一天空网络
<body> qKY第一天空网络
<form enctype="multipart/form-data" method="post" name="upform"> qKY第一天空网络
  上传文件: qKY第一天空网络
  <input name="upfile" type="file"> qKY第一天空网络
  <input type="submit" value="上传"><br> qKY第一天空网络
  允许上传的文件类型为:<?=implode(', ',$uptypes)?> qKY第一天空网络
</form> qKY第一天空网络
<?php qKY第一天空网络
if ($_SERVER['REQUEST_METHOD'] == 'POST') qKY第一天空网络
{ qKY第一天空网络
    if (!is_uploaded_file($_FILES["upfile"][tmp_name])) qKY第一天空网络
    //是否存在文件 qKY第一天空网络
    { qKY第一天空网络
         echo "图片不存在!"; qKY第一天空网络
         exit; qKY第一天空网络
    } qKY第一天空网络
    $file = $_FILES["upfile"]; qKY第一天空网络
    if($max_file_size < $file["size"]) qKY第一天空网络
    //检查文件大小 qKY第一天空网络
    { qKY第一天空网络
        echo "文件太大!"; qKY第一天空网络
        exit; qKY第一天空网络
    } qKY第一天空网络
    if(!in_array($file["type"], $uptypes)) qKY第一天空网络
    //检查文件类型 qKY第一天空网络
    { qKY第一天空网络
        echo "文件类型不符!".$file["type"]; qKY第一天空网络
        exit; qKY第一天空网络
    } qKY第一天空网络
    if(!file_exists($destination_folder)) qKY第一天空网络
    { qKY第一天空网络
        mkdir($destination_folder); qKY第一天空网络
    } qKY第一天空网络
    $filename=$file["tmp_name"]; qKY第一天空网络
    $image_size = getimagesize($filename); qKY第一天空网络
    $pinfo=pathinfo($file["name"]); qKY第一天空网络
    $ftype=$pinfo['extension']; qKY第一天空网络
    $destination = $destination_folder.time().".".$ftype; qKY第一天空网络
    if (file_exists($destination) && $overwrite != true) qKY第一天空网络
    { qKY第一天空网络
        echo "同名文件已经存在了"; qKY第一天空网络
        exit; qKY第一天空网络
    } qKY第一天空网络
    if(!move_uploaded_file ($filename, $destination)) qKY第一天空网络
    { qKY第一天空网络
        echo "移动文件出错"; qKY第一天空网络
        exit; qKY第一天空网络
    } qKY第一天空网络
    $pinfo=pathinfo($destination); qKY第一天空网络
    $fname=$pinfo[basename]; qKY第一天空网络
    echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>"; qKY第一天空网络
    echo " 宽度:".$image_size[0]; qKY第一天空网络
    echo " 长度:".$image_size[1]; qKY第一天空网络
    echo "<br> 大小:".$file["size"]." bytes"; qKY第一天空网络
    if($watermark==1) qKY第一天空网络
    { qKY第一天空网络
        $iinfo=getimagesize($destination,$iinfo); qKY第一天空网络
        $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); qKY第一天空网络
        $white=imagecolorallocate($nimage,255,255,255); qKY第一天空网络
        $black=imagecolorallocate($nimage,0,0,0); qKY第一天空网络
        $red=imagecolorallocate($nimage,255,0,0); qKY第一天空网络
        imagefill($nimage,0,0,$white); qKY第一天空网络
        switch ($iinfo[2]) qKY第一天空网络
        { qKY第一天空网络
            case 1: qKY第一天空网络
            $simage =imagecreatefromgif($destination); qKY第一天空网络
            break; qKY第一天空网络
            case 2: qKY第一天空网络
            $simage =imagecreatefromjpeg($destination); qKY第一天空网络
            break; qKY第一天空网络
            case 3: qKY第一天空网络
            $simage =imagecreatefrompng($destination); qKY第一天空网络
            break; qKY第一天空网络
            case 6: qKY第一天空网络
            $simage =imagecreatefromwbmp($destination); qKY第一天空网络
            break; qKY第一天空网络
            default: qKY第一天空网络
            die("不支持的文件类型"); qKY第一天空网络
            exit; qKY第一天空网络
        } qKY第一天空网络
        imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); qKY第一天空网络
        imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); qKY第一天空网络
        switch($watertype) qKY第一天空网络
        { qKY第一天空网络
            case 1:   //加水印字符串 qKY第一天空网络
            imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); qKY第一天空网络
            break; qKY第一天空网络
            case 2:   //加水印图片 qKY第一天空网络
            $simage1 =imagecreatefromgif("xplore.gif"); qKY第一天空网络
            imagecopy($nimage,$simage1,0,0,0,0,85,15); qKY第一天空网络
            imagedestroy($simage1); qKY第一天空网络
            break; qKY第一天空网络
        } qKY第一天空网络
        switch ($iinfo[2]) qKY第一天空网络
        { qKY第一天空网络
            case 1: qKY第一天空网络
            //imagegif($nimage, $destination); qKY第一天空网络
            imagejpeg($nimage, $destination); qKY第一天空网络
            break; qKY第一天空网络
            case 2: qKY第一天空网络
            imagejpeg($nimage, $destination); qKY第一天空网络
            break; qKY第一天空网络
            case 3: qKY第一天空网络
            imagepng($nimage, $destination); qKY第一天空网络
            break; qKY第一天空网络
            case 6: qKY第一天空网络
            imagewbmp($nimage, $destination); qKY第一天空网络
            //imagejpeg($nimage, $destination); qKY第一天空网络
            break; qKY第一天空网络
        } qKY第一天空网络
        //覆盖原上传文件 qKY第一天空网络
        imagedestroy($nimage); qKY第一天空网络
        imagedestroy($simage); qKY第一天空网络
    } qKY第一天空网络
    if($imgpreview==1) qKY第一天空网络
    { qKY第一天空网络
    echo "<br>图片预览:<br>"; qKY第一天空网络
    echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize); qKY第一天空网络
    echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; qKY第一天空网络
    } qKY第一天空网络
} qKY第一天空网络
?> qKY第一天空网络
</body> qKY第一天空网络
</html> qKY第一天空网络

文章评论

共有 位天空网友发表了评论 查看完整内容

特别推荐
  • 文字广告
  • 文字广告
  • 文字广告
  • 文字广告
站长黑板报

24小时热门信息