php 上传图片的代码

WBOY
Freigeben: 2016-07-25 09:04:11
Original
917 Leute haben es durchsucht
  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 "
    大小:" . $file["size"] . " bytes";
  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);
  80. $red = imagecolorallocate($nimage, 255, 0, 0);
  81. imagefill($nimage, 0, 0, $white);
  82. switch ($iinfo[2]) {
  83. case 1 :
  84. $simage = imagecreatefromgif($destination);
  85. break;
  86. case 2 :
  87. $simage = imagecreatefromjpeg($destination);
  88. break;
  89. case 3 :
  90. $simage = imagecreatefrompng($destination);
  91. break;
  92. case 6 :
  93. $simage = imagecreatefromwbmp($destination);
  94. break;
  95. default :
  96. die("不支持的文件类型");
  97. exit;
  98. }
  99. imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);

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

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

  104. break;
  105. case 2 : //加水印图片
  106. $simage1 = imagecreatefromgif("xplore.gif");

  107. imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
  108. imagedestroy($simage1);
  109. break;
  110. }
  111. switch ($iinfo[2]) {

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

  115. break;
  116. case 2 :
  117. imagejpeg($nimage, $destination);
  118. break;
  119. case 3 :
  120. imagepng($nimage, $destination);
  121. break;
  122. case 6 :
  123. imagewbmp($nimage, $destination);
  124. //imagejpeg($nimage, $destination);
  125. break;
  126. }
  127. //覆盖原上传文件

  128. imagedestroy($nimage);
  129. imagedestroy($simage);
  130. }
  131. if ($imgpreview == 1) {

  132. echo "
    图片预览:
    ";
  133. echo "\"图片预览:\r文件名:"";
  134. }
  135. }
  136. }
  137. ?>
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!