PHP image processing class library and demonstration sharing, php image processing class library _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP image processing class library and demo sharing, php image processing class library. PHP image processing class library and demonstration and sharing. The php image processing class library simply writes a PHP image processing class library. although it has few functions, it is useless at present, PHP image processing class library and demo sharing will be used later, and php image processing class library will be used

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.

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

Ghost has simply written a PHP image processing class library. although it has few functions, it is not very advanced yet and will be used in the future...

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.