PHP image processing class library and demo sharing a simple PHP image processing class library. although there are few functions, it is not very advanced yet. I will try it later, you can also tell me which of the following functions can be added to your suggestion or your need. if you have extended this class library, please share it with us, the code can be used now. there are not many things to consider. please let me know if you have any better suggestions. thank you.
Img. php
_ Re_Set (); foreach ($ Options as $ K => $ V) {if (in_array ($ K, array ('width', ''height '', ''backgroundcolor'', ''font'', ''fontsize', ''content'', ''align '', ''codes '', ''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;}/*** reset attribute, no external access */protected function _ Re_Set () {$ t His-> 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 specify 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 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)] ;}}/*** write strings to the image. Chinese characters */protected function _ Write_Text () are not supported currently () {$ 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 calculate 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 side. $ Y = ceil ($ this-> Height + $ FontHeight)/2 ); // set the distance from the Y axis to 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]); // sets the text color imagettftext ($ this-> _ Img, $ this-> FontSize, 0, $ X, $ Y, $ Color, __webroot __. $ this-> Font, $ this-> Content); // write Content}/*** write verification code to the image */protected function _ Write_Code () {$ _ X = $ this-> Width/$ this-> Codes; // set the Width to height ratio for ($ I = 0; $ I <$ this-> Codes; $ I ++) {// cyclically Codes, each time a verification code value $ Color = imagecolorallocate ($ this-> _ Img, mt_rand (0,156), mt_rand (0,156) is generated ), mt_rand (0,156); // The color of the randomly generated verification code value imagettftext ($ this-> _ Img, $ this-> FontSize, mt_rand (-30, 30), $ _ X * $ I + mt_rand (1.3), $ this-> Height/, $ Color ,__ WEBROOT __. $ this-> Font, $ this-> _ Code [$ I]); // Generate a verification code value}/*** write interference lines to the image */protected function _ Write_Line () {// generate 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 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-> BackgroundColor. = dechex (mt_rand (20,155);} $ this-> _ Create_Img_GB (Tru E); $ 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: the relative path of the image * @ return $ this | the bool returns an object, otherwise, FALSE */public function Load_Img ($ FilePath) {$ FilePath = _ WEBROOT _ is returned __. $ 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 array of information 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, 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 ;}}
Example
$ Img = new Img (); $ Options [''width''] = 300; $ Options [''height'] = 100; $ Options [''content''] = "Test Create Img"; $ Options [''fontcolor''] = "FF0000 "; $ Options [''backgroundcolor''] = "AAAAAA"; $ Img-> Style ($ Options)-> Create_Img_Jpg (); if ($ Img-> Load_Img ("/Public/images/ad1.png") {$ FileInfo = $ Img-> Style (array ('width'' => 30, ''height' => 30)-> Create_Thumb ("Thumb", "/Uploads/images/"); var_dump ($ FileInfo );} else {die ("failed to load image ,". $ Img-> LoadErr );}
Reprinted from: http://www.aspnetjia.com