php code to upload images

WBOY
Release: 2016-07-25 09:04:11
Original
916 people have browsed it
  1. /*

  2. * Parameter description
  3. * $max_file_size: Upload file size limit, unit BYTE
  4. * $destination_folder: Upload file path
  5. * $watermark: Whether to attach a watermark ( 1 means adding watermark, others means not adding watermark);
  6. * http://bbs.it-home.org
  7. * Instructions for use:
  8. * 1. In front of the line "extension=php_gd2.dll" in the PHP.INI file Remove the ; sign because we need to use the GD library;
  9. * 2. Change extension_dir = to the directory where your php_gd2.dll is located;
  10. */
  11. // Upload file type list
  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; //Upload file size limit, unit BYTE
  22. $destination_folder = 'uploadimg/'; //Upload file path
  23. $watermark = 1; //Whether to attach a watermark (1 means adding a watermark, others means not adding a watermark );
  24. $watertype = 1; //Watermark type (1 is text, 2 is picture)
  25. $waterposition = 1; //Watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner corner, 5 is centered);
  26. $waterstring = "http://bbs.it-home.org/"; //Watermark string
  27. $waterimg = "xplore.gif"; //Watermark image
  28. $imgpreview = 1 ; //Whether to generate a preview image (1 means generate, others do not generate);
  29. $imgpreviewsize = 1 / 2; //Thumbnail ratio
  30. ?>
  31. ZwelL picture upload program
  32. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  33. //Determine whether there is an uploaded file

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

  35. $upfile = $_FILES['upfile'];
  36. print_r($_FILES['upfile']);
  37. $name = $upfilep['name']; //File name
  38. $type = $upfile[' type']; //File type
  39. $size = $upfile['size']; //File size
  40. $tmp_name = $upfile['tmp_name']; //Temporary file
  41. $error = $upfile['error' ]; //Cause of error

  42. if ($max_file_size < $size) { //Determine the size of the file

  43. echo 'The uploaded file is too large';
  44. exit ();
  45. }< /p>
  46. if (!in_arrar($type, $uptypes)) { //Determine the file type

  47. echo 'Uploaded file type does not match' . $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 "The file with the same name already exists";
  64. exit ();
  65. }

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

  67. echo "Error moving file";
  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 "图片预览:r文件名:";
  135. }
  136. }
  137. }
  138. ?>
复制代码

您可能感兴趣的文章: php 多图片上传的简单例子(图文) php文件上传时遇到的一个问题(move_uploaded_file) php普通表单多文件上传的代码 了解PHP文件上传的原理 php简单文件上传的例子 php判断上传文件的文件类型的几种方法 一个php上传下载文件的源码 一个好用的php文件上传处理类 php上传多个文件的代码 php多文件上传的三种方法 php上传图片功能的实现



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!