//adv0.jpg就是背景圖片,注意函數與圖片格式對應
$im = imagecreatefromjpeg('/www/law/images/demo/adv0.jpg');
$font_color = imagecolorallocate ($im, 0, 250, 10); //這是文字顏色,綠色
$text = "張三的博客"; //文字內容
$font_file = "/www/font/hyi_xkj.ttf"; //字體的linux絕對路徑
//26:字體, 0 是角度, 10,36是坐標, $font_color是文字色, font是字體, 文本是填入的文字
imagettftext($im, 26,0, 10, 36, $font_color ,$font_file, $text); 往圖片插入文字
// output image
header ('content-type: image/png'); //即便是從jpg拷貝的圖片,也能以png輸出,
imagepng ($im);
// clean up
imagedestroy($im);
生成水印方法二
public final class imageutils {
public imageutils() {}
public final static string getpressimgpath(){
return applicationcontext.getrealpath("/template/data/util/shuiyin.gif");
}/**
* 把圖片印刷到圖片上
* @param pressimg -- 水印文件
* @param targetimg -- 目標文件
* @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);// 水印文件
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);
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
encoder.encode(image);
out.close();
} catch (exception e) {
e.printstacktrace();
}
}/**
* 打印文字水印圖片
* @param presstext --文字
* @param targetimg -- 目標圖片
* @param fontname -- 字體名
* @param fontstyle -- 字體樣式
* @param color -- 字體顏色
* @param fontsize -- 字體大小
* @param x -- 偏移量
* @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);
jpegimageencoder encoder = jpegcodec.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);
}
}