欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php水印代码,php半透明水印支持png透明背景

程序员文章站 2022-05-27 10:37:16
...
  1. //原始图像

  2. $dst = "/upload/20140914/20120914040740-0.jpg"; //图片路径
  3. //原始图片信息

  4. $dst_info = getimagesize($dst);
  5. switch ($dst_info[2])
  6. {
  7. case 1:
  8. $dst_im =imagecreatefromgif($dst);break;
  9. case 2:
  10. $dst_im =imagecreatefromjpeg($dst);break;
  11. case 3:
  12. $dst_im =imagecreatefrompng($dst);break;
  13. case 6:
  14. $dst_im =imagecreatefromwbmp($dst);break;
  15. default:
  16. die("不支持的文件类型1");exit;
  17. }
  18. //水印图像
  19. $src = "/images/shuiyin.png"; //注意路径要写对
  20. $src_info = getimagesize($src);
  21. switch ($src_info[2])
  22. {
  23. case 1:
  24. $src_im =imagecreatefromgif($src);break;
  25. case 2:
  26. $src_im =imagecreatefromjpeg($src);break;
  27. case 3:
  28. $src_im =imagecreatefrompng($src);break;
  29. case 6:
  30. $src_im =imagecreatefromwbmp($src);break;
  31. default:
  32. die("不支持的文件类型1");exit;
  33. }
  34. //半透明格式水印
  35. //$alpha = 50; //水印透明度
  36. //imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0]-10,$dst_info[1]-$src_info[1]-10,0,0,$src_info[0],$src_info[1],$alpha);
  37. //支持png本身透明度的方式
  38. imagecopy($dst_im,$src_im,$dst_info[0]-$src_info[0]-10,$dst_info[1]-$src_info[1]-10,0,0,$src_info[0],$src_info[1]);
  39. //保存图片

  40. switch ($dst_info[2]){
  41. case 1:
  42. imagegif($dst_im,$dst);break;
  43. case 2:
  44. imagejpeg($dst_im,$dst);break;
  45. case 3:
  46. imagepng($dst_im,$dst);break;
  47. case 6:
  48. imagewbmp($dst_im,$dst);break;
  49. default:
  50. die("不支持的文件类型2");exit;
  51. }
  52. //销毁gd库对象

  53. imagedestroy($dst_im);
  54. imagedestroy($src_im);
复制代码