- /*
- * Function: php image watermark (watermarks support images or text)
- * Parameters:
- * $ Groundimage: specifies the background image to be Watermark. Currently, only gif, jpg, and png formats are supported;
- * $ Waterpos watermark position, which has 10 statuses. 0 indicates a random position;
- * 1 is the top left, 2 is the top center, and 3 is the top right;
- * 4: center left, 5: center, and 6: center right;
- * 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
- * $ Waterimage: specifies the image Watermark. Currently, only the gif, jpg, and png formats are supported;
- * $ Watertext text watermark: uses text as a watermark. it supports ascii codes and does not support Chinese characters;
- * $ Textfont text size. The value is 1, 2, 3, 4, or 5. the default value is 5;
- * $ Textcolor text color. The value is a hexadecimal color value. The default value is # ff0000 (red );
- *
- * Note: support gd 2.0, support freetype, gif read, gif create, jpg, and png
- * $ Waterimage and $ watertext should not be used at the same time. select one of them and use $ waterimage first.
- * When $ waterimage is valid, $ waterstring, $ stringfont, and $ stringcolor are invalid.
- * The name of the watermark image is the same as that of $ groundimage.
- * Author: longware @ 2004-11-3 14:15:13
- */
- Function imagewatermark ($ groundimage, $ waterpos = 0, $ waterimage = "", $ watertext = "", $ textfont = 5, $ textcolor = "# ff0000 ")
- {
- $ Iswaterimage = false;
- $ Formatmsg = "this file format is not supported Currently. use image processing software to convert images to gif, jpg, and png formats. ";
- // Read the watermark file
- If (! Empty ($ waterimage) & file_exists ($ waterimage ))
- {
- $ Iswaterimage = true;
- $ Water_info = getimagesize ($ waterimage );
- $ Water_w = $ water_info [0]; // Obtain the watermark image width.
- $ Water_h = $ water_info [1]; // Get the watermark image height
- Switch ($ water_info [2]) // Obtain the watermark image format
- {
- Case 1: $ water_im = imagecreatefromgif ($ waterimage); break;
- Case 2: $ water_im = imagecreatefromjpeg ($ waterimage); break;
- Case 3: $ water_im = imagecreatefrompng ($ waterimage); break;
- Default: die ($ formatmsg );
- }
- }
- // Read the background image
- If (! Empty ($ groundimage) & file_exists ($ groundimage ))
- {
- $ Ground_info = getimagesize ($ groundimage );
- $ Ground_w = $ ground_info [0]; // Obtain the width of the background image.
- $ Ground_h = $ ground_info [1]; // get the height of the background image
- Switch ($ ground_info [2]) // Obtain the background image format
- {
- Case 1: $ ground_im = imagecreatefromgif ($ groundimage); break;
- Case 2: $ ground_im = imagecreatefromjpeg ($ groundimage); break;
- Case 3: $ ground_im = imagecreatefrompng ($ groundimage); break;
- Default: die ($ formatmsg );
- }
- }
- Else
- {
- Die ("the image to be watermarked does not exist! ");
- }
- // Watermark Position
- If ($ iswaterimage) // image watermark
- {
- $ W = $ water_w;
- $ H = $ water_h;
- $ Label = "image ";
- }
- Else // text watermark
- {
- $ Temp = imagettfbbox (ceil ($ textfont * 2.5), 0, "./cour. ttf", $ watertext); // get the text range using the truetype font
- $ W = $ temp [2]-$ temp [6];
- $ H = $ temp [3]-$ temp [7];
- Unset ($ temp );
- $ Label = "text area ";
- }
- If ($ ground_w <$ w) | ($ ground_h <$ h ))
- {
- Echo "the length or width of the image to be watermark is smaller than that of the watermark". $ label. ". The Watermark cannot be generated! ";
- Return;
- }
- Switch ($ waterpos)
- {
- Case 0: // random
- $ Posx = rand (0, ($ ground_w-$ w ));
- $ Posy = rand (0, ($ ground_h-$ h ));
- Break;
- Case 1: // 1 is the top left
- $ Posx = 0;
- $ Posy = 0;
- Break;
- Case 2: // 2 center the top
- $ Posx = ($ ground_w-$ w)/2;
- $ Posy = 0;
- Break;
- Case 3: // 3: top right
- $ Posx = $ ground_w-$ w;
- $ Posy = 0;
- Break;
- Case 4: // 4 is left in the middle
- $ Posx = 0;
- $ Posy = ($ ground_h-$ h)/2;
- Break;
- Case 5: // 5 center in the middle
- $ Posx = ($ ground_w-$ w)/2;
- $ Posy = ($ ground_h-$ h)/2;
- Break;
- Case 6: // 6 is the center and right
- $ Posx = $ ground_w-$ w;
- $ Posy = ($ ground_h-$ h)/2;
- Break;
- Case 7: // 7 is left at the bottom
- $ Posx = 0;
- $ Posy = $ ground_h-$ h;
- Break;
- Case 8: // 8 is centered at the bottom
- $ Posx = ($ ground_w-$ w)/2;
- $ Posy = $ ground_h-$ h;
- Break;
- Case 9: // 9: right
- $ Posx = $ ground_w-$ w;
- $ Posy = $ ground_h-$ h;
- Break;
- Default: // random
- $ Posx = rand (0, ($ ground_w-$ w ));
- $ Posy = rand (0, ($ ground_h-$ h ));
- Break;
- }
- // Set the mixed color mode of the image
- Imagealphablending ($ ground_im, true );
- If ($ iswaterimage) // image watermark
- {
- Imagecopy ($ ground_im, $ water_im, $ posx, $ posy, 0, 0, $ water_w, $ water_h); // Copy the watermark to the target file
- }
- Else // text watermark
- {
- If (! Empty ($ textcolor) & (strlen ($ textcolor) = 7 ))
- {
- $ R = hexdec (substr ($ textcolor, 1, 2 ));
- $ G = hexdec (substr ($ textcolor, 3, 2 ));
- $ B = hexdec (substr ($ textcolor, 5 ));
- }
- Else
- {
- Die ("the watermark text color format is incorrect! ");
- }
- Imagestring ($ ground_im, $ textfont, $ posx, $ posy, $ watertext, imagecolorallocate ($ ground_im, $ r, $ g, $ B ));
- }
- // The image after the watermark is generated
- @ Unlink ($ groundimage );
- Switch ($ ground_info [2]) // Obtain the background image format
- {
- Case 1: imagegif ($ ground_im, $ groundimage); break;
- Case 2: imagejpeg ($ ground_im, $ groundimage); break;
- Case 3: imagepng ($ ground_im, $ groundimage); break;
- Default: die ($ errormsg );
- }
- // Release the memory
- If (isset ($ water_info) unset ($ water_info );
- If (isset ($ water_im) imagedestroy ($ water_im );
- Unset ($ ground_info );
- Imagedestroy ($ ground_im );
- }
- //--------
- If (isset ($ _ files )&&! Empty ($ _ files ['userfile']) & $ _ files ['userfile'] ['size']> 0)
- {
- $ Uploadfile = "./". time (). "_". $ _ files ['userfile'] ['name'];
- If (copy ($ _ files ['userfile'] ['tmp _ name'], $ uploadfile ))
- {
- Echo "OK
";
- // Text watermark
- Imagewatermark ($ uploadfile, 0, "", "http://blog.csdn.net/longware/", 5, "# ff0000 ");
- // Image watermark
- // $ Waterimage = "./hanweb_shuiyin.gif"; // watermark image path
- // Imagewatermark ($ uploadfile, 0, $ waterimage );
- Echo "";
- }
- Else
- {
- Echo "fail
";
- }
- }
- ?>
|