首頁  >  文章  >  後端開發  >  php 上傳圖片的程式碼

php 上傳圖片的程式碼

WBOY
WBOY原創
2016-07-25 09:04:11920瀏覽
  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 echo '上傳檔案太大';

  43. exit ();
  44. }
  45. if (!in_arrar($type, $uptypes)) { //判斷檔案的型別

  46. echo '上傳檔案型別不符' . $type;
  47. exit ();
  48. }
  49. if (!file_exists($destination_folder)) {

  50. mkdir($destination_folder);
  51. }
  52. if (file_exists("upload/" . $ _FILES["file"]["name"])) {

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

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

  66. echo "行動檔案出錯";
  67. exit ();
  68. }
  69. $pinfo = pathinfo($destination);

  70. $fname = $pinfo[basename];
  71. echo " 已成功上傳檔名: " 。 $destination_folder 。 $fname . "
    ";
  72. echo "寬度:" . $image_size[0];
  73. echo "長度:" . $image_size[1];
  74. echo "
    大小:" 。 $檔案[“大小”]。 「位元組」;
  75. if ($watermark == 1) {

  76. $iinfo = getimagesize($destination, $iinfo);
  77. $nimage = imagecreatetruecolor($image_size[ 0], $image_size[1]);
  78. $white = imagecolorallocate($nimage, 255, 255, 255);
  79. $black = imagecolorallocate($nimage, 0, 0, 0); red = imagecolorallocate($nimage, 255, 0, 0);
  80. imagefill($nimage, 0, 0, $white);
  81. switch ($iinfo[2]) {
  82. 情況1 :
  83. $simage = imagecreatefromgif($destination);
  84. 中斷;
  85. 案例2 :
  86. $simage = imagecreatefromjpeg($destination);
  87. 中斷; 中斷;
  88. 案例6 :
  89. $simage = imagecreatefromwbmp($destination);
  90. break;
  91. 預設:
  92. die" ");
  93. exit;
  94. } p>
  95. imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]) ;

  96. imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $ image_size[1], $white);
  97. switch ($watertype) {

  98. case 1 : //加浮水印字串
  99. ; imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);

  100. break;
  101. case 2 : //加浮水印圖片
  102. $simage1 = imagecreatefromgif("xplore.gif");
  103. imagecopy($nimage, $simage1, 0, 0, 0, 0 , 85, 15);
  104. imagedestroy($simage1);
  105. 中斷;
  106. }
  107. switch ($iinfo[2]) {

  108. case 1 :

    switch ($iinfo[2]) {

  109. case 1 :
  110. //imagegif($nimage, $destination);
  111. imagejpeg($nimage, $destination);

  112. 中斷;
  113. 狀況2 :
  114. imagejpeg($nimage , $destination);
  115. 中斷;
  116. 情況3 :
  117. imagepng($nimage , $destination);
  118. 中斷;
  119. 情況6 :
  120. imagewbmp($image, $dedepdemation) ;
  121. //imagejpeg($nimage, $destination);
  122. 中斷;
  123. }
  124. //覆寫原始上傳檔案

  125. imagedestroy($nimage);
  126. imagedestroy($simage);
  127. }
  128. if ($imgpreview == 1) {

  129. echo "
    圖片預覽:
    ";
  130. echo "php 上傳圖片的程式碼";
  131. }
  132. }
  133. }
  134. ?>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn