php生成艺术字体图片水印代码

  1. //adv0.jpg就是背景图片,注意函数与图片格式对应
  2. $im = imagecreatefromjpeg('/www/law/images/demo/adv0.jpg');
  3. $font_color = imagecolorallocate ($im, 0, 250, 10); //这是文字颜色,绿色
  4. $text = "张三的博客"; //文字内容
  5. $font_file = "/www/font/hyi_xkj.ttf"; //字体的linux绝对路径
  6. //26:字体, 0 是角度, 10,36是坐标, $font_color是文字色, font是字体, 文本是填入的文字
  7. imagettftext($im, 26,0, 10, 36, $font_color ,$font_file, $text); 往图片插入文字
  8. // output image
  9. header ('content-type: image/png'); //即便是从jpg拷贝的图片,也能以png输出,
  10. imagepng ($im);
  11. // clean up
  12. imagedestroy($im);
  13. //生成水印方法二,代码如下:
  14. public final class imageutils {
  15. public imageutils() {
  16. }
  17. public final static string getpressimgpath(){
  18. return applicationcontext.getrealpath("/template/data/util/shuiyin.gif");
  19. }
  20. /**
  21. * 把图片印刷到图片上
  22. * @param pressimg -- 水印文件
  23. * @param targetimg -- 目标文件
  24. * @param x
  25. * @param y
  26. */
  27. public final static void pressimage(string pressimg, string targetimg, int x, int y) {
  28. try {
  29. file _file = new file(targetimg);
  30. image src = imageio.read(_file);
  31. int wideth = src.getwidth(null);
  32. int height = src.getheight(null);
  33. bufferedimage image = new bufferedimage(wideth, height,
  34. bufferedimage.type_int_rgb);
  35. graphics g = image.creategraphics();
  36. g.drawimage(src, 0, 0, wideth, height, null);
  37. // 水印文件
  38. file _filebiao = new file(pressimg);
  39. image src_biao = imageio.read(_filebiao);
  40. int wideth_biao = src_biao.getwidth(null);
  41. int height_biao = src_biao.getheight(null);
  42. g.drawimage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
  43. height_biao, null);
  44. // /
  45. g.dispose();
  46. fileoutputstream out = new fileoutputstream(targetimg);
  47. jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
  48. encoder.encode(image);
  49. out.close();
  50. } catch (exception e) {
  51. e.printstacktrace();
  52. }
  53. }
  54. /**
  55. * 打印文字水印图片
  56. * @param presstext --文字
  57. * @param targetimg -- 目标图片
  58. * @param fontname -- 字体名
  59. * @param fontstyle -- 字体样式
  60. * @param color -- 字体颜色
  61. * @param fontsize -- 字体大小
  62. * @param x -- 偏移量
  63. * @param y
  64. */
  65. public static void presstext(string presstext, string targetimg, string fontname,int fontstyle, int color, int fontsize, int x, int y) {
  66. try {
  67. file _file = new file(targetimg);
  68. image src = imageio.read(_file);
  69. int wideth = src.getwidth(null);
  70. int height = src.getheight(null);
  71. bufferedimage image = new bufferedimage(wideth, height,
  72. bufferedimage.type_int_rgb);
  73. graphics g = image.creategraphics();
  74. g.drawimage(src, 0, 0, wideth, height, null);
  75. // string s=www.111cn.net;
  76. g.setcolor(color.red);
  77. g.setfont(new font(fontname, fontstyle, fontsize));
  78. g.drawstring(presstext, wideth - fontsize - x, height - fontsize/2 - y);
  79. g.dispose();
  80. fileoutputstream out = new fileoutputstream(targetimg);
  81. jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
  82. encoder.encode(image);
  83. out.close();
  84. } catch (exception e) {
  85. system.out.println(e);
  86. }
  87. }
  88. //开源代码phpfensi.com
  89. public static void main(string[] args) {
  90. pressimage("c:/shuiyin/shuiyin.gif", "c:/shuiyin/dsc02342.jpg", 20 ,20);
  91. }
  92. }