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

Source: Internet
Author: User
Tags imagecopy php class

A very powerful PHP image processing class, you can customize the image watermark, transparency, image scaling, image sharpening, picture rotation, picture rollover, picture cut, the image color.

* Image processing function: Scaling, shearing, photo frame, watermark, sharpening, rotation, flip, transparency, anti-color
* Process and save the history of the idea: when there are changes in the picture automatically generated a new picture, named Way can be considered in the original picture based on the steps, such as: Picture name +__ The first few steps

The specific code is as follows:

<?php class picture{var $PICTURE _url;//To process the picture var $DEST _url = "temp__01.jpg";//Generate target picture location var $PICTURE _cr Eate; To create a picture var $TURE _color; Create a new True color image var $PICTURE _width; Original picture width var $PICTURE _height; 
   Original picture Height/** * Watermark type, default for watermark text * * var $MARK _type = 1; var $WORD; After UTF-8 the text var $WORD _x; Text horizontal axis var $WORD _y; Text ordinate var $FONT _type; Font type var $FONT _size = "12"; Font size var $FONT _word; Text var $ANGLE = 0; Text angle, 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 axis var $FORCE _y = 0; Watermark ordinate var $FORCE _start_x = 0; Cut watermark Picture Horizontal axis var $FORCE _start_y = 0; Cut the watermark image ordinate var $PICTURE _type; Picture type var $PICTURE _mime; The output of the head/** * scaling ratio of 1 to zoom height and width zoom/var $ZOOM = 1; Scale type var $ZOOM _multiple; Zoom ratio var $ZOOM _width; Reduced relaxation degree Var $ZOOM _height; 
  
  
  Zoom height/** * Cut, proportional and fixed length, Width * * * var $CUT _type = 1; Cut type var $CUT _x = 0; Cut horizontal axis var $CUT _y = 0; Trimmed ordinate var $CUT _width = 100; Cut 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"; /** * Arbitrary angle rotation * * var $CIRCUMROTATE = "90.0"; 
    Note that you must/** * error message for floating-point number */var $ERROR = array (' unalviable ' => ' did not find the relevant picture! ') 
  
  ); 
    
     /** * Constructor: Functions initialization/function __construct ($PICTURE _url) {$this-> get_info ($PICTURE _url); The function get_info ($PICTURE _url) {/** * processing the information of the original picture, first detects whether the picture exists, does not exist then gives the corresponding information * * $SIZE = Getima 
     Gesize ($PICTURE _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 a 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 into 10/Preg_match_all ("/[0-f]) {2,2}/i", $this-> Font_color, $MATCHE 
     S); 
     if (count ($MATCHES) = = 3) {$this-> RED = Hexdec ($MATCHES [0][0]); 
     $this-> GREEN = Hexdec ($MATCHES [0][1]); 
     $this-> BLUE = Hexdec ($MATCHES [0][2]); }} # EndThe __construct/** * Converts the color of 16 into a 10-in (r,g,b)/function Hex2dec () {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]); 
   }//Zoom type function Zoom_type ($ZOOM _type) {$this-> zoom = $ZOOM _type;  //Zoom the picture, if you do not specify the height and width to scale the function zoom () {//scaled size if ($this-> zoom = 0) {$this-> zoom_width 
     = $this-> 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-> ZOO 
   M_height, $this-> picture_width, $this-> picture_height); # End of zoom//trim picture, by coordinates or auto function cut () {$this-> True_color = Imagecreatetruecolor ($this-> Cut_wid 
   TH, $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 picture on the picture * watermark text/function _mark_text () {$this-> true_color = Imagecreatetruec 
   Olor ($this-> 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-& Gt 
   WORD); 
   $WORD _length = strlen ($this-> WORD); 
   $WORD _width = $TEMP [2]-$TEMP [6]; $WORd_height = $TEMP [3]-$TEMP [7]; /** * The default location for the text watermark is the lower right corner */if ($this-> word_x = "") {$this-> word_x = $this-> picture_width-$W 
     Ord_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 on watermark picture * * @ $SIZE = getimagesize ($this-> 
   Force_url); 
     if (! $SIZE) {exit ($this-> error[' unalviable ')); } $FORCE _picture_widTH = $SIZE [0]; 
   $FORCE _picture_height = $SIZE [1]; 
     Create a 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 /** * Determines the size of the watermark picture, and generates the size of the target picture, if the watermark is larger than the picture, the size of the picture is generated as a watermark picture. 
   Otherwise, the resulting picture size is the original picture size. 
   * * $this-> new_picture = $this-> picture_create; if ($FORCE _picture_width > $this-> picture_width) {$CREATE _width = $FORCE _picture_width-$this-> Force_star 
   t_x; 
   }else{$CREATE _width = $this-> picture_width; } if ($FORCE _picture_height > $this-> picture_height) {$CREATE _height = $FORCE _picture_height-$this-> FO 
   rce_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 picture to the background picture * * * 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-&G T 
 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 the y axis/function turn_y () {$this-> True_color = Imagecreatetruecolor ($this-&G T 
 Picture_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); }/** * Rotate along the x-axis/function turn_x () {$this-> true_color = ImagecReatetruecolor ($this-> picture_width, $this-> 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); }/** * Arbitrary angle rotation/function turn () {$this-> True_color = Imagecreatetruecolor ($this-> Picture_widt 
   H, $this-> picture_height); Imagecopyresized ($this-> true_color, $this-> picture_create, 0, 0, 0, 0, $this-> picture_width, $this-> PI 
   Cture_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); /** * Picture sharpening/Function sharp () {$this-> True_color = Imagecreatetruecolor ($this-> picture_width, $thi 
   s-> picture_height); 
   $cnt = 0; for ($x = 0; $x < $this-&Gt Picture_width; $x + +) {for ($y = 0; $y < $this-> picture_height $y + +) {$src _CLR1 = Imagecolorsforindex ($this-> T 
       Rue_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 ($t 
       His-> 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); 
 }}/** * To reverse the image processing?? /function Return_color () {/** * Create a canvas * * * * $NEW _picture_create = imagecreate ($this-> picture_width, $this-& Gt 
 Picture_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 target picture and display/function show () {//Judge browser, if IE does not send header if (isset ($_server[' http_user_agent ')) {$ua = 
   Strtoupper ($_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 () {//output the image to the browser or file in JPEG format $OUT = $this-> picture_type; if (Function_exists ($OUT) {//Judge browser, if IE does not send header if (isset ($_server[' http_user_agent ')) {$ua = Strtoupper ($_server[' Http_user_ag ') 
     ENT ']); 
       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, $this-> dest_url); 
     $OUT ($this-> true_color); 
 }}/** * destructor: Release picture/function __destruct () {/** * release picture */Imagedestroy ($this-> true_color); 
 Imagedestroy ($this-> picture_create);  } # End of Class}?>

This is a very powerful PHP image processing class, a good collection, pro, I believe that the future will come in handy.

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.