This article mainly introduces PHP implementation of the general image processing class, can be achieved for the image scaling, cutting, photo frame, watermark, sharpening, rotation, flip, transparency, anti-color and other functions, with a certain reference value, the need for friends can refer to the next
This article describes the common image processing class implemented by PHP. Share to everyone for your reference. Specific as follows:
The image processing function features: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color, process and save the history of the idea: when there are changes in the picture to automatically generate a new picture, naming method can be considered in the original image based on the addition of steps, such as: Picture name +__ The first few steps. You can refer to this class in some Web applications that require image advanced processing capabilities.
<?phpclass picture{var $PICTURE _url;//to process the picture var $DEST _url= "temp__01.jpg";//Generate target picture position var $PICTURE _create;//picture to create 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;//text Horizontal axis var $WORD _y;//text ordinate var $FONT _type;//font type var $FONT _size= "12";//FONT size VA R $FONT _word;//text var $ANGLE the angle of =0;//text, default to 0 var $FONT _color= "#000000";//Text color var $FONT _path= "Font/simkai.ttf";//Font library, The default is the Arial 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 horizontal var $FORC e_start_y=0;//cut up the image of the watermark ordinate var $PICTURE _type;//picture type var $PICTURE _mime;//output Header/* Scale to 1 then zoom/var $ZOOM =1;// Scale type var $ZOOM _multiple;//scale var $ZOOM _width;//scaling var $ZOOM _height;//zoom Height/* trim, proportional and fixed length, Width */var $CUT _type=1;//crop Type var $CUT _x=0;//cut the horizontal axis var $CUT _y=0;//crop the ordinate var $CUT _;//cut the width var $CUT _height=100;//cut the height/* Sharpening */var $SHARP = "7.0";//Sharp Degree/* Transparency processing */var $ALPHA = ' 100 ';//TransparentDegrees between 0-127 var $ALPHA _x= "90"; var $ALPHA _y= "50";/* rotation at any angle */var $CIRCUMROTATE = "90.0";//Note that you must have a floating-point number/* error message */var $ERROR =array (' unalviable ' = ') did not find the relevant Picture! ' );/* Constructor: Function initialization */functions __construct ($PICTURE _url) {$this->get_info ($PICTURE _url); } function Get_info ($PICTURE _url) {/* processing the information of the original picture, first detect whether the picture exists, does not exist to give the corresponding information */@ $SIZE =getimagesize ($PICTURE _url); if (! $SIZE) {exit ($this->error[' unalviable '); }//Get the original picture information type, width, height $this->picture_mime= $SIZE [' MIME ']; $this->picture_; $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/* Convert 16 binary colors to 10 binary (r,g,b) */function Hex2dec () {Preg_match_all ("/([0-f]) {2,2}/i", $th Is->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]); }}//scale 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 () {//scaled size if ($this->zoom==0) {$this->zoom_;gt; 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/* place text or picture watermark text on picture */function _mark_text () {$this->true_color=imagecreatetruecolor ($this-> ; Picture_width, $this->picture_height); $this->word=mb_convert_encoding ($this->font_word, ' utf-8 ', ' gb2312 '); /* Get used TrueThe range of text for type font */$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]; /* The default position for the text watermark 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, $TEXT 2 , $this->font_path, $this->word); }/* Watermark picture */function _mark_picture () {/* Get the watermark picture information */@ $SIZE =getimagesize ($this->force_url); if (! $SIZE) {exit ($this->error[' unalviable '); } $FORCE _picture_; $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, the image size is the size of the 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 _;gt; force_start_x; } else {$CREATE _;gt; Picture_width; if ($FORCE _picture_height> $this->picture_height) {$CREATE _height= $FORCE _picture_height-$this->force_ 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 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 Markfunction 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 = i Magecolorat ($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->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, $t his->picture_height-$y-1, 0, $y, $this->picture_width, 1); }}/* any angle rotation */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);} /* Picture Sharpening */function Sharp () {$this->true_color=imagecreatetruecolor ($this->picture_width, $this->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 ($this->p Icture_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); }}}/* Color-reversed the image processing?? */function Return_color () {/* Create a canvas */$NEW _picture_create=imagecreate ($this->picture_width, $this->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 the target picture and show */function Show () {//Judging 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 () {//Export the image to a browser or file in JPEG format $OUT = $this->picture_type; if (function_exists ($OUT)) {//Judgment 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"); }} 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}?>
Summary : The above is the entire content of this article, I hope to be able to help you learn.