PHP image upload Program (full version)

Source: Internet
Author: User
Tags imagecopy imagejpeg vars

From the PHP100, the function is very powerful. Almost every detail is taken into account to share with you! ~~~

  1. <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 "/>
  2. <?php
  3. /******************************************************************************
  4. Parameter description:
  5. $max _file_size: Upload file size limit, unit byte
  6. $destination _folder: Upload file path
  7. $watermark: Whether additional watermark (1 is watermark, the other is not watermark);
  8. Instructions for use:
  9. 1. Remove the "Extension=php_gd2.dll" line in the php.ini file, because we need to use the GD library;
  10. 2. Change Extension_dir = to the directory where your Php_gd2.dll is located;
  11. ******************************************************************************/
  12. Upload file Type list
  13. $uptypes =Array (
  14. ' Image/jpg ',
  15. ' Image/jpeg ',
  16. ' Image/png ',
  17. ' Image/pjpeg ',
  18. ' Image/gif ',
  19. ' Image/bmp ',
  20. ' Image/x-png '
  21. );
  22. $max _file_size=2000000; //Upload file size limit, unit byte
  23. $destination _folder="uploadimg/"; //Upload file path
  24. $watermark = 1;  //Whether additional watermark (1 is watermark, the other is not watermark);
  25. $watertype = 1; //Watermark type (1 for text, 2 for picture)
  26. $waterposition = 1;  //Watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, 5 is centered);
  27. $waterstring ="http://www.xplore.cn/"; Watermark String
  28. $waterimg ="Xplore.gif"; //Watermark picture
  29. $imgpreview = 1;  //Whether to generate a preview map (1 is generated, others are not generated);
  30. $imgpreviewsize =1/2; //thumbnail scale
  31. ?>
  32. <title>zwell Picture Uploading program </title>
  33. <style type="Text/css" >
  34. <!--
  35. Body
  36. {
  37. font-size:9pt;
  38. }
  39. Input
  40. {
  41. Background-color: #66CCFF;
  42. border:1px inset #CCCCCC;
  43. }
  44. -
  45. </style>
  46. <body>
  47. <form enctype="Multipart/form-data" method="post" name="Upform" >
  48. Upload file:
  49. <input name="upfile" type="file" >
  50. <input type="Submit" value="upload" ><br>
  51. The file types that are allowed to be uploaded are: <?=implode (', ',$uptypes)?>
  52. </form>
  53. <?php
  54. if ($_server[' request_method '] = = ' POST ')
  55. {
  56. if (! Is_uploaded_file ($_files["Upfile"][tmp_name]))
  57. Whether the file exists
  58. {
  59. echo "Picture does not exist!";
  60. Exit
  61. }
  62. $file = $_files["Upfile"];
  63. if ($max _file_size < $file ["size"])
  64. Check File size
  65. {
  66. echo "file too big!";
  67. Exit
  68. }
  69. if (!in_array ($file ["type"], $uptypes))
  70. Check file types
  71. {
  72. echo "file type does not match!".  $file ["type"];
  73. Exit
  74. }
  75. if (! File_exists ($destination _folder))
  76. {
  77. mkdir ($destination _folder);
  78. }
  79. $filename =$file ["Tmp_name"];
  80. $image _size = getimagesize ($filename);
  81. $pinfo =pathinfo ($file ["name"]);
  82. $ftype =$pinfo [' extension '];
  83. $destination = $destination _folder.time (). ".".  $ftype;
  84. if (file_exists ($destination) && $overwrite! = True)
  85. {
  86. echo "file with the same name already exists";
  87. Exit
  88. }
  89. if (!move_uploaded_file ($filename, $destination))
  90. {
  91. echo "error moving File";
  92. Exit
  93. }
  94. $pinfo =pathinfo ($destination);
  95. $fname =$pinfo [basename];
  96. echo "<font Color=red> has successfully uploaded </font><br> FileName: <font color=blue>". $destination _folder. $fname.  "</font><br>";
  97. echo "width:".   $image _size[0];
  98. echo "Length:".   $image _size[1];
  99. echo "<br> size:".   $file ["size"]."bytes";
  100. if ($watermark ==1)
  101. {
  102. $iinfo =getimagesize ($destination,$iinfo);
  103. $nimage =imagecreatetruecolor ($image _size[0],$image _size[1]);
  104. $white =imagecolorallocate ($nimage, 255,255,255);
  105. $black =imagecolorallocate ($nimage, 0,0,0);
  106. $red =imagecolorallocate ($nimage, 255,0,0);
  107. Imagefill ($nimage, 0,0,$white);
  108. Switch ($iinfo [2])
  109. {
  110. Case 1:
  111. $simage =imagecreatefromgif ($destination);
  112. Break
  113. Case 2:
  114. $simage =imagecreatefromjpeg ($destination);
  115. Break
  116. Case 3:
  117. $simage =imagecreatefrompng ($destination);
  118. Break
  119. Case 6:
  120. $simage =imagecreatefromwbmp ($destination);
  121. Break
  122. Default
  123. Die ("Unsupported file type");
  124. Exit
  125. }
  126. Imagecopy ($nimage,$simage, 0,0,0,0,$image _size[0],$image _size[1]);
  127. Imagefilledrectangle ($nimage, 1,$image _size[1]-15,80,$image _size[1],$white);
  128. Switch ($watertype)
  129. {
  130. Case 1: //Add watermark string
  131. Imagestring ($nimage, 2,3,$image _size[1]-15,$waterstring,$black);
  132. Break
  133. Case 2: //Add watermark Picture
  134. $simage 1 =imagecreatefromgif ("xplore.gif");
  135. Imagecopy ($nimage,$simage 1,0,0,0,0,85,15);
  136. Imagedestroy ($simage 1);
  137. Break
  138. }
  139. Switch ($iinfo [2])
  140. {
  141. Case 1:
  142. Imagegif ($nimage, $destination);
  143. Imagejpeg ($nimage, $destination);
  144. Break
  145. Case 2:
  146. Imagejpeg ($nimage, $destination);
  147. Break
  148. Case 3:
  149. Imagepng ($nimage, $destination);
  150. Break
  151. Case 6:
  152. Imagewbmp ($nimage, $destination);
  153. Imagejpeg ($nimage, $destination);
  154. Break
  155. }
  156. Overwrite the original upload file
  157. Imagedestroy ($nimage);
  158. Imagedestroy ($simage);
  159. }
  160. if ($imgpreview ==1)
  161. {
  162. echo "<br> picture preview:<br>";
  163. echo " $destination. " \" Width= ". ($image _size[0]*$imgpreviewsize). "height=".  ($image _size[1]*$imgpreviewsize);
  164. echo "alt=\" Picture preview: \ r file name: ". $destination.  "\ r upload time: \" > ";
  165. }
  166. }
  167. ?>
  168. </body>

PHP image upload Program (full version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.