Adding watermarks to images is something that many websites will do. The following code only supports adding text watermarks to images. Currently, only English is supported, if you want this program to support Chinese characters, set $ font = \ 'verdana. ttf \ '; it is OK to call a Chinese font.
Php Tutorial: Add text watermark code to images
/*
Adding watermarks to images is something that many websites will do. The following code only supports adding text watermarks to images. Currently, only English is supported, if you want this program to support Chinese characters, set $ font = 'verdana. ttf'; it is OK to call a Chinese font.
*/
$ Image = $ _ get [id];
$ Im = getimagesize ($ image );
// Print_r ($ im );
// The following is the width and height of the read text
$ Font = 'verdana. ttf'; // font
$ Font_size = $ im [0]/20; // text size
$ Size = imagettfbbox ($ font_size, 0, $ font, 'bkjia. c0m'); // text width and height
$ Textwidth = $ size [4]; // retrieve the width
$ Textheight = abs ($ size [7]); // retrieve height
$ Pic_w = abs ($ im [0]-$ textwidth)/2); // location of the text storage width
$ Pic_h = abs ($ im [1]-$ textheight)/2); // high text storage location
Switch ($ im [2])
{
Case 1:
$ Im = imagecreatefromgif ($ image );
Break;
Case 2:
$ Im = imagecreatefromjpeg ($ image );
Break;
Case 3:
$ Im = imagecreatefrompng ($ image );
Break;
}
// $ Logo = "2.png"; // Trademark Image
// $ Ing = getimagesize ($ logo );
//
// Switch ($ ing [2])
//
//{
// Case 1:
// $ In = imagecreatefromgif ($ logo );
// Break;
//
// Case 2:
// $ In = imagecreatefromjpeg ($ logo );
// Break;
//
// Case 3:
// $ In = imagecreatefrompng ($ logo );
// Break;
//}
// Imagecopy ($ im, $ in, 120, 3, 110, '000000', '000000'); // merge Images
$ Te = imagecolorclosestalpha ($ im, 255,255,255, 60); // The last value is the greater the transparency, the more transparent.
$ Str = iconv ("gbk", "UTF-8", "bKjia. c0m"); // set the text content and encoding. It's easy.
Imagettftext ($ im, $ font_size, 0, $ pic_w, $ pic_h, $ te, 'verdana. ttf', $ str );
Header ("content-type: image/jpeg ");
Imagejpeg ($ im );
?>