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

php文字水印与php图片水印代码实例

程序员文章站 2022-04-15 14:06:21
...
  1. $dst_path = 'dst.jpg';
  2. //创建图片的实例
  3. $dst = imagecreatefromstring(file_get_contents($dst_path));
  4. //打上文字
  5. $font = './simsun.ttc';//字体
  6. $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
  7. imagefttext($dst, 13, 0, 20, 20, $black, $font, '快乐编程');
  8. //输出图片
  9. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
  10. switch ($dst_type) {
  11. case 1://GIF
  12. header('Content-Type: image/gif');
  13. imagegif($dst);
  14. break;
  15. case 2://JPG
  16. header('Content-Type: image/jpeg');
  17. imagejpeg($dst);
  18. break;
  19. case 3://PNG
  20. header('Content-Type: image/png');
  21. imagepng($dst);
  22. break;
  23. default:
  24. break;
  25. }
  26. imagedestroy($dst);
复制代码

例2,php图片水印 图片水印,将一张图片加在另外一张图片上,主要使用gd库的imagecopy和imagecopymerge。

效果图: php文字水印与php图片水印代码实例

代码:

  1. $dst_path = 'dst.jpg';
  2. $src_path = 'src.jpg';
  3. //创建图片的实例
  4. $dst = imagecreatefromstring(file_get_contents($dst_path));
  5. $src = imagecreatefromstring(file_get_contents($src_path));
  6. //获取水印图片的宽高
  7. list($src_w, $src_h) = getimagesize($src_path);
  8. //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
  9. imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 50);
  10. //如果水印图片本身带透明色,则使用imagecopy方法
  11. //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
  12. //输出图片
  13. list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
  14. switch ($dst_type) {
  15. case 1://GIF
  16. header('Content-Type: image/gif');
  17. imagegif($dst);
  18. break;
  19. case 2://JPG
  20. header('Content-Type: image/jpeg');
  21. imagejpeg($dst);
  22. break;
  23. case 3://PNG
  24. header('Content-Type: image/png');
  25. imagepng($dst);
  26. break;
  27. default:
  28. break;
  29. }
  30. imagedestroy($dst);
  31. imagedestroy($src);
复制代码