PHP common image processing class,
Types= $types; $this->image= $this->imagesources ($imageaddress); $this->width= $this->imagesizex (); $this->height= $this->imagesizey (); $this->value1= $value 1; $this->value2= $value 2; $this->endaddress= $endaddress; } function Outimage () {///depending on the type value passed in, output a different function switch ($this->types) {Case 1: $this->scaling (); break; case 2: $this- >clipping (); Break Case 3: $this->imagewater (); Break Default:return false; }} Private Function Imagewater () {//http://www.hzhuti.com plus picture watermark function//Use function to get the length and width of the watermark file $imagearrs = $this->getimagearr ($ THIS->VALUE1); The calling function calculates where the watermark is loaded $positionarr = $this->position ($this->value2, $imagearrs [0], $imagearrs [1]); Watermark Imagecopy ($this->image, $this->imagesources ($this->value1), $positionarr [0], $positionarr [1], 0, 0, $ Imagearrs[0], $imagearrs [1]); Call the output method to save the $this->output ($this->image); The private Function clipping () {///Picture clipping function//assigns the passed in values to the variable list ($src _x, $src _y) =explode (",", $this->value1); list ($dst _w , $DST _h) =explOde (",", $this->value2); if ($this->width < $src _x+ $DST _w | | $this->height < $SRC _y+ $dst _h) {//This judgment is to limit not to intercept the image outside to return false;}//Create New The canvas resource $newimg =imagecreatetruecolor ($dst _w, $dst _h); Cropping imagecopyresampled ($newimg, $this->image, 0, 0, $src _x, $src _y, $dst _w, $dst _h, $dst _w, $dst _h); Call the output method to save the $this->output ($newimg); } Private Function Scaling () {//Picture zoom function//Get equal width and height $this-proimagesize ()///////////////////////////////////////// Output ($this->imagescaling ()); The Private Function imagesources ($imgad) {//Gets the picture type and opens the image resource $imagearray = $this->getimagearr ($imgad); switch ($ Imagearray[2]) {case 1://gif $this->imgtype=1; $img =imagecreatefromgif ($imgad), break, Case 2://jpeg $this imgtype=2; $img =imagecreatefromjpeg ($imgad); Break Case 3://png $this->imgtype=3; $img =imagecreatefrompng ($imgad); Break Default:return false; } return $img; } Private Function Imagesizex () {//Get picture width return imagesx ($this->image);} Private Function Imagesizey () {//Get picture height REturn Imagesy ($this->image); } Private Function Proimagesize () {//Calculate the width and height of the picture as compared to the scaled ($this->value1 && ($this->width < $this Height) {//equal to the scaling algorithm $this->value1=round (($this->value2/$this->height) * $this->width);} else{$this->value2=round (($this->value1/$this->width) * $this->height);}} Private Function imagescaling () {//Image scaling function, returns the processed image resource $newimg =imagecreatetruecolor ($this->value1, $this value2); $tran =imagecolortransparent ($this->image);//process transparent algorithm if ($tran >= 0 && $tran < imagecolorstotal ($this- >image) {$tranarr =imagecolorsforindex ($this->image, $tran) $newcolor =imagecolorallocate ($newimg, $tranarr [ ' Red '], $tranarr [' Green '], $tranarr [' Blue ']; Imagefill ($newimg, 0, 0, $newcolor); Imagecolortransparent ($newimg, $newcolor); } imagecopyresampled ($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this-& Gt;height); return $newimg; } Private Function output ($image) {//Output image SWITCH ($this->imgtype) {case 1:imagegif ($image, $this->endaddress); the case 2:imagejpeg ($image, $this endaddress); Break Case 3:imagepng ($image, $this->endaddress); Break Default:return false; }} Private Function Getimagearr ($imagesou) {//Return image attribute array method return getimagesize ($imagesou);} Private function Position ($ Num, $width, $height) {//returns the coordinates of a position based on the number passed in, $width and $height represent the width and height switch ($num) {Case 1: $positionarr [0]=0; $] of the inserted image positionarr[1]=0; Break Case 2: $positionarr [0]= ($this->width-$width)/2; $positionarr [1]=0; Break Case 3: $positionarr [0]= $this->width-$width; $positionarr [1]=0; Break Case 4: $positionarr [0]=0; $positionarr [1]= ($this->height-$height)/2; Break Case 5: $positionarr [0]= ($this->width-$width)/2; $positionarr [1]= ($this->height-$height)/2; Break Case 6: $positionarr [0]= $this->width-$width; $positionarr [1]= ($this->height-$height)/2; Break Case 7: $positionarr [0]=0; $positionarr [1]= $this->height-$height; Break Case 8: $positionarr[0]= ($this->width-$width)/2; $positionarr [1]= $this->height-$height; Break Case 9: $positionarr [0]= $this->width-$width; $positionarr [1]= $this->height-$height; Break Case 0: $positionarr [0]=rand (0, $this->width-$width); $positionarr [1]=rand (0, $this->height-$height); Break } return $positionarr; } function __destruct () {Imagedestroy ($this->image);}} ?>
http://www.bkjia.com/PHPjc/1109847.html www.bkjia.com true http://www.bkjia.com/PHPjc/1109847.html techarticle PHP Common image processing class, PHP/* Known issue: 1. In the picture zoom function, use the Imagecreatetruecolor function to create the canvas and use the transparent processing algorithm, but the PNG format picture cannot ...