PHP 上传图片,生成水印,支持文字, gif, png

时间:2023-03-18 19:29:02
  1. //admin_upfile.php
  2. <html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php
    //这里上传 $upsize判断上传文件的大小
    $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] : "/attached/"; //文件上传路径
    $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : $_REQUEST["formName"]; //回传到上页面编辑框所在Form的Name
    $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"]; //回传到上页面编辑框的Name
    //转换根目录的路径
    if (strpos($uppath, "/") == ) {
    $i = ;
    $thpath = $_SERVER["SCRIPT_NAME"];
    $thpath = substr($thpath, , strlen($thpath));
    while (strripos($thpath, "/") !== false) {
    $thpath = substr($thpath, strpos($thpath, "/") + , strlen($thpath));
    $i = ++$i;
    } $pp = "";
    for ($j = ; $j < $i; ++$j) {
    $pp .="../";
    } $uppaths = $pp . substr($uppath, , strlen($thpath));
    }
    $filename = date("y-m");
    if (is_dir($uppaths . $filename) != TRUE)
    mkdir($uppaths . $filename, );
    // if(is_dir($filename."/".$ctime)!=TRUE) mkdir($filename."/".$ctime,0777); $f = $_FILES['file1']; /*if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png") {
    echo "<script>alert('只能上传图片格式的文件');window.close()</script>";
    //echo $f['type'];
    return false;
    }
    */ //获得文件扩展名
    $temp_arr = explode(".", $f["name"]);
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext); //新文件名
    $new_file_name = md5(date("YmdHis") . '_' . rand(, )) . '.' . $file_ext; $dest = $uppaths . $filename . "/" . $new_file_name; //设置文件名为日期加上文件名避免重复 上传目录
    $dest1 = $uppath . $filename . "/" . $new_file_name; //设置文件名为日期加上文件名避免重复 $image_size = getimagesize($f['tmp_name']);
    $r = move_uploaded_file($f['tmp_name'], $dest);
    if($r){
    $uptypes=array(
    'image/jpg',
    'image/jpeg',
    'image/png',
    'image/pjpeg',
    'image/gif',
    'image/bmp',
    'image/x-png'
    );
    $max_file_size=; //上传文件大小限制, 单位BYTE
    $destination_folder="attached/"; //上传文件路径
    $watermark=; //是否附加水印(1为加水印,其他为不加水印);
    $watertype=; //水印类型(1为文字,2为图片)
    $waterposition=; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
    $waterstring="Thinfo.org"; //水印字符串
    $waterimg="wart.png"; //水印图片
    $imgpreview=; //是否生成预览图(1为生成,其他为不生成);
    $imgpreviewsize=/; //缩略图比例
    if($watermark==)
    {
    $iinfo=getimagesize($dest,$iinfo); $nimage=imagecreatetruecolor($image_size[],$image_size[]);
    $white=imagecolorallocate($nimage,,,);
    $black=imagecolorallocate($nimage,,,);
    $red=imagecolorallocate($nimage,,,);
    imagefill($nimage,,,$white);
    switch ($iinfo[])
    {
    case :
    $simage =imagecreatefromgif($dest);
    break;
    case :
    $simage =imagecreatefromjpeg($dest);
    break;
    case :
    $simage =imagecreatefrompng($dest);
    break;
    case :
    $simage =imagecreatefromwbmp($dest);
    break;
    default:
    die("不支持的文件类型");
    exit;
    }
    imagecopy($nimage,$simage,,,,,$image_size[],$image_size[]);
    //imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
    switch($watertype)
    {
    case : //加水印字符串
    //imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
    break;
    case : //加水印图片
    $simage1 =imagecreatefrompng('wart.png');
    imagecopy($nimage,$simage1,$image_size[]-,$image_size[]-,,,,);
    imagedestroy($simage1);
    break;
    }
    switch ($iinfo[])
    {
    case :
    //imagegif($nimage, $destination);
    imagejpeg($nimage, $dest);
    break;
    case :
    imagejpeg($nimage, $dest);
    break;
    case :
    imagepng($nimage, $dest);
    break;
    case :
    imagewbmp($nimage, $dest);
    //imagejpeg($nimage, $destination);
    break;
    }
    //覆盖原上传文件
    imagedestroy($nimage);
    imagedestroy($simage);
    } }
    if ($f['size'] > ) { echo "<script>window.opener.document." . $formName . "." . $editName . ".value='" . $dest1 . "'</script>";
    echo "<script>alert('图片上传成功');window.close()</script>";
    }
    ?>
    </html>

    admin_upload.php

  3. <html>
    <head>
    <title>图片上传</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php
    $uppath=isset($_REQUEST["upPath"])?$_REQUEST["upPath"]."/":"/attached/"; //文件上传路径
    $formName=isset($_REQUEST["formName"])?$_REQUEST["formName"]:"myform"; //回传到上页面编辑框所在Form的Name
    $editName=isset($_REQUEST["editName"])?$_REQUEST["editName"]:"pic"; //回传到上页面编辑框的Name
    //示例
    /* <form name="myform">
    <input name="pic" Type="text" size="50" maxlength="50" >
    <input type="button" name="Submit" value="点击上传图片" onClick="window.open('Admin_upload.php?formName=myform&editName=pic&upPath=/uploadfile','','status=no,scrollbars=no,top=20,left=110,width=420,height=165')">
    </form> */
    ?>
    <script language="javascript">
    <!--
    function mysub()
    {
    esave.style.visibility="visible";
    }
    -->
    </script>
    <style type="text/css">
    <!--
    .STYLE2 {
    font-size: 12pt
    }
    .STYLE3 {
    font-size: 14pt
    }
    -->
    </style>
    </head>
    <body oncontextmenu='return false' onselectstart="return false" oncopy="return false;" oncut="return false;">
    <form name="form1" method="post" action="admin_upfile.php" enctype="multipart/form-data" >
    <div id="esave" style="position:absolute; top:18px; left:40px; z-index:10; visibility:hidden">
    <TABLE WIDTH= BORDER= CELLSPACING= CELLPADDING=>
    <TR>
    <td width=?></td>
    <TD bgcolor=#ff0000 width="60%"><TABLE WIDTH=% height= BORDER= CELLSPACING= CELLPADDING=>
    <TR>
    <td bgcolor=#ffffff align=center><font color=red>正在上传文件,请稍候...</font></td>
    </tr>
    </table></td>
    <td width=?></td>
    </tr>
    </table>
    </div>
    <table class="Border" width="100%" border="" height="100%" align="center" cellpadding="" cellspacing="" bgcolor="#FFFFFF">
    <tr>
    <td align="center" class="title" height=""><b><span class="style2 STYLE3">图 片 上 传</span>
    <input type="hidden" name="upPath" value="<?=$uppath?>">
    <input type="hidden" name="editName" value="<?=$editName?>">
    <input type="hidden" name="formName" value="<?=$formName?>">
    </b> </td>
    </tr>
    <tr bgcolor="#E8F1FF">
    <td height="" align="center" id="upid">选择文件:
    <input type="file" name="file1" size="" class="tx1" value="">
    <input type="submit" name="Submit" value="开始上传" class="button" onClick="mysub();">
    <br></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    1. <input name="pic" type="text" id="pic" size="80" />
    2. <input type="button" name="Submit" value="点击上传图片" onclick="window.open('admin_upload.php?formName=myform&editName=pic&upPath=/attached','','status=no,scrollbars=no,top=20,left=110,width=420,height=165')" />