php 上傳圖片的代碼

來源:互聯網
上載者:User
  1. /*

  2. * 參數說明
  3. * $max_file_size : 上傳檔案大小限制, 單位BYTE
  4. * $destination_folder : 上傳檔案路徑
  5. * $watermark : 是否附加浮水印(1為加浮水印,其他為不加浮水印);
  6. * http://bbs.it-home.org
  7. * 使用說明:
  8. * 1. 將PHP.INI檔案裡面的"extension=php_gd2.dll"一行前面的;號去掉,因為我們要用到GD庫;
  9. * 2. 將extension_dir =改為你的php_gd2.dll所在目錄;
  10. */
  11. // 上傳檔案類型列表
  12. $uptypes = array (
  13. 'image/jpg',
  14. 'image/png',
  15. 'image/jpeg',
  16. 'image/pjpeg',
  17. 'image/gif',
  18. 'image/bmp',
  19. 'image/x-png'
  20. );
  21. $max_file_size = 20000000; //上傳檔案大小限制,單位BYTE
  22. $destination_folder = 'uploadimg/'; //上傳檔案路徑
  23. $watermark = 1; //是否附加浮水印(1為加浮水印,其他為不加浮水印);
  24. $watertype = 1; //浮水印類型(1為文字,2為圖片)
  25. $waterposition = 1; //浮水印位置(1為左下角,2為右下角,3為左上方,4為右上方,5為置中);
  26. $waterstring = "http://bbs.it-home.org/"; //浮水印字串
  27. $waterimg = "xplore.gif"; //浮水印圖片
  28. $imgpreview = 1; //是否產生預覽圖(1為產生,其他為不產生);
  29. $imgpreviewsize = 1 / 2; //縮圖比例
  30. ?>
  31. ZwelL圖片上傳程式
  32. if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  33. //判斷是否有上傳檔案

  34. if (is_uploaded_file($_FILES['upfile']['tmp_name'])) {

  35. $upfile = $_FILES['upfile'];
  36. print_r($_FILES['upfile']);
  37. $name = $upfilep['name']; //檔案名稱
  38. $type = $upfile['type']; //檔案類型
  39. $size = $upfile['size']; //檔案大小
  40. $tmp_name = $upfile['tmp_name']; //臨時檔案
  41. $error = $upfile['error']; //出錯原因

  42. if ($max_file_size < $size) { //判斷檔案的大小

  43. echo '上傳檔案太大';
  44. exit ();
  45. }

  46. if (!in_arrar($type, $uptypes)) { //判斷檔案的類型

  47. echo '上傳檔案類型不符' . $type;
  48. exit ();
  49. }

  50. if (!file_exists($destination_folder)) {

  51. mkdir($destination_folder);
  52. }

  53. if (file_exists("upload/" . $_FILES["file"]["name"])) {

  54. echo $_FILES["file"]["name"] . " already exists. ";
  55. } else {
  56. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
  57. echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  58. }

  59. $pinfo = pathinfo($name);

  60. $ftype = $pinfo['extension'];
  61. $destination = $destination_folder . time() . "." . $ftype;
  62. if (file_exists($destination) && $overwrite != true) {
  63. echo "同名的檔案已經存在了";
  64. exit ();
  65. }

  66. if (!move_uploaded_file($tmp_name, $destination)) {

  67. echo "移動檔案出錯";
  68. exit ();
  69. }

  70. $pinfo = pathinfo($destination);

  71. $fname = $pinfo[basename];
  72. echo " 已經成功上傳
    檔案名稱: " . $destination_folder . $fname . "
    ";
  73. echo " 寬度:" . $image_size[0];
  74. echo " 長度:" . $image_size[1];
  75. echo "
    大小:" . $file["size"] . " bytes";

  76. if ($watermark == 1) {

  77. $iinfo = getimagesize($destination, $iinfo);
  78. $nimage = imagecreatetruecolor($image_size[0], $image_size[1]);
  79. $white = imagecolorallocate($nimage, 255, 255, 255);
  80. $black = imagecolorallocate($nimage, 0, 0, 0);
  81. $red = imagecolorallocate($nimage, 255, 0, 0);
  82. imagefill($nimage, 0, 0, $white);
  83. switch ($iinfo[2]) {
  84. case 1 :
  85. $simage = imagecreatefromgif($destination);
  86. break;
  87. case 2 :
  88. $simage = imagecreatefromjpeg($destination);
  89. break;
  90. case 3 :
  91. $simage = imagecreatefrompng($destination);
  92. break;
  93. case 6 :
  94. $simage = imagecreatefromwbmp($destination);
  95. break;
  96. default :
  97. die("不支援的檔案類型");
  98. exit;
  99. }

  100. imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);

  101. imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $image_size[1], $white);

  102. switch ($watertype) {

  103. case 1 : //加浮水印字串

  104. imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);

  105. break;
  106. case 2 : //加浮水印圖片

  107. $simage1 = imagecreatefromgif("xplore.gif");

  108. imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
  109. imagedestroy($simage1);
  110. break;
  111. }

  112. switch ($iinfo[2]) {

  113. case 1 :
  114. //imagegif($nimage, $destination);

  115. imagejpeg($nimage, $destination);

  116. break;
  117. case 2 :
  118. imagejpeg($nimage, $destination);
  119. break;
  120. case 3 :
  121. imagepng($nimage, $destination);
  122. break;
  123. case 6 :
  124. imagewbmp($nimage, $destination);
  125. //imagejpeg($nimage, $destination);
  126. break;
  127. }

  128. //覆蓋原上傳檔案

  129. imagedestroy($nimage);
  130. imagedestroy($simage);
  131. }

  132. if ($imgpreview == 1) {

  133. echo "
    圖片預覽:
    ";
  134. echo " echo " alt=\"圖片預覽:\r檔案名稱:" . $destination . "\r上傳時間:\">";
  135. }
  136. }
  137. }
  138. ?>
複製代碼

您可能感興趣的文章:php 多圖片上傳的簡單例子(圖文)php檔案上傳時遇到的一個問題(move_uploaded_file)php普通表單多檔案上傳的代碼瞭解PHP檔案上傳的原理php簡單檔案上傳的例子php判斷上傳檔案的檔案類型的幾種方法一個php上傳下載檔案的源碼一個好用的php檔案上傳處理類php上傳多個檔案的代碼php多檔案上傳的三種方法php上傳圖片功能的實現

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.