首页 > 语言 > PHP > 正文

PHP中上传图片到服务器中程序代码

2024-09-04 11:44:53
字体:大 中 小
来源:转载
供稿:网友

本文章来给大家介绍PHP中上传图片到服务器中程序代码,本程序不但可以上传图片,同时还可以把上传的图片加上水印效果,代码如下:

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  2. <?php 
  3. /****************************************************************************** 
  4. 参数说明: 
  5. $max_file_size  : 上传文件大小限制, 单位BYTE 
  6. $destination_folder : 上传文件路径 
  7. $watermark   : 是否附加水印(1为加水印,其他为不加水印); 
  8.  
  9. 使用说明: 
  10. . 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 
  11. . 将extension_dir =改为你的php_gd2.dll所在目录; 
  12. ******************************************************************************/ 
  13.  
  14. //上传文件类型列表 
  15. $uptypes=array( 
  16.     'image/jpg', 
  17.     'image/jpeg', 
  18.     'image/png', 
  19.     'image/pjpeg', 
  20.     'image/gif', 
  21.     'image/bmp', 
  22.     'image/x-png' 
  23. ); 
  24.  
  25. $max_file_size=2000000;     //上传文件大小限制, 单位BYTE 
  26. $destination_folder="uploadimg/"; //上传文件路径 
  27. $watermark=1;      //是否附加水印(1为加水印,其他为不加水印); 
  28. $watertype=1;      //水印类型(1为文字,2为图片) 
  29. $waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); 
  30. $waterstring="http://www.xplore.cn/";  //水印字符串 
  31. $waterimg="xplore.gif";    //水印图片 
  32. $imgpreview=1;      //是否生成预览图(1为生成,其他为不生成); 
  33. $imgpreviewsize=1/2;    //缩略图比例 
  34. ?> 
  35. <html> 
  36. <head> 
  37. <title>ZwelL图片上传程序</title> 
  38. <style type="text/css"> 
  39. <!-- 
  40. body 
  41. { 
  42.      font-size: 9pt; 
  43. } 
  44. input 
  45. { 
  46.      background-color: #66CCFF; 
  47.      border: 1px inset #CCCCCC; 
  48. } 
  49. --> 
  50. </style> 
  51. </head> 
  52.  
  53. <body> 
  54. <form enctype="multipart/form-data" method="post" name="upform"> 
  55.   上传文件: 
  56.   <input name="upfile" type="file"> 
  57.   <input type="submit" value="上传"><br> 
  58.   允许上传的文件类型为:<?=implode(', ',$uptypes)?> 
  59. </form> 
  60.  
  61. <?php 
  62. if ($_SERVER['REQUEST_METHOD'] == 'POST') 
  63. { 
  64.     if (!is_uploaded_file($_FILES["upfile"][tmp_name])) 
  65.     //是否存在文件 
  66.     { 
  67.          echo "图片不存在!"; 
  68.          exit; 
  69.     } 
  70.  
  71.     $file = $_FILES["upfile"]; 
  72.     if($max_file_size < $file["size"]) 
  73.     //检查文件大小 
  74.     { 
  75.         echo "文件太大!"; 
  76.         exit; 
  77.     } 
  78.  
  79.     if(!in_array($file["type"], $uptypes)) 
  80.     //检查文件类型 
  81.     { 
  82.         echo "文件类型不符!".$file["type"]; 
  83.         exit; 
  84.     } 
  85.  
  86.     if(!file_exists($destination_folder)) 
  87.     { 
  88.         mkdir($destination_folder); 
  89.     } 
  90.  
  91.     $filename=$file["tmp_name"]; 
  92.     $image_size = getimagesize($filename); 
  93.     $pinfo=pathinfo($file["name"]); 
  94.     $ftype=$pinfo['extension']; 
  95.     $destination = $destination_folder.time().".".$ftype; 
  96.     if (file_exists($destination) && $overwrite != true) 
  97.     { 
  98.         echo "同名文件已经存在了"; 
  99.         exit; 
  100.     } 
  101.  
  102.     if(!move_uploaded_file ($filename, $destination)) 
  103.     { 
  104.         echo "移动文件出错"; 
  105.         exit; 
  106.     } 
  107.  
  108.     $pinfo=pathinfo($destination); 
  109.     $fname=$pinfo[basename]; 
  110.     echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>"; 
  111.     echo " 宽度:".$image_size[0]; 
  112.     echo " 长度:".$image_size[1]; 
  113.     echo "<br> 大小:".$file["size"]." bytes"; 
  114.  
  115.     if($watermark==1) 
  116.     { 
  117.         $iinfo=getimagesize($destination,$iinfo); 
  118.         $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); 
  119.         $white=imagecolorallocate($nimage,255,255,255); 
  120.         $black=imagecolorallocate($nimage,0,0,0); 
  121.         $red=imagecolorallocate($nimage,255,0,0); 
  122.         imagefill($nimage,0,0,$white); 
  123.         switch ($iinfo[2]) 
  124.         { 
  125.             case 1: 
  126.             $simage =imagecreatefromgif($destination); 
  127.             break; 
  128.             case 2: 
  129.             $simage =imagecreatefromjpeg($destination); 
  130.             break; 
  131.             case 3: 
  132.             $simage =imagecreatefrompng($destination); 
  133.             break; 
  134.             case 6: 
  135.             $simage =imagecreatefromwbmp($destination); 
  136.             break; 
  137.             default: 
  138.             die("不支持的文件类型"); 
  139.             exit; 
  140.         } 
  141.  
  142.         imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 
  143.         imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); 
  144.  
  145.         switch($watertype) 
  146.         { 
  147.             case 1:   //加水印字符串 
  148.             imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); 
  149.             break; 
  150.             case 2:   //加水印图片 
  151.             $simage1 =imagecreatefromgif("xplore.gif"); 
  152.             imagecopy($nimage,$simage1,0,0,0,0,85,15); 
  153.             imagedestroy($simage1); 
  154.             break; 
  155.         } 
  156.  
  157.         switch ($iinfo[2]) 
  158.         { 
  159.             case 1: 
  160.             //imagegif($nimage, $destination); 
  161.             imagejpeg($nimage, $destination); 
  162.             break; 
  163.             case 2: 
  164.             imagejpeg($nimage, $destination); 
  165.             break; 
  166.             case 3: 
  167.             imagepng($nimage, $destination); 
  168.             break; 
  169.             case 6: 
  170.             imagewbmp($nimage, $destination); 
  171.             //imagejpeg($nimage, $destination); 
  172.             break; 
  173.         } 
  174.  
  175.         //覆盖原上传文件 
  176.         imagedestroy($nimage); 
  177.         imagedestroy($simage); 
  178.     } 
  179.  
  180.     if($imgpreview==1) 
  181.     { 
  182.     echo "<br>图片预览:<br>"; 
  183.     echo "<img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize); 
  184.     echo " alt="图片预览:r文件名:".$destination."r上传时间:">"; 
  185.     } 
  186. } 
  187. ?> 
  188. </body> 
  189. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表