查看: 134|回复: 0

PHP实例解析:实现给上传图片加水印图案

[复制链接]
发表于 2009-5-10 02:06:53 | 显示全部楼层 |阅读模式
给图片增加水印相信大家都了解。这里教大家给上传的图片加水印图案。 <br />用PHP给上传图片加水印的程序是通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面。如果你对PHP的GD库比较熟悉,看懂这篇文章一点都不难了! <br />以下是引用片段: <br />
以下为引用的内容:<br />/***************************************************** <br />参数说明: <br />$max_file_size : 上传文件大小限制, 单位BYTE <br />$destination_folder : 上传文件路径 <br />$watermark : 是否附加水印(1为加水印,其他为不加水印); <br />使用说明: <br />1. 将PHP.INI文件里面的\&quot;extension=php_gd2.dll\&quot;一行前面的;号去掉,因为我们要用到GD库; <br />2. 将extension_dir =改为你的php_gd2.dll所在目录; <br />3. http://www.knowsky.com/php.asp; <br />****************************************************/ <br />//上传文件类型列表 <br />$uptypes=array( <br />\&#39;image/jpg\&#39;, <br />\&#39;image/jpeg\&#39;, <br />\&#39;image/png\&#39;, <br />\&#39;image/pjpeg\&#39;, <br />\&#39;image/gif\&#39;, <br />\&#39;image/bmp\&#39;, <br />\&#39;image/x-png\&#39; <br />); <br />$max_file_size=2000000; //上传文件大小限制, 单位BYTE <br />$destination_folder=\&quot;uploadimg/\&quot;; //上传文件路径 <br />$watermark=1; //是否附加水印(1为加水印,其他为不加水印); <br />$watertype=1; //水印类型(1为文字,2为图片) <br />$waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); <br />$waterstring=\&quot;http://www.xplore.cn/\&quot;; //水印字符串 <br />$waterimg=\&quot;xplore.gif\&quot;; //水印图片 <br />$imgpreview=1; //是否生成预览图(1为生成,其他为不生成); <br />$imgpreviewsize=1/2; //缩略图比例 <br />?&gt; <br /> <br />&lt;html&gt; <br />&lt;head&gt; <br />&lt;title&gt;ZwelL图片上传程序&lt;/title&gt; <br />&lt;style type=\&quot;text/css\&quot;&gt; <br />&lt;!-- <br />body <br />{ <br />     font-size: 9pt; <br />} <br />input <br />{ <br />     background-color: #66CCFF; <br />     border: 1px inset #CCCCCC; <br />} <br />--&gt; <br />&lt;/style&gt; <br />&lt;/head&gt; <br />&lt;body&gt; <br />&lt;form enctype=\&quot;multipart/form-data\&quot; method=\&quot;post\&quot; name=\&quot;upform\&quot;&gt; <br />  上传文件: <br />  &lt;input name=\&quot;upfile\&quot; type=\&quot;file\&quot;&gt; <br />  &lt;input type=\&quot;submit\&quot; value=\&quot;上传\&quot;&gt;&lt;br&gt; <br />  允许上传的文件类型为:&lt;?=implode(\&#39;, \&#39;,$uptypes)?&gt; <br />&lt;/form&gt; <br />&lt;?php <br />if ($_SERVER[\&#39;REQUEST_METHOD\&#39;] == \&#39OST\&#39;) <br />{ <br />    if (!is_uploaded_file($_FILES[\&quot;upfile\&quot;][tmp_name])) <br />    //是否存在文件 <br />    { <br />         echo \&quot;图片不存在!\&quot;; <br />         exit; <br />    } <br />    $file = $_FILES[\&quot;upfile\&quot;]; <br />    if($max_file_size &lt; $file[\&quot;size\&quot;]) <br />    //检查文件大小 <br />    { <br />        echo \&quot;文件太大!\&quot;; <br />        exit; <br />    } <br />    if(!in_array($file[\&quot;type\&quot;], $uptypes)) <br />    //检查文件类型 <br />    { <br />        echo \&quot;文件类型不符!\&quot;.$file[\&quot;type\&quot;]; <br />        exit; <br />    } <br />    if(!file_exists($destination_folder)) <br />    { <br />        mkdir($destination_folder); <br />    } <br />    $filename=$file[\&quot;tmp_name\&quot;]; <br />    $image_size = getimagesize($filename); <br />    $pinfo=pathinfo($file[\&quot;name\&quot;]); <br />    $ftype=$pinfo[\&#39;extension\&#39;]; <br />    $destination = $destination_folder.time().\&quot;.\&quot;.$ftype; <br />    if (file_exists($destination) &amp;&amp; $overwrite != true) <br />    { <br />        echo \&quot;同名文件已经存在了\&quot;; <br />        exit; <br />    } <br />    if(!move_uploaded_file ($filename, $destination)) <br />    { <br />        echo \&quot;移动文件出错\&quot;; <br />        exit; <br />    } <br />    $pinfo=pathinfo($destination); <br />    $fname=$pinfo[basename]; <br />    echo \&quot; &lt;font color=red&gt;已经成功上传&lt;/font&gt;&lt;br&gt;文件名:  <br />&lt;font color=blue&gt;\&quot;.$destination_folder.$fname.\&quot;&lt;/font&gt;&lt;br&gt;\&quot;; <br />    echo \&quot; 宽度:\&quot;.$image_size[0]; <br />    echo \&quot; 长度:\&quot;.$image_size[1]; <br />    echo \&quot;&lt;br&gt; 大小:\&quot;.$file[\&quot;size\&quot;].\&quot; bytes\&quot;; <br />    if($watermark==1) <br />    { <br />        $iinfo=getimagesize($destination,$iinfo); <br />        $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); <br />        $white=imagecolorallocate($nimage,255,255,255); <br />        $black=imagecolorallocate($nimage,0,0,0); <br />        $red=imagecolorallocate($nimage,255,0,0); <br />        imagefill($nimage,0,0,$white); <br />        switch ($iinfo[2]) <br />        { <br />            case 1: <br />            $simage =imagecreatefromgif($destination); <br />            break; <br />            case 2: <br />            $simage =imagecreatefromjpeg($destination); <br />            break; <br />            case 3: <br />            $simage =imagecreatefrompng($destination); <br />            break; <br />            case 6: <br />            $simage =imagecreatefromwbmp($destination); <br />            break; <br />            default: <br />            die(\&quot;不支持的文件类型\&quot;); <br />            exit; <br />        } <br />        imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); <br />        imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); <br />        switch($watertype) <br />        { <br />            case 1:   //加水印字符串 <br />            imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); <br />            break; <br />            case 2:   //加水印图片 <br />            $simage1 =imagecreatefromgif(\&quot;xplore.gif\&quot;); <br />            imagecopy($nimage,$simage1,0,0,0,0,85,15); <br />            imagedestroy($simage1); <br />            break; <br />        } <br />        switch ($iinfo[2]) <br />        { <br />            case 1: <br />            //imagegif($nimage, $destination); <br />            imagejpeg($nimage, $destination); <br />            break; <br />            case 2: <br />            imagejpeg($nimage, $destination); <br />            break; <br />            case 3: <br />            imagepng($nimage, $destination); <br />            break; <br />            case 6: <br />            imagewbmp($nimage, $destination); <br />            //imagejpeg($nimage, $destination); <br />            break; <br />        } <br />        //覆盖原上传文件 <br />        imagedestroy($nimage); <br />        imagedestroy($simage); <br />    } <br />    if($imgpreview==1) <br />    { <br />    echo \&quot;&lt;br&gt;图片预览:&lt;br&gt;\&quot;; <br />    echo \&quot;&lt;ccid_file values=\&quot;\\\&quot; width=\&quot;.($image_size[0]*$imgpreviewsize).\&quot;<br />height=\&quot;.($image_size[1]*$imgpreviewsize);\&quot; <br />    echo \&quot; alt=\\\&quot;图片预览:\\r文件名:\&quot;.$destination.\&quot;\\r上传时间:\\\&quot; /&gt;\&quot;; <br />    } <br />} <br />?&gt; <br />&lt;/body&gt; <br />&lt;/html&gt; <br />
<br /><br /><br /><blockquote class="blockquote">From: http://www.lantuz.com/read.php?tid=4142&newpost=1  Powered by PHPWind.com</blockquote>
回复

使用道具 举报

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖
关注公众号
QQ客服返回顶部