jpge 与png图片的融合

WBOY
Libérer: 2016-07-25 09:08:56
original
978 Les gens l'ont consulté

jpge图片与png图片的融合,其实就是大家熟悉的水印技术。下面代码中最重要的一句为:

//设定图像的混色模式
imagealphablending($ground_im, true);

  1. /*
  2. * 功能:PHP图片水印 (水印支持图片或文字)
  3. * 参数:
  4. * $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  5. * $waterPos 水印位置,有10种状态,0为随机位置;
  6. * 1为顶端居左,2为顶端居中,3为顶端居右;
  7. * 4为中部居左,5为中部居中,6为中部居右;
  8. * 7为底端居左,8为底端居中,9为底端居右;
  9. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  10. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  11. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  12. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  13. *
  14. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
  15. * $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
  16. * 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
  17. * 加水印后的图片的文件名和 $groundImage 一样。
  18. * 作者: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 = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
  25. //读取水印文件
  26. if(!empty($waterImage) && file_exists($waterImage))
  27. {
  28. $isWaterImage = TRUE;
  29. $water_info = getimagesize($waterImage);
  30. $water_w = $water_info[0];//取得水印图片的宽
  31. $water_h = $water_info[1];//取得水印图片的高
  32. switch($water_info[2])//取得水印图片的格式
  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. //读?尘巴计?
  41. if(!empty($groundImage) && file_exists($groundImage))
  42. {
  43. $ground_info = getimagesize($groundImage);
  44. $ground_w = $ground_info[0];//取得背景图片的宽
  45. $ground_h = $ground_info[1];//取得背景图片的高
  46. switch($ground_info[2])//取得背景图片的格式
  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("需要加水印的图片不存在!");
  57. }
  58. //水印位置
  59. if($isWaterImage)//图片水印
  60. {
  61. $w = $water_w;
  62. $h = $water_h;
  63. $label = "图片的";
  64. }
  65. else//文字水印
  66. {
  67. $temp = imagettfbbox(ceil($textFont*5),0,"./cour.ttf",$waterText);//取得使用 TrueType 字体的文本的范围
  68. $w = $temp[2] - $temp[6];
  69. $h = $temp[3] - $temp[7];
  70. unset($temp);
  71. $label = "文字区域";
  72. }
  73. if( ($ground_w {
  74. echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
  75. return;
  76. }
  77. switch($waterPos)
  78. {
  79. case 0://随机
  80. $posX = rand(0,($ground_w - $w));
  81. $posY = rand(0,($ground_h - $h));
  82. break;
  83. case 1://1为顶端居左
  84. $posX = 0;
  85. $posY = 0;
  86. break;
  87. case 2://2为顶端居中
  88. $posX = ($ground_w - $w) / 2;
  89. $posY = 0;
  90. break;
  91. case 3://3为顶端居右
  92. $posX = $ground_w - $w;
  93. $posY = 0;
  94. break;
  95. case 4://4为中部居左
  96. $posX = 0;
  97. $posY = ($ground_h - $h) / 2;
  98. break;
  99. case 5://5为中部居中
  100. $posX = ($ground_w - $w) / 2;
  101. $posY = ($ground_h - $h) / 2;
  102. break;
  103. case 6://6为中部居右
  104. $posX = $ground_w - $w;
  105. $posY = ($ground_h - $h) / 2;
  106. break;
  107. case 7://7为底端居左
  108. $posX = 0;
  109. $posY = $ground_h - $h;
  110. break;
  111. case 8://8为底端居中
  112. $posX = ($ground_w - $w) / 2;
  113. $posY = $ground_h - $h;
  114. break;
  115. case 9://9为底端居右
  116. $posX = $ground_w - $w;
  117. $posY = $ground_h - $h;
  118. break;
  119. default://随机
  120. $posX = rand(0,($ground_w - $w));
  121. $posY = rand(0,($ground_h - $h));
  122. break;
  123. }
  124. //设定图像的混色模式
  125. imagealphablending($ground_im, true);
  126. if($isWaterImage)//图片水印
  127. {
  128. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  129. }
  130. else//文字水印
  131. {
  132. if( !empty($textColor) && (strlen($textColor)==7) )
  133. {
  134. $R = hexdec(substr($textColor,1,2));
  135. $G = hexdec(substr($textColor,3,2));
  136. $B = hexdec(substr($textColor,5));
  137. }
  138. else
  139. {
  140. die("水印文字颜色格式不正确!");
  141. }
  142. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  143. }
  144. //生成水印后的图片
  145. @unlink($groundImage);
  146. switch($ground_info[2])//取得背景图片的格式
  147. {
  148. case 1:imagegif($ground_im,$groundImage);break;
  149. case 2:imagejpeg($ground_im,$groundImage);break;
  150. case 3:imagepng($ground_im,$groundImage);break;
  151. default:die($errorMsg);
  152. }
  153. //释放内存
  154. if(isset($water_info)) unset($water_info);
  155. if(isset($water_im)) imagedestroy($water_im);
  156. unset($ground_info);
  157. imagedestroy($ground_im);
  158. }
  159. $uploadfile="your_picture.jpg"; //待处理的背景图片位置
  160. $waterImage="Hat.png"; //透明图片位置
  161. imageWaterMark($uploadfile,9,$waterImage);
  162. //在web上显示出来
  163. echo "jpge 与png图片的融合 ";
  164. ?>
复制代码


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!