Generate a picture class with a watermark (support watermark is a picture or text)

xiaoxiao2021-03-06  83

$ img = new gimage (); // $ IMG-> WM_Text = "We are all Chinese"; $ IMG-> WM_Text = "www.discuz.com"; $ IMG-> WM_Text_Font = "C: / Windows / FONTS / StXINWEI.TTTF "; // $ IMG-> WM_IMAGE_NAME =" Images / b_edit.png "; $ IMG-> CREATE (" ./ mouse.jpg ");

Where mouse.jpg is the picture name you want to add watermark, note that the path name stxinwei.ttf is the path name of the font file file name is a simple test. If you want to adjust the more complex display, you can modify the properties in the class, such as enlargement of fonts can be $ IMG-> WM_TEXT_SIZE = 20; add watermark pictures to $ IMG-> WM_IMAGE_NAME = "file name";

* /

Class GIMAGE {VAR $ SRC_IMAGE_NAME = ""; // Enter the file name of the image (must include path name) var $ jpeg_quality = 90; // jpeg picture quality var $ save_file = ""; // Output file name var $ wm_image_name = ""; // Watermark image file name (must include path name) var $ wm_image_pos = 3; // Watermark picture placement position // 0 = middle // 1 = TOP LEFT // 2 = TOP Right // 3 = Bottom Right // 4 = Bottom Left // 5 = Top Middle // 6 = Middle Right // 7 = Bottom Middle // 8 = Middle Left // Other = 3Var $ WM_IMAGE_TRANSITION = 80; // Watermark Picture with the original picture Fusion (1 = 100)

VAR $ wm_text = ""; // Watermark text (supporting Chinese and English, cross-line text with / R / N) var $ wm_text_size = 20; // Watermark text size var $ wm_text_angle = 5; // Watermark text angle, this Try not to change VAR $ WM_TEXT_POS = 3; // Watermark text Place position VAR $ wm_text_font = ""; // Watermark text Fonts Var $ WM_TEXT_COLOR = "#cccccc"; // Watermark font color value Function Create ($ filename) = "") {IF ($ filename) {$ this-> src_image_name = start (Trim ($ filename));}

$ Src_image_type = $ this-> get_type ($ this-> src_image_name); $ src_image = $ this-> createImage ($ src_image_type, $ this-> src_image_name); if (! $ Src_image) return; $ src_image_w = ImageSX ($ src_image $ SRC_IMAGE_H = ImageESY ($ SRC_IMAGE);

if ($ this-> wm_image_name) {$ this-> wm_image_name = strtolower (trim ($ this-> wm_image_name)); $ wm_image_type = $ this-> get_type ($ this-> wm_image_name); $ wm_image = $ this-> createImage ($ wm_image_type, $ this-> wm_image_name); $ wm_image_w = ImageSX ($ wm_image); $ wm_image_h = ImageSY ($ wm_image); $ temp_wm_image = $ this-> getPos ($ src_image_w, $ src_image_h, $ this-> wm_image_pos , $ wm_image); $ wm_image_x = $ temp_wm_image [ "dest_x"]; $ wm_image_y = $ temp_wm_image [ "dest_y"]; imageCopyMerge ($ src_image, $ wm_image, $ wm_image_x, $ wm_image_y, 0,0, $ wm_image_w, $ wm_image_h $ this-> wm_image_transition);

IF ($ this-> wm_text) {$ this-> wm_text = $ this-> GB2UTF8 ($ this-> wm_text); $ TEMP_WM_TEXT = $ THIS-> GetPOS ($ src_image_w, $ src_image_h, $ this-> wm_text_pos); $ WM_TEXT_X = $ TEMP_WM_TEXT ["DEST_X"]; $ wm_text_y = $ TEMP_WM_TEXT ["DEST_Y"]; if (preg_match ("/ ([A-F0-9] [A-F0-9]) ([A-F0- 9] [A-F0-9]) ([A-F0-9]) / I ", $ this-> wm_text_color, $ color)) {$ red = hexdec ($ color [1 ]); $ Green = HEXDEC ($ Color [2]); $ blue = HEXDEC ($ Color [3]); $ wm_text_color = imagecolorallocate ($ SRC_IMAGE, $ Red, $ Green, $ blue);} else {$ wm_text_color = imagecolorallocate ($ src_image, 255,255,255);} imagettftext ($ src_image, $ this-> wm_text_size, $ this-> wm_text_angle, $ wm_text_x, $ wm_text_y, $ wm_text_color, $ this-> wm_text_font, $ this-> wm_text);}

IF ($ this-> save_file) {switch ($ this-> get_type ($ this-> save_file) {case 'gif': $ src_img = imagepng ($ src_image, $ this-> save_file); break; case 'jpeg ': $ src_img = imageJPEG ($ src_image, $ this-> save_file, $ this-> jpeg_quality); break; case' png ': $ src_img = ImagePNG ($ src_image, $ this-> save_file); break; default: $ src_img = imageJPEG ($ src_image, $ this-> save_file, $ this-> jpeg_quality); break;}} else {if ($ src_image_type = "jpg") $ src_image_type = "jpeg"; header ( "Content-type: image / {$ src_image_type} "); switch ($ src_image_type) {case 'gif': $ src_img = ImagePNG ($ src_image); break; case 'jpg': $ src_img = imageJPEG ($ src_image," ", $ this-> jpeg_quality); break; case 'png': $ src_img = ImagePNG ($ src_image); break; default: $ src_img = imageJPEG ($ src_image, "", $ this-> jpeg_quality); break;}} imagedestroy ($ src_image) } / * / * CREATEIMAGE Create a picture internal function according to the file name and type

$ TYPE: The type of pictures, including GIF, JPG, PNG $ IMG_NAME: Image file name, including path name, for example "./mouse.jpg" /Function CreateImage ($ TYPE, $ IMG_NAME) {if (! $ TYPE) {$ TYPE = $ this-> get_type ($ IMG_NAME);

switch ($ type) {case 'gif': if (function_exists ( 'imagecreatefromgif')) $ tmp_img = @ ImageCreateFromGIF ($ img_name); break; case 'jpg': $ tmp_img = ImageCreateFromJPEG ($ img_name); break; case ' PNG ': $ TMP_IMG = ImageCreateFromNG ($ IMG_NAME); Break; Default: $ TMP_IMG = ImageCreateFromString ($ IMG_NAME); Break;} Return $ TMP_IMG;} / * GETPOS generates the internal function of the watermark in the source image based on the length, wide, position code, and watermark image ID of the source image.

$ sourceFile_Width: Source Image Width $ SourceFile_HEIGHT: Original Image High $ POS: Position Code // 0 = Middle // 1 = Top Left // 2 = TOP Right // 3 = Bottom Right // 4 = Bottom LEFT //// 5 = TOP MIDDLE / / 6 = Middle Right // 7 = bottom middle // 8 = Middle Left $ WM_IMAGE: Watermark Image ID * / Function GetPOS ($ SOUREFILE_WIDTH, $ SOURCEFILE_HEIGHT, $ POS, $ WM_IMAGE = "") {IF ($ wm_image) {$ insertfile_width = ImageSx ($ wm_image); $ insertfile_height = ImageSy ($ wm_image);} else {$ lineCount = explode ( "/ r / n", $ this-> wm_text); $ fontSize = imagettfbbox ( $ this-> wm_text_size, $ this-> wm_text_angle, $ this-> wm_text_font, $ this-> wm_text); $ insertfile_width = $ fontSize [2] - $ fontSize [0]; $ insertfile_height = count ($ lineCount) * ( $ FONTSIZE [1] - $ FONTSIZE [3]);} Switch ($ POS) {Case 0: $ DEST_X = ($ SOURCEFILE_WIDTH / 2) - ($ INSERTFILE_WIDTH / 2); $ dest_y = ($ sourcefile_height / 2) - ($ INSERT File_Height / 2); Break;

Case 1: $ dest_x = 0; if ($ this-> wm_text) {$ dest_y = $ insertfile_Height;} else {$ dest_y = 0;

Case 2: $ DEST_X = $ SOURCEFILE_WIDTH - $ INSERTFILE_WIDTH; if ($ this-> wm_text) {$ dest_y = $ insertfile_height;} else {$ dest_y = 0;} Break;

case 3: $ dest_x = $ sourcefile_width - $ insertfile_width; $ dest_y = $ sourcefile_height - $ insertfile_height; break; case 4: $ dest_x = 0; $ dest_y = $ sourcefile_height - $ insertfile_height; break;

Case 5: $ DEST_X = ($ SOURCEFILE_WIDTH - $ INSERTFILE_WIDTH) / 2); if ($ this-> wm_text) {$ dest_y = $ insertfile_height;} else {$ dest_y = 0;

Case 6: $ DEST_X = $ SOURCEFILE_WIDTH - $ INSERTFILE_WIDTH; $ DEST_Y = ($ Sourcefile_Heart / 2) - ($ INSERTFILE_HEIGHT / 2); Break;

Case 7: $ DEST_X = ($ SOURCEFILE_WIDTH - $ INSERTFILE_WIDTH) / 2); $ dest_y = $ sourcefile_height - $ insertfile_height; break;

Case 8: $ DEST_X = 0; $ dest_y = ($ sourcefile_height / 2) - ($ INSERTFILE_HEIGHT / 2); Break;

default: $ dest_x = $ sourcefile_width - $ insertfile_width; $ dest_y = $ sourcefile_height - $ insertfile_height; break;} return array ( "dest_x" => $ dest_x, "dest_y" => $ dest_y);}

/ * GB2UTF8 Specifies the text to UTF-8 format, including Chinese and English mixed internal functions * / function GB2UTF8 ($ GB) {if (! Trim ($ GB) RETURN $ GB; $ filename = "./ gb2312.txt"; $ tmp = file ($ filename); $ CODETABLE = array (); while (list ($ key, $ value) = Each ($ TMP)) $ CODETABLE [HEXDEC (Substr ($ Value, 0, 6))] = Substr ($ Value, 7, 6); $ UTF8 = "; While ($ GB) {if (ORD (Substr ($ GB, 0, 1 ))))> 127) {$ TTHIS = Substr ($ GB, 0, 2); $ GB = Substr ($ GB, 2, Strlen ($ GB) -2); $ UTF8. = $ This-> u2UTF8 (HEXDEC $ CODETABLE [HEXDEC (bin2hex ($ tthis)) - 0x8080]);} else {$ TTHIS = SUBSTR ($ GB, 0, 1); $ GB = Substr ($ GB, 1, Strlen ($ GB) -1 $ UTF8. = $ THIS-> U2UTF8 ($ TTHIS);}}

RETURN $ UTF8;

Function U2UTF8 ($ C) {$ Str = ""; IF ($ C <0x80) {$ Str. = $ C;} else IF ($ c <0x800) {$ Str. = Chr (0xc0 | $ C >> 6); $ Str. = CHR (0x80 | $ C & 0x3F); Else IF ($ C <0x10000) {$ Str. = CHR (0xE0 | $ C >> 12); $ Str. = CHR (0x80 | $ C >> 6 & 0x3f); $ Str. = Chr (0x80 | $ C & 0x3f); Else IF ($ C <0x200000) {$ Str. = Chr (0xF0 | $ C >> 18); $ STR = CHR (0x80 | $ C >> 12 & 0x3f); $ Str. = Chr (0x80 | $ C >> 6 & 0x3f); $ Str. = Chr (0x80 | $ C & 0x3F);} Return $ STR } / * GET_TYPE Gets the format of the picture, including JPG, PNG, GIF internal functions

$ IMG_NAME: Image file name, can include path name * / function get_type ($ IMG_NAME) // Get image file type {$ name_Array = EXPLODE (".", $ IMG_NAME); if (preg_match ("//. (jpg | JPEG | GIF | PNG) $ / ", $ IMG_NAME, $ matches)) {$ TYPE = STRTOLOWER ($ matches [1]);} else {$ type =" string ";} Return $ TYPE;}

}?>

转载请注明原文地址:https://www.9cbs.com/read-107189.html

New Post(0)