Chinaunix首页 | 论坛 | 博客
  • 博客访问: 314251
  • 博文数量: 111
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 707
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-26 11:00
个人简介

小伙向前冲呀,小伙向前冲呀。

文章分类

全部博文(111)

文章存档

2014年(43)

2013年(68)

我的朋友

分类: PHP

2013-12-18 16:28:40

转自:


php 上传图片的代码,很简单,实现了基本的文件类型、文件大小的检测,并实现了基本的水印与缩略功能,比较适合初学的朋友参考。

上传页面与处理页面合二为一了,文件名为test_upload_pic.php,完整代码如下:

  1. <?php
  2. /*
  3.  * 参数说明
  4.  * $max_file_size : 上传文件大小限制, 单位BYTE
  5.  * $destination_folder : 上传文件路径
  6.  * $watermark : 是否附加水印(1为加水印,其他为不加水印);
  7.  *
  8.  * 使用说明:
  9.  * 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
  10.  * 2. 将extension_dir =改为你的php_gd2.dll所在目录;
  11.  */
  12. // 上传文件类型列表
  13. $uptypes = array (
  14.     'image/jpg',
  15.     'image/png',
  16.     'image/jpeg',
  17.     'image/pjpeg',
  18.     'image/gif',
  19.     'image/bmp',
  20.     'image/x-png'
  21. );
  22. $max_file_size = 20000000; //上传文件大小限制,单位BYTE
  23. $destination_folder = 'uploadimg/'; //上传文件路径
  24. $watermark = 1; //是否附加水印(1为加水印,其他为不加水印);
  25. $watertype = 1; //水印类型(1为文字,2为图片)
  26. $waterposition = 1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
  27. $waterstring = "/"; //水印字符串
  28. $waterimg = "xplore.gif"; //水印图片
  29. $imgpreview = 1; //是否生成预览图(1为生成,其他为不生成);
  30. $imgpreviewsize = 1 / 2; //缩略图比例
  31. ?>
  32. <html>
  33. <head>
  34. <title>ZwelL图片上传程序</title>
  35. </head>
  36. <body>
  37. <form id="upfile" name="upform" enctype="multipart/form-data" method="post" action="">
  38.   <label for="upfile">上传文件:</label>
  39.   <input type="file" name="upfile" id="fileField" />
  40.   <input type="submit" name="submit" value="上传"/>
  41. </form>
  42. </body></html>
  43. <?php
  44. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  45.     //判断是否有上传文件
  46.     if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {
  47.         $upfile = $_FILES['upfile'];
  48.         print_r($_FILES['upfile']);
  49.         $name = $upfilep['name']; //文件名
  50.         $type = $upfile['type']; //文件类型
  51.         $size = $upfile['size']; //文件大小
  52.         $tmp_name = $upfile['tmp_name']; //临时文件
  53.         $error = $upfile['error']; //出错原因
  54.         if ($max_file_size < $size) { //判断文件的大小
  55.             echo '上传文件太大';
  56.             exit ();
  57.         }
  58.         if (!in_arrar($type, $uptypes)) { //判断文件的类型
  59.             echo '上传文件类型不符' . $type;
  60.             exit ();
  61.         }
  62.         if (!file_exists($destination_folder)) {
  63.             mkdir($destination_folder);
  64.         }
  65.         if (file_exists("upload/" . $_FILES["file"]["name"])) {
  66.             echo $_FILES["file"]["name"] . " already exists. ";
  67.         } else {
  68.             move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
  69.             echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  70.         }
  71.         $pinfo = pathinfo($name);
  72.         $ftype = $pinfo['extension'];
  73.         $destination = $destination_folder . time() . "." . $ftype;
  74.         if (file_exists($destination) && $overwrite != true) {
  75.             echo "同名的文件已经存在了";
  76.             exit ();
  77.         }
  78.         if (!move_uploaded_file($tmp_name, $destination)) {
  79.             echo "移动文件出错";
  80.             exit ();
  81.         }
  82.         $pinfo = pathinfo($destination);
  83.         $fname = $pinfo[basename];
  84.         echo " 已经成功上传
    文件名: "
    . $destination_folder . $fname . "
    ";
  85.         echo " 宽度:" . $image_size[0];
  86.         echo " 长度:" . $image_size[1];
  87.         echo "
    大小:"
    . $file["size"] . " bytes";
  88.         if ($watermark == 1) {
  89.             $iinfo = getimagesize($destination, $iinfo);
  90.             $nimage = imagecreatetruecolor($image_size[0], $image_size[1]);
  91.             $white = imagecolorallocate($nimage, 255, 255, 255);
  92.             $black = imagecolorallocate($nimage, 0, 0, 0);
  93.             $red = imagecolorallocate($nimage, 255, 0, 0);
  94.             imagefill($nimage, 0, 0, $white);
  95.             switch ($iinfo[2]) {
  96.                 case 1 :
  97.                     $simage = imagecreatefromgif($destination);
  98.                     break;
  99.                 case 2 :
  100.                     $simage = imagecreatefromjpeg($destination);
  101.                     break;
  102.                 case 3 :
  103.                     $simage = imagecreatefrompng($destination);
  104.                     break;
  105.                 case 6 :
  106.                     $simage = imagecreatefromwbmp($destination);
  107.                     break;
  108.                 default :
  109.                     die("不支持的文件类型");
  110.                     exit;
  111.             }
  112.             imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);
  113.             imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $image_size[1], $white);
  114.             switch ($watertype) {
  115.                 case 1 : //加水印字符串
  116.                     imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);
  117.                     break;
  118.                 case 2 : //加水印图片
  119.                     $simage1 = imagecreatefromgif("xplore.gif");
  120.                     imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
  121.                     imagedestroy($simage1);
  122.                     break;
  123.             }
  124.             switch ($iinfo[2]) {
  125.                 case 1 :
  126.                     //imagegif($nimage, $destination);
  127.                     imagejpeg($nimage, $destination);
  128.                     break;
  129.                 case 2 :
  130.                     imagejpeg($nimage, $destination);
  131.                     break;
  132.                 case 3 :
  133.                     imagepng($nimage, $destination);
  134.                     break;
  135.                 case 6 :
  136.                     imagewbmp($nimage, $destination);
  137.                     //imagejpeg($nimage, $destination);
  138.                     break;
  139.             }
  140.             //覆盖原上传文件
  141.             imagedestroy($nimage);
  142.             imagedestroy($simage);
  143.         }
  144.         if ($imgpreview == 1) {
  145.             echo "
    图片预览:
    "
    ;
  146.             echo " . $destination . "\" width=" . ($image_size[0] * $imgpreviewsize) . " height=" . ($image_size[1] * $imgpreviewsize);
  147.             echo " alt=\"图片预览:\r文件名:" . $destination . "\r上传时间:\">";
  148.         }
  149.     }
  150. }
  151. ?>
  152. </body>
  153. </html>
您可能感兴趣的文章:










阅读(1443) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~