首页 > 语言 > PHP > 正文

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表