A watermark tool for images.
// Font Library
Define ('fontfile', 'c:/Windows/Fonts/MSYH. TTC ');
$ Text = '2014/1/31 ';
$ Fontfile = 'C:/Windows/Fonts/MSYH. TTC ';
$ NewImageName = NULL; // NULL is not saved and output to the browser. If the file is saved, it is changed to the file name to be saved.
// $ NewImageName = 'd:/myWorkspace/get_img/deal_img/jiaShuiYin/aaa.jpg ';
$ ImgData = array ('src' => 'd:/myWorkspace/get_img/deal_img/jiaShuiYin/sun.jpg ',
'Fontcolor' => array (0, 0 ),
'X' => 190,
'Y' => 352,
);
Water_text ($ text, $ fontfile, $ imgData, $ newImageName); // Add a watermark to call the Function
/*
Parameter: $ src -- image address, $ text -- text to be added; $ fontsize -- text size; $ jiaodu -- text angle; $ x -- text upper left corner x; $ y -- y in the upper left corner of the text;
$ Fontcolor = array (, 0) --- text color, array, length 3, red, green, and blue color allocation; $ fontfile -- Fonts file path, default Fonts/times. ttf
Water_text ($ src, $ text = '', $ text2 ='', $ fontsize = 20, $ jiaodu = 0, $ x = 0, $ y = 0, $ x2 = 0, $ y2 = 0, $ fontcolor = array (255, 0, 0), $ fontfile = 'fonts/times. ttf ')
*/
Function water_text ($ text, $ fontfile, $ imgData, $ newImageName ){
$ Fontsize = 15; // font size
$ Jiaodu = 0; // font Rotation Angle
$ X = $ imgData ['X']; // Add the x axis of the watermark.
$ Y = $ imgData ['y']; // Add the y axis of the watermark.
$ Src = $ imgData ['src']; // source image path
$ Fontcolor = $ imgData ['fontcolor']; // font color
// Array of image-related information
$ Arr = getimagesize ($ src );
If (! $ Arr) return 'image reading error ';
Switch ($ arr [2]) {
// Index 2 shows the image type and returns numbers, where 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD
Case 1: $ img = imagecreatefromgif ($ src); break;
Case 2: $ img = imagecreatefromjpeg ($ src); break;
Case 3: $ img = imagecreatefrompng ($ src); break;
// Default :;
}
If (! Is_array ($ fontcolor) | count ($ fontcolor) <3) return 0;
If (! File_exists ($ fontfile) $ fontfile = FONTFILE;
If (! File_exists ($ fontfile) return 'fontfile was not found ';
// Text width and height
$ Box = imagettfbbox ($ fontsize, $ jiaodu, $ fontfile, $ text );
$ K = max ($ box [2], $ box [4])-min ($ box [0], $ box [6]);
$ G = max ($ box [1], $ box [3])-min ($ box [5], $ box [7]);
// Set the lower left foot
$ X1 = $ x;
$ Y1 = $ y + $ g;
// Text color
$ Color = imagecolorallocate ($ img, $ fontcolor [0], $ fontcolor [1], $ fontcolor [2]);
// Write a string
Imagettftext ($ img, $ fontsize, $ jiaodu, $ x1, $ y1, $ color, $ fontfile, $ text );
Switch ($ arr [2]) {
Case 1: header ('content-type: image/gif'); imagegif ($ img, $ newImageName, 100); break;
Case 2: header ('content-type: image/jpeg '); imagejpeg ($ img, $ newImageName, 100); break;
Case 3: header ('content-type: image/png '); imagepng ($ img, $ newImageName, 100); break;
}
Return 1;
}