PHP image plus watermark and upload image plus watermark class

Source: Internet
Author: User
Tags imagecopy imagejpeg
  1. Upload file Type list
  2. $uptypes =array (
  3. ' Image/jpg ',
  4. ' Image/jpeg ',
  5. ' Image/png ',
  6. ' Image/pjpeg ',
  7. ' Image/gif ',
  8. ' Image/bmp ',
  9. ' Image/x-png '
  10. );
  11. $max _file_size=2000000; Upload file size limit, unit byte
  12. $destination _folder= "uploadimg/"; Upload file path
  13. $watermark = 1; Whether additional watermark (1 is watermark, the other is not watermark);
  14. $watertype = 1; Watermark Type (1 for text, 2 for picture)
  15. $waterposition = 1; Watermark position (1 is lower left corner, 2 is lower right corner
  16. , 3 is the upper left corner, 4 is the upper right corner, 5 is centered);
  17. $waterstring = "
  18. http://www.xplore.cn/"; Watermark String
  19. $waterimg = "Xplore.gif"; Watermark Picture
  20. $imgpreview = 1; Whether to generate a preview map (1 is generated, others are not generated);
  21. $imgpreviewsize =1/2; Thumbnail scale
  22. ?>
  23. Zwell Picture Uploading Program
  24. if ($_server[' request_method '] = = ' POST ')
  25. {
  26. if (!is_uploaded_file ($_files["Upfile")
  27. [Tmp_name]))
  28. Whether the file exists
  29. {
  30. echo "Picture does not exist!";
  31. Exit
  32. }
  33. $file = $_files["Upfile"];
  34. if ($max _file_size < $file ["size"])
  35. Check File size
  36. {
  37. echo "File too big!";
  38. Exit
  39. }
  40. if (!in_array ($file ["type"], $uptypes))
  41. Check file types
  42. {
  43. echo "file type does not match!". $file ["type"];
  44. Exit
  45. }
  46. if (!file_exists ($destination _folder))
  47. {
  48. mkdir ($destination _folder);
  49. }
  50. $filename = $file ["Tmp_name"];
  51. $image _size = getimagesize ($filename);
  52. $pinfo =pathinfo ($file ["name"]);
  53. $ftype = $pinfo [' extension '];
  54. $destination = $destination _folder.
  55. Time (). ".". $ftype;
  56. if (file_exists ($destination) &&
  57. $overwrite! = True)
  58. {
  59. echo "file with the same name already exists";
  60. Exit
  61. }
  62. if (!move_uploaded_file ($filename,
  63. $destination))
  64. {
  65. echo "Error moving File";
  66. Exit
  67. }
  68. $pinfo =pathinfo ($destination);
  69. $fname = $pinfo [basename];
  70. echo "has been successfully uploaded

  71. Filename:
  72. ". $destination _folder.
  73. $fname. "
    ";
  74. echo "width:". $image _size[0];
  75. echo "Length:". $image _size[1];
  76. echo "
    Size: ". $file [" Size "]." bytes ";
  77. if ($watermark ==1)
  78. {
  79. $iinfo =getimagesize ($destination, $iinfo);
  80. $nimage =imagecreatetruecolor ($image _size[0]
  81. , $image _size[1]);
  82. $white =imagecolorallocate ($nimage, 255,255,255);
  83. $black =imagecolorallocate ($nimage, 0,0,0);
  84. $red =imagecolorallocate ($nimage, 255,0,0);
  85. Imagefill ($nimage, 0,0, $white);
  86. Switch ($iinfo [2])
  87. {
  88. Case 1:
  89. $simage =imagecreatefromgif ($destination);
  90. Break
  91. Case 2:
  92. $simage =imagecreatefromjpeg ($destination);
  93. Break
  94. Case 3:
  95. $simage =imagecreatefrompng ($destination);
  96. Break
  97. Case 6:
  98. $simage =imagecreatefromwbmp ($destination);
  99. Break
  100. Default
  101. Die ("Unsupported file type");
  102. Exit
  103. }
  104. Imagecopy ($nimage, $simage, 0,0,0,0,
  105. $image _size[0], $image _size[1]);
  106. Imagefilledrectangle ($nimage, 1,
  107. $image _size[1]-15,80, $image _size[1], $white);
  108. Switch ($watertype)
  109. {
  110. Case 1://Add watermark String
  111. Imagestring ($nimage, 2,3, $image _size[1]-15,
  112. $waterstring, $black);
  113. Break
  114. Case 2://Add watermark Picture
  115. $simage 1 =imagecreatefromgif ("Xplore.gif");
  116. Imagecopy ($nimage, $simage 1,0,0,0,0,85,15);
  117. Imagedestroy ($simage 1);
  118. Break
  119. }
  120. Switch ($iinfo [2])
  121. {
  122. Case 1:
  123. Imagegif ($nimage, $destination);
  124. Imagejpeg ($nimage, $destination);
  125. Break
  126. Case 2:
  127. Imagejpeg ($nimage, $destination);
  128. Break
  129. Case 3:
  130. Imagepng ($nimage, $destination);
  131. Break
  132. Case 6:
  133. Imagewbmp ($nimage, $destination);
  134. Imagejpeg ($nimage, $destination);
  135. Break
  136. }
  137. Overwrite the original upload file
  138. Imagedestroy ($nimage);
  139. Imagedestroy ($simage);
  140. }
  141. if ($imgpreview ==1)
  142. {
  143. echo "
    Picture preview:
    ";
  144. echo "
  145. Height= ". ($image _size[1]* $imgpreviewsize); "
  146. echo "alt=\" Picture preview: \ r file name: ".
  147. $destination. " \ r upload time: \ "/>";
  148. }
  149. }
  150. ?>
Copy Code

  • 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.