Home >Backend Development >PHP Tutorial >PHP image watermarking and uploaded image watermarking

PHP image watermarking and uploaded image watermarking

WBOY
WBOYOriginal
2016-07-25 09:07:401223browse
  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 to attach a watermark (1 is watermarked, others are not watermarked);
  14. $watertype=1; //Watermark type (1 is text, 2 is picture)
  15. $waterposition =1; //Watermark position (1 is the lower left corner, 2 is the lower right corner
  16. , 3 is the upper left corner, 4 is the upper right corner, 5 is in the center);
  17. $waterstring="
  18. http://www.xplore.cn/"; //Watermark string
  19. $waterimg="xplore.gif"; //Watermark image
  20. $imgpreview=1 ; //Whether to generate a preview image (1 means generate, others do not generate);
  21. $imgpreviewsize=1/2; //Thumbnail ratio
  22. ?>
  23. ZwelL image uploader
  24. method ="post" name="upform">
  25. Upload file:
  26. The file types allowed to be uploaded are:
  27. if ($_SERVER['REQUEST_METHOD'] == 'POST ')
  28. {
  29. if (!is_uploaded_file($_FILES["upfile"]
  30. [tmp_name]))
  31. //Whether the file exists
  32. {
  33. echo "The picture does not exist!";
  34. exit;
  35. }
  36. $file = $ _FILES["upfile"];
  37. if($max_file_size <$file["size"])
  38. //Check the file size
  39. {
  40. echo "The file is too big!";
  41. exit;
  42. }
  43. if(!in_array( $file["type"], $uptypes))
  44. //Check file type
  45. {
  46. echo "File type does not match!".$file["type"];
  47. exit;
  48. }
  49. if(!file_exists($destination_folder ))
  50. {
  51. mkdir($destination_folder);
  52. }
  53. $filename=$file["tmp_name"];
  54. $image_size = getimagesize($filename);
  55. $pinfo=pathinfo($file["name"]);
  56. $ftype=$pinfo['extension'];
  57. $destination = $destination_folder.
  58. time().".".$ftype;
  59. if (file_exists($destination) &&
  60. $overwrite != true)
  61. {
  62. echo "The file with the same name already exists";
  63. exit;
  64. }
  65. if(!move_uploaded_file ($filename,
  66. $destination))
  67. {
  68. echo "Error moving file";
  69. exit;
  70. }
  71. $pinfo=pathinfo($ destination);
  72. $fname=$pinfo[basename];
  73. echo " has been successfully uploaded

  74. File name:
  75. ".$ destination_folder.
  76. $fname."
    ";
  77. echo " width:".$image_size[0];
  78. echo " length:".$image_size[1];
  79. echo "
  80. if($watermark==1)
  81. {
  82. $iinfo=getimagesize($destination,$iinfo);
  83. $nimage=imagecreatetruecolor($image_size[ 0]
  84. ,$image_size[1]);
  85. $white=imagecolorallocate($nimage,255,255,255);
  86. $black=imagecolorallocate($nimage,0,0,0);
  87. $red=imagecolorallocate($nimage,255, 0,0);
  88. imagefill($nimage,0,0,$white);
  89. switch ($iinfo[2])
  90. {
  91. case 1:
  92. $simage =imagecreatefromgif($destination);
  93. break;
  94. case 2 :
  95. $simage =imagecreatefromjpeg($destination);
  96. break;
  97. case 3:
  98. $simage =imagecreatefrompng($destination);
  99. break;
  100. case 6:
  101. $simage =imagecreatefromwbmp($destination);
  102. break;
  103. default :
  104. die("Unsupported file type");
  105. exit;
  106. }
  107. imagecopy($nimage,$simage,0,0,0,0,
  108. $image_size[0],$image_size[1]);
  109. imagefilledrectangle($nimage,1,
  110. $image_size[1]-15,80,$image_size[1],$white);
  111. switch($watertype)
  112. {
  113. case 1: //Add watermark string
  114. imagestring($ nimage,2,3,$image_size[1]-15,
  115. $waterstring,$black);
  116. break;
  117. case 2: //Add watermark image
  118. $simage1 =imagecreatefromgif("xplore.gif");
  119. imagecopy( $nimage,$simage1,0,0,0,0,85,15);
  120. imagedestroy($simage1);
  121. break;
  122. }
  123. switch ($iinfo[2])
  124. {
  125. case 1:
  126. //imagegif ($nimage, $destination);
  127. imagejpeg($nimage, $destination);
  128. break;
  129. case 2:
  130. imagejpeg($nimage, $destination);
  131. break;
  132. case 3:
  133. imagepng($nimage, $destination );
  134. break;
  135. case 6:
  136. imagewbmp($nimage, $destination);
  137. //imagejpeg($nimage, $destination);
  138. break;
  139. }
  140. //Overwrite the original uploaded file
  141. imagedestroy($nimage);
  142. imagedestroy($simage);
  143. }
  144. if($imgpreview==1)
  145. {
  146. echo "
    Image preview:
    ";
  147. echo "height=".($image_size[1]*$imgpreviewsize);"
  148. echo " alt="Image preview:rFile name:".
  149. $destination."rUpload time:" />" ;
  150. }
  151. }
  152. ?>
Copy code



Statement:
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