Home > Backend Development > PHP Tutorial > Fusion of jpge and png pictures

Fusion of jpge and png pictures

WBOY
Release: 2016-07-25 09:08:56
Original
1056 people have browsed it

The fusion of jpge images and png images is actually the watermark technology that everyone is familiar with. The most important sentence in the code below is:

//Set the color blending mode of the image
imagealphablending($ground_im, true);

  1. /*
  2. * Function: PHP image watermark (watermark supports images or text)
  3. * Parameters:
  4. * $groundImage background image, that is, the image that needs to be watermarked, currently only supports GIF, JPG, PNG format;
  5. * $waterPos watermark position, there are 10 states, 0 is a random position;
  6. * 1 is the top left, 2 is the top center, 3 is the top right;
  7. * 4 is the middle left, 5 is the middle center, 6 means the middle is on the right;
  8. * 7 is on the bottom on the left, 8 is on the bottom in the center, 9 is on the bottom on the right;
  9. * $waterImage image watermark, that is, the image used as the watermark, currently only supports GIF, JPG, and PNG formats;
  10. * $waterText text watermark, that is, text is used as a watermark, supports ASCII code, does not support Chinese;
  11. * $textFont text size, the value is 1, 2, 3, 4 or 5, the default is 5;
  12. * $textColor text Color, the value is a hexadecimal color value, the default is #FF0000 (red);
  13. *
  14. * Note: Support GD 2.0, Support FreeType, GIF Read, GIF Create, JPG, PNG
  15. * $waterImage and $waterText are best Don't use them at the same time, just choose one of them, and use $waterImage first.
  16. * When $waterImage is valid, the parameters $waterString, $stringFont, and $stringColor are not valid.
  17. * The file name of the watermarked image is the same as $groundImage.
  18. * Author: longware @ 2004-11-3 14:15:13
  19. */
  20. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$ textColor="#FF0000")
  21. //function imageWaterMark($groundImage,$waterPos=0,$waterImage,$waterText=,$textFont=5,$textColor=#FF0000″)
  22. {
  23. $isWaterImage = FALSE;
  24. $ formatMsg = "This file format is not supported yet. Please use image processing software to convert the image to GIF, JPG, or PNG format.";
  25. //Read watermark file
  26. if(!empty($waterImage) && file_exists($waterImage))
  27. {
  28. $isWaterImage = TRUE;
  29. $water_info = getimagesize($waterImage);
  30. $water_w = $water_info[ 0];//Get the width of the watermark image
  31. $water_h = $water_info[1];//Get the height of the watermark image
  32. switch($water_info[2])//Get the format of the watermark image
  33. {
  34. case 1:$ water_im = imagecreatefromgif($waterImage);break;
  35. case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  36. case 3:$water_im = imagecreatefrompng($waterImage);break;
  37. default:die($formatMsg);
  38. }
  39. }
  40. //Read? Chenbaji?
  41. if(!empty($groundImage) && file_exists($groundImage))
  42. {
  43. $ground_info = getimagesize($groundImage);
  44. $ground_w = $ground_info[0]; //Get the width of the background image
  45. $ground_h = $ground_info[1];//Get the height of the background image
  46. switch($ground_info[2])//Get the format of the background image
  47. {
  48. case 1:$ground_im = imagecreatefromgif ($groundImage);break;
  49. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  50. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  51. default:die($formatMsg);
  52. }
  53. }
  54. else
  55. {
  56. die("The picture that needs to be watermarked does not exist!");
  57. }
  58. //Watermark position
  59. if($isWaterImage)//Picture watermark
  60. {
  61. $w = $water_w;
  62. $h = $ water_h;
  63. $label = "Picture";
  64. }
  65. else//Text watermark
  66. {
  67. $temp = imagettfbbox(ceil($textFont*5),0,"./cour.ttf",$waterText);/ /Get the range of text using TrueType font
  68. $w = $temp[2] - $temp[6];
  69. $h = $temp[3] - $temp[7];
  70. unset($temp);
  71. $ label = "text area";
  72. }
  73. if( ($ground_w<$w) || ($ground_h<$h) )
  74. {
  75. echo "The length or width of the image that needs to be watermarked is greater than the watermark".$label. "It's too young to generate a watermark! ";
  76. return;
  77. }
  78. switch($waterPos)
  79. {
  80. case 0://random
  81. $posX = rand(0,($ground_w - $w));
  82. $posY = rand(0,($ground_h - $h));
  83. break;
  84. case 1://1 is top left
  85. $posX = 0;
  86. $posY = 0;
  87. break;
  88. case 2://2 is top center
  89. $posX = ($ ground_w - $w) / 2;
  90. $posY = 0;
  91. break;
  92. case 3://3 is top right
  93. $posX = $ground_w - $w;
  94. $posY = 0;
  95. break;
  96. case 4 ://4 means center left
  97. $posX = 0;
  98. $posY = ($ground_h - $h) / 2;
  99. break;
  100. case 5://5 means center center
  101. $posX = ($ground_w - $w ) / 2;
  102. $posY = ($ground_h - $h) / 2;
  103. break;
  104. case 6://6 is the center right
  105. $posX = $ground_w - $w;
  106. $posY = ($ground_h - $h) / 2;
  107. break;
  108. case 7://7 means the bottom is on the left
  109. $posX = 0;
  110. $posY = $ground_h - $h;
  111. break;
  112. case 8://8 means the bottom is in the center
  113. $posX = ($ground_w - $w) / 2;
  114. $posY = $ground_h - $h;
  115. break;
  116. case 9://9 is bottom right
  117. $posX = $ground_w - $w;
  118. $posY = $ground_h - $h;
  119. break;
  120. default://random
  121. $posX = rand(0,($ground_w - $w));
  122. $posY = rand(0,($ground_h - $h) );
  123. break;
  124. }
  125. //Set the color mixing mode of the image
  126. imagealphablending($ground_im, true);
  127. if($isWaterImage)//Image watermark
  128. {
  129. imagecopy($ground_im, $water_im, $posX, $ posY, 0, 0, $water_w,$water_h);//Copy the watermark to the target file
  130. }
  131. else//Text watermark
  132. {
  133. if( !empty($textColor) && (strlen($textColor)==7) )
  134. {
  135. $R = hexdec(substr($textColor,1,2));
  136. $G = hexdec(substr($textColor,3,2));
  137. $B = hexdec(substr($textColor,5) );
  138. }
  139. else
  140. {
  141. die("The watermark text color format is incorrect! ");
  142. }
  143. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  144. }
  145. //The image after generating the watermark
  146. @unlink($groundImage);
  147. switch($ground_info[2])//Get the format of the background image
  148. {
  149. case 1:imagegif($ground_im,$groundImage);break;
  150. case 2:imagejpeg($ground_im,$ groundImage);break;
  151. case 3:imagepng($ground_im,$groundImage);break;
  152. default:die($errorMsg);
  153. }
  154. //Release memory
  155. if(isset($water_info)) unset($water_info );
  156. if(isset($water_im)) imagedestroy($water_im);
  157. unset($ground_info);
  158. imagedestroy($ground_im);
  159. }
  160. $uploadfile="your_picture.jpg"; //Background image to be processed Position
  161. $waterImage="Hat.png"; //Transparent image position
  162. imageWaterMark($uploadfile,9,$waterImage);
  163. //Displayed on the web
  164. echo "";
  165. ?>
Copy code


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