PHP Image processing library and demo sharing, PHP image processing class Library _php tutorial

Source: Internet
Author: User
Tags image processing library imagejpeg

PHP Image processing class library and demo sharing, PHP image processing class Library


Simply write a PHP image processing class library, although the function is relatively small, but at present also useless to too advanced, later used to fill it, or the point of the suggestion add what function, or what needs can tell me, I have time to add, if who has extended to this class library, Also trouble to take out everyone share, the code is now can be used on the line, considering the things are not many, what better advice please tell me, thank you

img.php

  _re_set (); foreach ($Options as $K = + $V) {if (In_array ($K, Array (' Width ', ' Height ', ' backgroundcolor ', ' FontSize ', ' fontcolor ', ' 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 property, no 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, create an image * @param bool $BGC Specify whether to create a background color and a rectangular block */protected function _create_img_gb ($BGC = True) {$t His-_img = Imagecreatetruecolor ($this Width, $this-Height);   Create background source if ($BGC) {   Preg_match ("# ([a-za-z0-9]{2}] ([a-za-z0-9]{2}) ([a-za-z0-9]{2}) #", $this-BackgroundColor, $COLORARR); Separates color values into three groups of 16-bit binary numbers $Color = imagecolorallocate ($this-_img,hexdec ($COLORARR [1]), Hexdec ($COLORARR [2]), Hexdec ($ COLORARR[3])); Add a background color to the IMG image source Imagefilledrectangle ($this, _img,0, $this, Height, $this, width,0, $Color);    Create Image}}/** * Create 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)]; }}/** * Writes a string to the image and does not support Chinese/protected function _write_text () {$FontWidth = Imagefontwidth ($this fontsiz e); Gets the width of a character of font size Preg_match_all ('/(.) /us ", $this, Content, $TEXTARR); Divide content by the number of statistics $FontHeight = Imagefontheight ($this-FontSize); Gets the height of the font size $X = Ceil (($this Width-($FontWidth * COUNT ($TextArr [0])))/2); Set the distance from the left margin of the x wheelbase $Y = Ceil ($this, Height + $FontHeight)/ 2);    Sets the distance from the top margin of the Y wheelbase 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 text color Imagettftext ($this-_img, $this-fontsize,0, $X, $Y, $Color, __webroot__. Font, $this-- tent); Write content}/** * Write verification code to image */protected function _write_code () {$_x = $this width/$this-Codes;//settings Aspect ratio for ($i = 0; $i < $this, Codes; $i + +) {//Loop Codes times, generate one verification code value at a time $Color = Imagecolorallocate ($this-_img , Mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156)); Randomly generated verification code value color Imagettftext ($this, _img, $this, Fontsize,mt_rand ( -30,30), $_x* $i +mt_rand (1,5), $this-Heig ht/1.3, $Color, __webroot__ $this, Font, $this-_code[$i]);  Generate a verification code value}}/** * Write interference lines into images */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 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 CAPTCHA */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 ();  /** * External obtain MD5 encrypted CAPTCHA * @return String */Public Function get_verify () {return MD5 ($this-_code); }/** * Loads an image file and obtains image related information * @param string $FilePath image relative path address * @return $this |bool successfully returned the object, otherwise false */public F    Unction 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);      Case 2: $this->_fileimg = Imagecreatefromjpeg ($FilePath); Case 3: $this->_fileimg = Imagecreatefrompng ($FilePath);; Default: $this-loaderr = "type error, unsupported picture type";    Return False;  } Return True; }/** * Create thumbnail * @param string $FileName saved picture name prefix * @param string $FilePath Save picture relative path * @return Mixed returns information about the generated picture    Array */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);      Case 2:imagejpeg ($this, _img, $SavePath);    Case 3:imagepng ($this, _img, $SavePath);    } $FIleInfo [' FileName '] = $FileName; $FIleinfo[' FilePath '] = $FilePath;  Return $FIleInfo; }}

Using the example

$IMG = new IMG (); $Options [' Width '] = +, $Options [' Height '] = +, $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  ("Load image failed,". $IMG-Loaderr);}
Reprinted from: http://www.aspnetjia.com

http://www.bkjia.com/PHPjc/1094588.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094588.html techarticle PHP image processing library and demo sharing, PHP image processing library simply wrote a PHP image processing class library, although the function is relatively small, but at present also useless to too advanced, later used ...

  • Related Article

    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.