The php class library adds text watermarks to existing images. the code is not very complete. thank you for your advice! The code is as follows:
-
- /* PHP image and text watermark class library
- QQ: 3697578482 sad songs
- Currently, this class library only supports text watermarks. the position is in the lower right corner and the color is random.
- Call method:
- 1. introduce a class library at the top of the file to be watermarked:
- Include_once 'imageclass. php ';
- 2. declare a new class:
- $ Tpl = new image_fu;
- 3. provide parameters for image watermarks:
- $ Tpl-> img (image path, watermark text, font path, font size, font angle );
- For example: $ tpl-> img('abc.jpg ', 'This is the watermark text', 'ziti. ttf)
- */
- Class image_fu {
- Private $ image;
- Private $ img_info;
- Private $ img_width;
- Private $ img_height;
- Private $ img_im;
- Private $ img_text;
- Private $ img_ttf = '';
- Private $ img_new;
- Private $ img_text_size;
- Private $ img_jd;
- Function img ($ img = '', $ txt ='', $ ttf = '', $ size = 12, $ jiaodu = 0 ){
- If (isset ($ img) & file_exists ($ img) {// check whether the image exists
- $ This-> image = $ img;
- $ This-> img_text = $ txt;
- $ This-> img_text_size = $ size;
- $ This-> img_jd = $ jiaodu;
- If (file_exists ($ ttf )){
- $ This-> img_ttf = $ ttf;
- } Else {
- Exit ('font File: '. $ ttf.' does not exist! ');
- }
- $ This-> imgyesno ();
- } Else {
- Exit ('Image File: '. $ img.' doesn't exist ');
- }
- }
- Private function imgyesno (){
- $ This-> img_info = getimagesize ($ this-> image );
- $ This-> img_width = $ this-> img_info [0]; // image width
- $ This-> img_height = $ this-> img_info [1]; // picture height
- // Check the image type
- Switch ($ this-> img_info [2]) {
- Case 1: $ this-> img_im = imagecreatefromgif ($ this-> image); break;
- Case 2: $ this-> img_im = imagecreatefromjpeg ($ this-> image); break;
- Case 3: $ this-> img_im = imagecreatefrompng ($ this-> image); break;
- Default: exit ('watermarks are not supported in image format ');
- }
- $ This-> img_text ();
- }
- Private function img_text (){
- Imagealphablending ($ this-> img_im, true );
- // Set the color
- $ Color = imagecolorallocate ($ this-> img_im, rand (0,255), rand (0,255), rand (0,255 ));
- $ Txt_height = $ this-> img_text_size;
- $ Txt_jiaodu = $ this-> img_jd;
- $ Ttf_im = imagettfbbox ($ txt_height, $ txt_jiaodu, $ this-> img_ttf, $ this-> img_text );
- $ W = $ ttf_im [2]-$ ttf_im [6];
- $ H = $ ttf_im [3]-$ ttf_im [7];
- // $ W = $ ttf_im [7];
- // $ H = $ ttf_im [8];
- Unset ($ ttf_im );
- $ Txt_y = $ this-> img_height-$ h;
- $ Txt_x = $ this-> img_width-$ w;
- // $ Txt_y = 0;
- // $ Txt_x = 0;
- $ This-> img_new = @ imagettftext ($ this-> img_im, $ txt_height, $ txt_jiaodu, $ txt_x, $ txt_y, $ color, $ this-> img_ttf, $ this-> img_text );
- @ Unlink ($ this-> image); // delete an image
- Switch ($ this-> img_info [2]) {// Obtain the background image format
- Case 1: imagegif ($ this-> img_im, $ this-> image); break;
- Case 2: imagejpeg ($ this-> img_im, $ this-> image); break;
- Case 3: imagepng ($ this-> img_im, $ this-> image); break;
- Default: exit ('watermark Image failed ');
- }
- }
- // Display the image
- Function img_show () {echo 'image. '"border =" 0 "alt ="'. $ this-> img_text. '"/> ';}
- // Release the memory
- Private function img_nothing (){
- Unset ($ this-> img_info );
- Imagedestroy ($ this-> img_im );
- }
- }
- ?>
|