Generate the watermark code for the art font image. Adv0.jpg?background image, and the picture format should be imimagecreatefrom=(wwwlawimagesdemoadv0.jpg); $ font_colorimagecolorallocate ($ im, 0,250, 10); this is
// Adv0.jpg is the background image. Note that the function corresponds to the image format.
$ Im = imagecreatefromjpeg ('/www/law/images/demo/adv0.jpg ');
$ Font_color = imagecolorallocate ($ im, 0,250, 10); // This is the text color, green
$ Text = "zhang san's blog"; // text content
$ Font_file = "/www/font/hyi_xkj.ttf"; // The absolute path of the linux font
// 26: font. 0 indicates the angle, 10, 36 indicates the coordinate, $ font_color indicates the text color, font indicates the font, and text indicates the entered text.
Imagettftext ($ im, 26, 0, 10, 36, $ font_color, $ font_file, $ text); insert text into the image
// Output image
Header ('content-type: image/png '); // output in png format even if an image is copied from jpg,
Imagepng ($ im );
// Clean up
Imagedestroy ($ im );
Method 2
Public final class imageutils {
Public imageutils (){
}
Public final static string getpressimgpath (){
Return applicationcontext. getrealpath ("/template/data/util/shuiyin.gif ");
}
/**
* Print the image to the image.
* @ Param pressimg -- watermark file
* @ Param targetimg -- target file
* @ Param x
* @ Param y
*/
Public final static void pressimage (string pressimg, string targetimg, int x, int y ){
Try {
File _ file = new file (targetimg );
Image src = imageio. read (_ file );
Int wideth = src. getwidth (null );
Int height = src. getheight (null );
Bufferedimage image = new bufferedimage (wideth, height,
Bufferedimage. type_int_rgb );
Graphics g = image. creategraphics ();
G. drawimage (src, 0, 0, wideth, height, null );
// Watermark file
File _ filebiao = new file (pressimg );
Image src_biao = imageio. read (_ filebiao );
Int wideth_biao = src_biao.getwidth (null );
Int height_biao = src_biao.getheight (null );
G. drawimage (src_biao, wideth-wideth_biao-x, height-height_biao-y, wideth_biao,
Height_biao, null );
///
G. dispose ();
Fileoutputstream out = new fileoutputstream (targetimg );
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Print a text watermark image
* @ Param presstext -- text
* @ Param targetimg -- target image
* @ Param fontname -- body name
* @ Param fontstyle -- font style
* @ Param color -- Font color
* @ Param fontsize -- Font size
* @ Param x -- offset
* @ Param y
*/
Public static void presstext (string presstext, string targetimg, string fontname, int fontstyle, int color, int fontsize, int x, int y ){
Try {
File _ file = new file (targetimg );
Image src = imageio. read (_ file );
Int wideth = src. getwidth (null );
Int height = src. getheight (null );
Bufferedimage image = new bufferedimage (wideth, height,
Bufferedimage. type_int_rgb );
Graphics g = image. creategraphics ();
G. drawimage (src, 0, 0, wideth, height, null );
// String s = www. bKjia. c0m;
G. setcolor (color. red );
G. setfont (new font (fontname, fontstyle, fontsize ));
G. drawstring (presstext, wideth-fontsize-x, height-fontsize/2-y );
G. dispose ();
Fileoutputstream out = new fileoutputstream (targetimg );
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. close ();
} Catch (exception e ){
System. out. println (e );
}
}
Public static void main (string [] args ){
Pressimage ("c:/shuiyin/shuiyin.gif", "c:/shuiyin/dsc02342.jpg", 20, 20 );
}
}
Watermark $ im = imagecreatefromjpeg ('/www/law/images/demo/adv0.jpg'); $ font_color = imagecolorallocate ($ im, 0,250, 10); // This is...