Simple PHP image processing class library

Source: Internet
Author: User
Tags imagejpeg
I simply wrote a PHP image processing class library. Although it has few functions, it is not very advanced at present. I will use it later, or which of the following functions can I add to my suggestion, if you want to extend the class library, you can share it with me.

I simply wrote a PHP image processing class library. Although it has few functions, it is not very advanced at present. I will use it later, or which of the following functions can I add to my suggestion, if you want to extend the class library, you can share it with me.

I simply wrote a PHP image processing class library. Although it has few functions, it is not very advanced at present. I will use it later, or which of the following functions can I add to my suggestion, if you want to extend the class library, I have time to add it. If you want to extend the class library, you can share it with me. 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. <无>
  _ 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 ;} /*** reset the property and do 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 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_mat Ch ("# ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) ([a-zA-Z0-9] {2}) #", $ this-> BackgroundColor, $ ColorArr); // separate 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 from the X axis to the left. $ 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 the verification code to the image */protected function _ Write_Code () {$ _ X = $ this-> Width/$ this-> Codes; // set 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 )); // 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 _ Im G_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 CRER Ate_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 (True); $ this-> _ Create_Code (); $ this-> _ Write_Line (); $ this-> _ Write_Code (); $ this-> _ Img_Png ();}/*** obtain the MD5 encrypted Verification Code externally * @ return s Tring */public function Get_Verify () {return md5 ($ this-> _ Code);}/*** load an image file, obtain image information * @ param string $ FilePath relative path address of the image * @ return $ this | the bool returns the object successfully; otherwise, FALSE */public function Load_Img ($ FilePath) is returned) {$ 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, no Supported image types "; 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 ;}}
$ 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 ("image loading failed ,". $ Img-> LoadErr );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.