Home  >  Article  >  Backend Development  >  Generate artistic font image watermark code_PHP tutorial

Generate artistic font image watermark code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:45:341568browse

//adv0.jpg is the background image, please 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"; 
$font_file = "/www/font/hyi_xkj.ttf"; //The linux absolute path of the font

//26: font, 0 is the angle, 10, 36 are the coordinates, $font_color is the text color, font is the font, and the text is the filled in text
imagettftext($im, 26,0, 10, 36, $font_color ,$font_file, $text); Insert text into the picture

// output image
header ('content-type: image/png');                                                // Even pictures copied from jpg can be output as png,                                   imagepng ($im);
// clean up
imagedestroy($im);

Method 2 of generating watermark


public final class imageutils {
public imageutils() {


}

public final static string getpressimgpath(){

return applicationcontext.getrealpath("/template/data/util/shuiyin.gif");

}

/**

* Print pictures onto pictures

* @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, width, 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);
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
encoder.encode(image);
out.close();
} catch (exception e) {
e.printstacktrace();
}
}

/**

* Print text watermark image

* @param presstext --Text
* @param targetimg -- target image
* @param fontname -- font 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, width, height, null);
// string s=www.bKjia.c0m;
g.setcolor(color.red);
g.setfont(new font(fontname, fontstyle, fontsize));

g.drawstring(presstext, width - 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);

}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633027.htmlTechArticle//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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn