|
如果对PHP的GD库比较熟悉,看懂这篇文章一点都不难了! 3nLLinux联盟 <?php 3nLLinux联盟 /********************************************************* 3nLLinux联盟 3nLLinux联盟 参数说明: 3nLLinux联盟 $max_file_size : 上传文件大小限制, 单位BYTE 3nLLinux联盟 $destination_folder : 上传文件路径 3nLLinux联盟 $watermark : 是否附加水印(1为加水印,其他为不加水印); 3nLLinux联盟 3nLLinux联盟 使用说明: 3nLLinux联盟 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 3nLLinux联盟 2. 将extension_dir =改为你的php_gd2.dll所在目录; 3nLLinux联盟 *****************************************************************/ 3nLLinux联盟 3nLLinux联盟 //上传文件类型列表 3nLLinux联盟 $uptypes=array( 3nLLinux联盟 'image/jpg', 3nLLinux联盟 'image/jpeg', 3nLLinux联盟 'image/png', 3nLLinux联盟 'image/pjpeg', 3nLLinux联盟 'image/gif', 3nLLinux联盟 'image/bmp', 3nLLinux联盟 'image/x-png' 3nLLinux联盟 ); 3nLLinux联盟 3nLLinux联盟 $max_file_size=2000000; //上传文件大小限制, 单位BYTE 3nLLinux联盟 $destination_folder="uploadimg/"; //上传文件路径 3nLLinux联盟 $watermark=1; //是否附加水印(1为加水印,其他为不加水印); 3nLLinux联盟 $watertype=1; //水印类型(1为文字,2为图片) 3nLLinux联盟 $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); 3nLLinux联盟 $waterstring="http://www.xplore.cn/"; //水印字符串 3nLLinux联盟 $waterimg="xplore.gif"; //水印图片 3nLLinux联盟 $imgpreview=1; //是否生成预览图(1为生成,其他为不生成); 3nLLinux联盟 $imgpreviewsize=1/2; //缩略图比例 3nLLinux联盟 ?> 3nLLinux联盟 <html> 3nLLinux联盟 <head> 3nLLinux联盟 <title>ZwelL图片上传程序</title> 3nLLinux联盟 <style type="text/css"> 3nLLinux联盟 <!-- 3nLLinux联盟 body 3nLLinux联盟 { 3nLLinux联盟 font-size: 9pt; 3nLLinux联盟 } 3nLLinux联盟 input 3nLLinux联盟 { 3nLLinux联盟 background-color: #66CCFF; 3nLLinux联盟 border: 1px inset #CCCCCC; 3nLLinux联盟 } 3nLLinux联盟 --> 3nLLinux联盟 </style> 3nLLinux联盟 </head> 3nLLinux联盟 3nLLinux联盟 <body> 3nLLinux联盟 <form enctype="multipart/form-data" method="post" name="upform"> 3nLLinux联盟 上传文件: 3nLLinux联盟 <input name="upfile" type="file"> 3nLLinux联盟 <input type="submit" value="上传"><br> 3nLLinux联盟 允许上传的文件类型为:<?=implode(', ',$uptypes)?> 3nLLinux联盟 </form> 3nLLinux联盟 3nLLinux联盟 <?php 3nLLinux联盟 if ($_SERVER['REQUEST_METHOD'] == 'POST') 3nLLinux联盟 { 3nLLinux联盟 if (!is_uploaded_file($_FILES["upfile"][tmp_name])) 3nLLinux联盟 //是否存在文件 3nLLinux联盟 { 3nLLinux联盟 echo "图片不存在!"; 3nLLinux联盟 exit; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 $file = $_FILES["upfile"]; 3nLLinux联盟 if($max_file_size < $file["size"]) 3nLLinux联盟 //检查文件大小 3nLLinux联盟 { 3nLLinux联盟 echo "文件太大!"; 3nLLinux联盟 exit; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 if(!in_array($file["type"], $uptypes)) 3nLLinux联盟 //检查文件类型 3nLLinux联盟 { 3nLLinux联盟 echo "文件类型不符!".$file["type"]; 3nLLinux联盟 exit; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 if(!file_exists($destination_folder)) 3nLLinux联盟 { 3nLLinux联盟 mkdir($destination_folder); 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 $filename=$file["tmp_name"]; 3nLLinux联盟 $image_size = getimagesize($filename); 3nLLinux联盟 $pinfo=pathinfo($file["name"]); 3nLLinux联盟 $ftype=$pinfo['extension']; 3nLLinux联盟 $destination = $destination_folder.time().".".$ftype; 3nLLinux联盟 if (file_exists($destination) && $overwrite != true) 3nLLinux联盟 { 3nLLinux联盟 echo "同名文件已经存在了"; 3nLLinux联盟 exit; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 if(!move_uploaded_file ($filename, $destination)) 3nLLinux联盟 { 3nLLinux联盟 echo "移动文件出错"; 3nLLinux联盟 exit; 3nLLinux联盟 } 3nLLinux联盟 $pinfo=pathinfo($destination); 3nLLinux联盟 $fname=$pinfo[basename]; 3nLLinux联盟 echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>"; 3nLLinux联盟 echo " 宽度:".$image_size[0]; 3nLLinux联盟 echo " 长度:".$image_size[1]; 3nLLinux联盟 echo "<br> 大小:".$file["size"]." bytes"; 3nLLinux联盟 3nLLinux联盟 if($watermark==1) 3nLLinux联盟 { 3nLLinux联盟 $iinfo=getimagesize($destination,$iinfo); 3nLLinux联盟 $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); 3nLLinux联盟 $white=imagecolorallocate($nimage,255,255,255); 3nLLinux联盟 $black=imagecolorallocate($nimage,0,0,0); 3nLLinux联盟 $red=imagecolorallocate($nimage,255,0,0); 3nLLinux联盟 imagefill($nimage,0,0,$white); 3nLLinux联盟 switch ($iinfo[2]) 3nLLinux联盟 { 3nLLinux联盟 case 1: 3nLLinux联盟 $simage =imagecreatefromgif($destination); 3nLLinux联盟 break; 3nLLinux联盟 case 2: 3nLLinux联盟 $simage =imagecreatefromjpeg($destination); 3nLLinux联盟 break; 3nLLinux联盟 case 3: 3nLLinux联盟 $simage =imagecreatefrompng($destination); 3nLLinux联盟 break; 3nLLinux联盟 case 6: 3nLLinux联盟 $simage =imagecreatefromwbmp($destination); 3nLLinux联盟 break; 3nLLinux联盟 default: 3nLLinux联盟 die("不支持的文件类型"; 3nLLinux联盟 exit; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 3nLLinux联盟 imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); 3nLLinux联盟 3nLLinux联盟 switch($watertype) 3nLLinux联盟 { 3nLLinux联盟 case 1: //加水印字符串 3nLLinux联盟 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); 3nLLinux联盟 break; 3nLLinux联盟 case 2: //加水印图片 3nLLinux联盟 $simage1 =imagecreatefromgif("xplore.gif"; 3nLLinux联盟 imagecopy($nimage,$simage1,0,0,0,0,85,15); 3nLLinux联盟 imagedestroy($simage1); 3nLLinux联盟 break; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 switch ($iinfo[2]) 3nLLinux联盟 { 3nLLinux联盟 case 1: 3nLLinux联盟 //imagegif($nimage, $destination); 3nLLinux联盟 imagejpeg($nimage, $destination); 3nLLinux联盟 break; 3nLLinux联盟 case 2: 3nLLinux联盟 imagejpeg($nimage, $destination); 3nLLinux联盟 break; 3nLLinux联盟 case 3: 3nLLinux联盟 imagepng($nimage, $destination); 3nLLinux联盟 break; 3nLLinux联盟 case 6: 3nLLinux联盟 imagewbmp($nimage, $destination); 3nLLinux联盟 //imagejpeg($nimage, $destination); 3nLLinux联盟 break; 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 //覆盖原上传文件 3nLLinux联盟 imagedestroy($nimage); 3nLLinux联盟 imagedestroy($simage); 3nLLinux联盟 } 3nLLinux联盟 3nLLinux联盟 if($imgpreview==1) 3nLLinux联盟 { 3nLLinux联盟 echo "<br>图片预览:<br>"; 3nLLinux联盟 echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize); 3nLLinux联盟 echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; 3nLLinux联盟 } 3nLLinux联盟 } 3nLLinux联盟 ?> 3nLLinux联盟 </body> 3nLLinux联盟 </html> 3nLLinux联盟 3nLLinux联盟 3nLLinux联盟 3nLLinux联盟 最后我觉得有必要说一下给图片加水印的原理: 3nLLinux联盟 3nLLinux联盟 取得上传的文件信息,建立一个真彩色图象,分配颜色方案并且填充图像。 3nLLinux联盟 3nLLinux联盟 通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像 3nLLinux联盟 3nLLinux联盟 程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面。 3nLLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|