/** * Created by PhpStorm. * User: MCtion * Date: 2015/5/14 0014 * Time: * Simple Image class library. all relative paths in this class are based on the website root directory. to modify them, modify the constant _ WEBROOT _. * Function: specify the text content to create an Image (Chinese characters not supported), create a verification code Image, create a thumbnail, and other functions to be continued * Method: * Style (array $ Options) sets the image Style. it is reset to the default Style before each setting. * Create_Img_Png (): create a PNG image. the relevant attributes are specified by the Style. * Create_Img_Jpeg (): Creates a JPEG image. the attributes are specified by the Style. * Create_Verify () creates a verification code Image. the relevant attributes are specified by the Style. * Get_Verify () obtains the created verification code value, which is MD5 encrypted. * Load_Img (string $ FilePath) loads the image and creates an image source for other methods to call. the FilePath is the relative path of the image. * Create_Thumb (string $ FileName, string $ FilePath): create a thumbnail of the image loaded by Load_Img (). FileName is the prefix of the saved image, and FilePath is the relative path of the saved image, does not contain file names (for example,/Uploads/images/Thumb /) */ If (! Defined ("_ WEBROOT _") define ("_ WEBROOT _", $ _ SERVER ['document _ root']); Class Img { Protected $ _ Img; // Image Source Protected $ _ FileImg; // the uploaded image source. Protected $ _ FileInfo; // array of loaded image information Protected $ _ PicInfo; // array of the width and height of the loaded image Protected $ _ Rand = 'abcdefghijkmnopqrstuvwxyzabcdefghjklmnopqrstuvwxyz1234567890'; // random factor Protected $ _ Code = ''; // verification Code Public $ Width = 300; // Default image Width Public $ Height = 80; // The default Height of the image Public $ BackgroundColor = "000000 "; Public $ Font = "/phps/Public/Font/ARIALNB. TTF"; // Default Font Public $ FontSize = 16; // Default Font size Public $ FontColor = "ffffff"; // Default Font color Public $ Content = "Test Word "; Public $ Align = "left "; Public $ Codes = 4; // Number of verification Codes Public $ Line = 6; // Number of interfering lines Public $ LoadErr = ''; // error message // Public function _ construct (){} /** Set Image attributes * @ Param array $ Options attribute array * @ Return $ this returns the object */ Public function Style ($ Options ){ $ This-> _ Re_Set (); Foreach ($ Options as $ K => $ V ){ If (in_array ($ K, array ('width', 'height', 'backgroundcolor', 'font', 'fontsize', 'fontcolor', 'content ', 'align ', 'Code', 'line', 'snow '))){ If ($ K = "BackgroundColor" | $ K = "FontColor "){ If (preg_match ("# [a-zA-Z0-9] {6}) #", $ V) $ this-> $ K = $ V; } Else { $ This-> $ K = $ V; } } } Return $ this; } /** * Resetting properties does not provide external access */ Protected function _ Re_Set (){ $ This-> Width = 100; $ This-> Height = 30; $ This-> BackgroundColor = "000000 "; $ This-> Font = "/phps/Public/Font/ARIALNB. TTF "; $ This-> FontSize = 16; $ This-> FontColor = "ffffff "; $ This-> Align = "left "; $ This-> Codes = 4; $ This-> Line = 6; } /** * Create an image source, add a background, and create an image * @ Param bool $ BGC specifies whether to create the background color and rectangular block. */ Protected function _ Create_Img_GB ($ BGC = True ){ $ This-> _ Img = imagecreatetruecolor ($ this-> Width, $ this-> Height); // create a back scene Source If ($ BGC ){ Preg_match ("# ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) #", $ this-> BackgroundColor, $ ColorArr); // separate the color values into three groups of hexadecimal numbers. $ Color = imagecolorallocate ($ this-> _ Img, hexdec ($ ColorArr [1]), hexdec ($ ColorArr [2]), hexdec ($ ColorArr [3]); // add the background color to the Img Image Source Imagefilledrectangle ($ this-> _ Img, 0, $ this-> Height, $ this-> Width, 0, $ Color); // create an image } } /** * Create a random verification code */ Protected function _ Create_Code (){ $ Len = strlen ($ this-> _ Rand)-1; For ($ I = 0; $ I <$ this-> Codes; $ I ++ ){ $ This-> _ Code. = $ this-> _ Rand [mt_rand (0, $ Len)]; } } /** * Writing a string to an image does not support Chinese characters. */ Protected function _ Write_Text (){ $ FontWidth = imagefontwidth ($ this-> FontSize); // Obtain the width of a character in the font size. Preg_match_all ('/(.)/us', $ this-> Content, $ TextArr); // separate the Content into an array to count the number of times. $ FontHeight = imagefontheight ($ this-> FontSize); // Obtain the font size height. $ X = ceil ($ this-> Width-($ FontWidth * count ($ TextArr [0])/2); // set the distance between the X axis and the left $ Y = ceil ($ this-> Height + $ FontHeight)/2); // you can specify the distance between the Y axis and the top margin. Preg_match ("# ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) #", $ this-> FontColor, $ ColorArr ); $ Color = imagecolorallocate ($ this-> _ Img, hexdec ($ ColorArr [1]), hexdec ($ ColorArr [2]), hexdec ($ ColorArr [3]); // set the text color Imagettftext ($ this-> _ Img, $ this-> FontSize, 0, $ X, $ Y, $ Color ,__ WEBROOT __. $ this-> Font, $ this-> Content); // write Content } /** * Write a verification code to the image. */ Protected function _ Write_Code (){ $ _ X = $ this-> Width/$ this-> Codes; // you can specify the aspect ratio. For ($ I = 0; $ I <$ this-> Codes; $ I ++) {// cyclically Codes times, each time a verification code value is generated $ Color = imagecolorallocate ($ this-> _ Img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); // Color of the randomly generated verification code value Imagettftext ($ this-> _ Img, $ this-> FontSize, mt_rand (-30, 30), $ _ X * $ I + mt_rand (1, 5 ), $ this-> Height/1.3, $ Color ,__ WEBROOT __. $ this-> Font, $ this-> _ Code [$ I]); // generates a verification Code value. } } /** * Write interference lines to the image */ Protected function _ Write_Line () {// generates interference lines For ($ I = 0; $ I <$ this-> Line; $ I ++ ){ $ Color = imagecolorallocate ($ this-> _ Img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 )); Imageline ($ this-> _ Img, mt_rand (0, $ this-> Width), mt_rand (0, $ this-> Height), mt_rand (0, $ this-> Width), mt_rand (0, $ this-> Height), $ Color ); } } /** * Set the image type to JPEG. */ Protected function _ Img_Jpeg (){ Header ('content-type: image/jpeg '); Imagejpeg ($ this-> _ Img ); Imagedestroy ($ this-> _ Img ); } /** * Set the image type to PNG. */ Protected function _ Img_Png (){ Header ('content-type: image/png '); Imagepng ($ this-> _ Img ); Imagedestroy ($ this-> _ Img ); } /** * Create a JPEG string image */ Public function Create_Img_Jpg (){ $ This-> _ Create_Img_GB (True ); $ This-> _ Write_Text (); $ This-> _ Img_Jpeg (); } /** * Create a PNG string image */ Public function Create_Img_Png (){ $ This-> _ Create_Img_GB (True ); $ This-> _ Write_Text (); $ This-> _ Img_Png (); } /** * Create a PNG image of the verification code */ Public function Create_Verify (){ $ This-> BackgroundColor = ''; For ($ I = 0; $ I <3; $ I ++ ){ $ This-& gt; BackgroundColor. = dechex (mt_rand (20,155 )); } $ This-> _ Create_Img_GB (True ); $ This-> _ Create_Code (); $ This-> _ Write_Line (); $ This-> _ Write_Code (); $ This-> _ Img_Png (); } /** * Obtain the MD5 encrypted verification code externally * @ Return string */ Public function Get_Verify (){ Return md5 ($ this-> _ Code ); } /** * Load an image file and obtain image-related information * @ Param string $ FilePath relative path of the image * @ Return $ this | the bool returns an object successfully; otherwise, FALSE is returned. */ Public function Load_Img ($ FilePath ){ $ FilePath = _ WEBROOT _. $ FilePath; If (! Is_file ($ FilePath )){ $ This-> LoadErr = "path error, file does not exist "; Return False; } $ This-> _ PicInfo = getimagesize ($ FilePath ); $ This-> _ FileInfo = pathinfo ($ FilePath ); Switch ($ this-> _ PicInfo [2]) { Case 1: $ this-> _ FileImg = imagecreatefromgif ($ FilePath); break; Case 2: $ this-> _ FileImg = imagecreatefromjpeg ($ FilePath); break; Case 3: $ this-> _ FileImg = imagecreatefrompng ($ FilePath); break; Default: $ this-> LoadErr = "type error, unsupported image type"; Return False; } Return True; } /** * Create a thumbnail * @ Param string $ prefix of the image name saved by FileName * @ Param string $ FilePath: save the relative path of the image * @ Return mixed returns the information array of the generated image. */ Public function Create_Thumb ($ FileName, $ FilePath ){ $ SavePath = _ WEBROOT _. $ FilePath; If (! File_exists ($ SavePath )){ Mkdir ($ savepath, 0777, true ); } $ FileName = $ FileName. date ("YmdHis"). rand (100,999). '.'. $ this-> _ FileInfo ['extension']; $ FilePath = $ FilePath. $ FileName; $ SavePath = $ SavePath. $ FileName; $ This-> _ Create_Img_GB (False ); Imagecopyresampled ($ this-> _ Img, $ this-> _ FileImg, 0, 0, 0, $ this-> Width, $ this-> Height, $ this-> _ PicInfo [0], $ this-> _ PicInfo [1]); Switch ($ this-> _ PicInfo [2]) { Case 1: imagegif ($ this-> _ Img, $ SavePath); break; Case 2: imagejpeg ($ this-> _ Img, $ SavePath); break; Case 3: imagepng ($ this-> _ Img, $ SavePath); break; } $ FIleInfo ['filename'] = $ FileName; $ FIleInfo ['filepath'] = $ FilePath; Return $ FIleInfo; } } |