Powerful PHP Image processing class (watermark, transparency, rotation), _php tutorial

Source: Internet
Author: User
Tags imagecopy

Powerful PHP Image processing class (watermark, transparency, rotation),


Very powerful PHP image processing class, you can customize the image watermark, transparency, image scaling, image sharpening, picture rotation, picture flipping, picture cutting, picture inverse color.

* Image processing functions: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color
* Process and save the history of ideas: when there are changes to the image automatically generated a new picture, naming method can be considered in the original image based on the addition of steps, for example: Picture name +__ First Steps

The specific code is as follows:

<?php class picture{var $PICTURE _url;//The picture to be processed var $DEST _url = "temp__01.jpg";//Generate target picture position var $PICTURE _create; To create the picture var $TURE _color; Create a new True color image var $PICTURE _width; Original image width var $PICTURE _height;    Original image Height/** * Watermark type, default to watermark text */var $MARK _type = 1; var $WORD; After UTF-8 the text var $WORD _x; The text horizontal axis var $WORD _y; Text ordinate var $FONT _type; Font type var $FONT _size = "12"; Font size var $FONT _word; literal var $ANGLE = 0; The text angle, the default is 0 var $FONT _color = "#000000"; Text color var $FONT _path = "Font/simkai.ttf"; Font library, the default is the song body var $FORCE _url; Watermark picture var $FORCE _x = 0; Watermark Horizontal var $FORCE _y = 0; Watermark ordinate var $FORCE _start_x = 0; Cut the watermark picture of the horizontal var $FORCE _start_y = 0; The picture of the watermark is cut from the ordinate var $PICTURE _type; Picture type var $PICTURE _mime; The output of the head/** * Zoom ratio of 1 is scaled by the zoom height and Width */var $ZOOM = 1; Scale type var $ZOOM _multiple; scale Var $ZOOM _width; The scale of the relaxation var $ZOOM _height; Scale height/** * crop, proportional and fixed length, Width */var $CUT _type = 1; Cut type var $CUT _x = 0; Cutting the horizontal axis var $CUT _y = 0; The trimmed ordinate var $CUT _width = 100; Cutting width var $CUT _height = 100; Cutting height/** * Sharpening * * var $SHARP = "7.0"; Sharpening degree/** * Transparency processing */var $ALPHA = ' 100 ';    Transparency between 0-127 var $ALPHA _x = "90";     var $ALPHA _y = "50"; /** * Rotate at any angle */var $CIRCUMROTATE = "90.0";     Note that the floating-point number must be/** * * error message */var $ERROR = array (' unalviable ' = ' does not find the relevant picture! ')     );          /** * Constructor: function initialization */functions __construct ($PICTURE _url) {$this-get_info ($PICTURE _url); } function Get_info ($PICTURE _url) {/** * handles the information of the original picture, first detects if the picture exists, does not exist, and gives the corresponding information */@ $SIZE = getimagesize ($PI      Cture_url);        if (! $SIZE) {exit ($this-error[' unalviable ');      }//Get the original picture information type, width, height $this-picture_mime = $SIZE [' MIME '];      $this-picture_width = $SIZE [0];          $this-Picture_height = $SIZE [1]; Create picture switch ($SIZE [2]) {CAsE 1: $this-picture_create = Imagecreatefromgif ($PICTURE _url);        $this-Picture_type = "imagejpeg";        $this-picture_ext = "JPG";      Break        Case 2: $this, picture_create = Imagecreatefromjpeg ($PICTURE _url);        $this-Picture_type = "Imagegif";        $this-picture_ext = "gif";      Break        Case 3: $this, picture_create = Imagecreatefrompng ($PICTURE _url);        $this-Picture_type = "Imagepng";        $this-picture_ext = "png";        Break      }/** * Text color conversion 16 binary converted to 10 */Preg_match_all ("/([0-f]) {2,2}/i", $this-Font_color, $MATCHES);      if (count ($MATCHES) = = 3) {$this, RED = Hexdec ($MATCHES [0][0]);      $this-GREEN = Hexdec ($MATCHES [0][1]);      $this BLUE = Hexdec ($MATCHES [0][2]); }} # End of __construct/** * Converts 16 colors into 10 binary (r,g,b) */function Hex2dec () {Preg_match_all ("/([0-f]) {2,2}/i", Font_color, $this, $MAtches);      if (count ($MATCHES) = = 3) {$this, RED = Hexdec ($MATCHES [0][0]);      $this-GREEN = Hexdec ($MATCHES [0][1]);      $this BLUE = Hexdec ($MATCHES [0][2]);    }}//zoom type function Zoom_type ($ZOOM _type) {$this, zoom = $ZOOM _type; }//Scale the picture if you do not specify a height and width to zoom function zoom () {//Zoom size if ($this-zoom = 0) {$this-zoom_width = $th      IS-picture_width * $this-zoom_multiple;      $this-zoom_height = $this, Picture_height * $this, zoom_multiple;    }//Create a new True color image $this-True_color = Imagecreatetruecolor ($this, Zoom_width, $this-zoom_height);    $WHITE = Imagecolorallocate ($this, True_color, 255, 255, 255);    Imagefilledrectangle ($this, true_color, 0, 0, $this-zoom_width, $this-Zoom_height, $WHITE); Imagecopyresized ($this, True_color, $this-picture_create, 0, 0, 0, 0, $this-zoom_width, $this-zoom_ HEIGHT, $this-Picture_width, $thiS--picture_height); } # End of zoom//trim picture, by coordinates or auto function cut () {$this-true_color = Imagecreatetruecolor ($this, Cut_width, $    this, cut_width); Imagecopy ($this-True_color, $this-picture_create, 0, 0, $this-cut_x, $this-cut_y, $this-Cut_    WIDTH, $this-cut_height); } # End of Cut/** * put text or pictures on the picture * watermark Text */function _mark_text () {$this, True_color = Imagecreatetruecolor ($th    IS--picture_width, $this-picture_height);   $this WORD = mb_convert_encoding ($this, Font_word, ' utf-8 ', ' gb2312 ');  /** * Get the range of text using TrueType fonts */$TEMP = Imagettfbbox ($this, font_size, 0, $this, Font_path, $this-    WORD);    $WORD _length = strlen ($this, WORD);    $WORD _width = $TEMP [2]-$TEMP [6];   $WORD _height = $TEMP [3]-$TEMP [7]; /** * Text watermark default location is lower right corner */if ($this, word_x = = "") {$this, word_x = $this, picture_width-$WORD _      WIDTH; } if ($this->      Word_y = = "") {$this, word_y = $this, picture_height-$WORD _height;    } imagesettile ($this, True_color, $this-picture_create); Imagefilledrectangle ($this, true_color, 0, 0, $this, Picture_width, $this, Picture_height, img_color_tiled    );    $TEXT 2 = imagecolorallocate ($this, True_color, $this, RED, $this-GREEN, $this-Blue); Imagettftext ($this-True_color, $this-Font_size, $this-ANGLE, $this-word_x, $this-word_y, $TE    XT2, $this, Font_path, $this, WORD); /** * Watermark Picture */function _mark_picture () {/** * get information about watermark Picture */@ $SIZE = getimagesize ($this-Force_url)    ;      if (! $SIZE) {exit ($this-error[' unalviable ');    } $FORCE _picture_width = $SIZE [0];    $FORCE _picture_height = $SIZE [1];      Create watermark Picture switch ($SIZE [2]) {Case 1: $FORCE _picture_create = Imagecreatefromgif ($this, Force_url);      $FORCE _picture_type = "gif";  Break  Case 2: $FORCE _picture_create = Imagecreatefromjpeg ($this, Force_url);      $FORCE _picture_type = "jpg";    Break      Case 3: $FORCE _picture_create = Imagecreatefrompng ($this, Force_url);      $FORCE _picture_type = "png";      Break }/** * Determine the size of the watermark image and generate the size of the target image, if the watermark is larger than the picture, then the image size is the size of the watermark picture.    Otherwise, the resulting picture size is the original picture size.    */$this New_picture = picture_create, $this; if ($FORCE _picture_width > $this, picture_width) {$CREATE _width = $FORCE _picture_width-$this, Force_start    _x;    }else{$CREATE _width = $this, picture_width; if ($FORCE _picture_height > $this, picture_height) {$CREATE _height = $FORCE _picture_height-$this, Forc    e_start_y;    }else{$CREATE _height = $this, picture_height;  }/** * Create a canvas */$NEW _picture_create = Imagecreatetruecolor ($CREATE _width, $CREATE _height); $WHITE = Imagecolorallocate ($NEW _picture_create, 255, 255, 255); /** * Copy the background map to the canvas */imagecopy ($NEW _picture_create,$this, picture_create, 0, 0, 0, 0, $this, Picture_width, $this, picture_height); /** * Copy the target image to the background image */imagecopy ($NEW _picture_create, $FORCE _picture_create, $this-force_x, $this-force_y,  $this-force_start_x, $this-force_start_y, $FORCE _picture_width, $FORCE _picture_height);  $this-true_color = $NEW _picture_create; } # End of Mark function Alpha_ () {$this-true_color = Imagecreatetruecolor ($this-Picture_width, $this-  Picture_height);  $rgb = "#CDCDCD";  $tran _color = "#000000";      for ($j = 0; $j <= $this-picture_height-1; $j + +) {for ($i = 0; $i <= $this, picture_width-1; $i + +) {      $rgb = Imagecolorat ($this, Picture_create, $i, $j);      $r = ($rgb >>) & 0xFF;      $g = ($rgb >> 8) & 0xFF;      $b = $rgb & 0xFF;      $now _color = imagecolorallocate ($this, Picture_create, $r, $g, $b);        if ($now _color = = $tran _color) {continue;      } else {$color = Imagecolorallocatealpha ($this, Picture_create, $r, $g, $b, $ALPHA);        Imagesetpixel ($this-picture_create, $ALPHA _x + $i, $ALPHA _y + $j, $color);          } $this True_color = $this, picture_create; }}}/** * Picture rotation: * Rotate along Y-axis */function turn_y () {$this, True_color = Imagecreatetruecolor ($this pictur  E_width, $this-picture_height); for ($x = 0; $x < $this, picture_width; $x + +) {imagecopy ($this, True_color, $this, Picture_create, $    This, picture_width-$x-1, 0, $x, 0, 1, $this-picture_height); }}/** * Rotates along the x axis */function turn_x () {$this, True_color = Imagecreatetruecolor ($this, Picture_width, $thi    S--picture_height);  for ($y = 0; $y < $this, picture_height; $y + +) {imagecopy ($this, True_color, $this, Picture_create,    0, $this-picture_height-$y-1, 0, $y, $this-picture_width, 1); }}/** * Rotate at any angle */FUNction Turn () {$this, True_color = Imagecreatetruecolor ($this, Picture_width, $this-picture_height); Imagecopyresized ($this, True_color, $this-picture_create, 0, 0, 0, 0, $this-picture_width, $this-    Picture_height, $this-Picture_width, $this-picture_height);    $WHITE = Imagecolorallocate ($this, True_color, 255, 255, 255);  $this-True_color = Imagerotate ($this, True_color, $this, Circumrotate, $WHITE); }/** * Image Sharpening * * Function sharp () {$this-true_color = Imagecreatetruecolor ($this, picture_width, $this-&G T    Picture_height);    $cnt = 0;        for ($x = 0; $x < $this, picture_width; $x + +) {for ($y = 0; $y < $this, picture_height; $y + +) {        $src _CLR1 = Imagecolorsforindex ($this, True_color, Imagecolorat ($this, Picture_create, $x-1, $y-1));        $src _CLR2 = Imagecolorsforindex ($this, True_color, Imagecolorat ($this, Picture_create, $x, $y));$r = intval ($src _clr2["Red"] + $this, sharp * ($SRC _clr2["Red")-$src _clr1["Red"]));        $g = intval ($src _clr2["green" + $this, Sharp * ($SRC _clr2["green")-$src _clr1["green"]);        $b = intval ($src _clr2["Blue" + $this, Sharp * ($SRC _clr2["Blue")-$src _clr1["Blue"]));        $r = min (255, max ($r, 0));        $g = min (255, max ($g, 0));        $b = min (255, max ($b, 0)); if ($DST _clr = Imagecolorexact ($this, Picture_create, $r, $g, $b)) = =-1) $DST _clr = Imagecolorallocate ($th        IS--picture_create, $r, $g, $b);        $cnt + +;        if ($DST _clr = =-1) die ("Color allocate faile at $x, $y ($cnt).");     Imagesetpixel ($this, True_color, $x, $y, $DST _clr);  }}}/** * Reverse color image processing?? */function Return_color () {/** * Create a canvas */$NEW _picture_create = Imagecreate ($this, Picture_width, $this-P  Icture_height); $WHITE = Imagecolorallocate ($NEW _picture_create, 255, 255, 255); /** * Copy the background map to the canvas */imagecopy ($NEW _picture_create, $this-picture_create, 0, 0, 0, 0, $this-picture_width, $this-picture_height);  $this-true_color = $NEW _picture_create; }/** * Generate the target picture and display the */function show () {//Judgment browser, if IE does not send header if (isset ($_server[' http_user_agent ')) {$ua = Strtoupp    ER ($_server[' http_user_agent ');      if (!preg_match ('/^.*msie.*\) $/i ', $ua)) {header ("Content-type: $this->picture_mime");  }} $OUT = $this, picture_type;  $OUT ($this, True_color);  /** * Generate the target picture and save */function Save_picture () {//in JPEG format to output the image to a browser or file $OUT = $this, picture_type; if (function_exists ($OUT)) {//Judgment browser, if IE does not send header if (isset ($_server[' http_user_agent ')) {$ua = Strtoupper ($_se      rver[' http_user_agent ');        if (!preg_match ('/^.*msie.*\) $/i ', $ua)) {header ("Content-type: $this->picture_mime");      }} if (! $this-True_color) {exit ($this, error[' unavilable '); }else{$OUT ($this, True_color, $thisDest_url);      $OUT ($this, True_color);  }}}/** * destructor: Release image */function __destruct () {/** * release picture */Imagedestroy ($this, True_color);  Imagedestroy ($this-picture_create);  } # End of Class}?>

This is very powerful PHP image processing class, good collection, pro, I believe it will come in handy.

http://www.bkjia.com/PHPjc/1063224.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063224.html techarticle Powerful PHP Image processing class (watermark, transparency, rotation), very powerful PHP image processing class, you can customize the image watermark, transparency, image scaling, image sharpening, picture rotation ...

  • 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.