PHP Image clipping cutting class source code and how to use

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color
Recently in the Web page drag verification code of open source projects, need to generate images on the server of the corresponding movable color blocks, but the resources on the Internet are thumbnails, the entire picture is scaled, so do it yourself, finished the tool to crop small pieces of the picture

<?phpnamespace app\libs;/*** 2016-01-07 15:54:58* lixiaoyu* * Mode 1: Force cropping, generate images in strict accordance with needs, not enlarge, over cropping, picture always covered * Mode 2: and 1 similar, But not when the General Assembly produces filler, can be removed with PNG. * Mode 3: Only zoom, do not crop, retain all picture information, will produce filler, * Mode 4: Only scale, not crop, keep all picture information, generate picture size for final scaled picture valid information of the actual size, do not produce filler * default filler is white, if you want to make the filter into transparent pixels, Please use the Savealpha () method instead of the SaveImage () method */class Imagecrop{var $sImage, var $dImage, var $src _file;var $dst _file;var $src _width; var $src _height;var $src _ext;var $src _type;function __construct ($src _file, $dst _file= ") {$this->src_file= $src _ File, $this->dst_file= $dst _file; $this->loadimage ();} function Setsrcfile ($src _file) {$this->src_file= $src _file;} function Setdstfile ($dst _file) {$this->dst_file= $dst _file;} function LoadImage () {list ($this->src_width, $this->src_height, $this->src_type) = getimagesize ($this src_file), switch ($this->src_type) {case imagetype_jpeg: $this->simage=imagecreatefromjpeg ($this->src_ file); $this->ext= ' jpg '; break;case imagetype_png: $this->simage=imagecreatefrompng ($this->src_file); $this->ext= ' png '; break;case imagetype_gif: $this->simage=imagecreatefromgif ($this->src_file); $this- >ext= ' gif '; Break;default:exit ();}} function SaveImage ($fileName = ") {$this->dst_file= $fileName? $fileName: $this->dst_file;switch ($this->src _type) {case Imagetype_jpeg:imagejpeg ($this->dimage, $this->dst_file,100); Break;case Imagetype_png:imagepng ( $this->dimage, $this->dst_file); break;case imagetype_gif:imagegif ($this->dimage, $this->dst_file); Break;default:break;}} function outimage () {switch ($this->src_type) {case Imagetype_jpeg:header (' content-type:image/jpeg '); Imagejpeg ($ this->dimage); break;case imagetype_png:header (' content-type:image/png '); Imagepng ($this->dimage); break; Case Imagetype_gif:header (' content-type:image/gif '); Imagegif ($this->dimage); break;default:break;}} function Savealpha ($fileName = ") {$this->dst_file= $fileName? $fileName. '. png ': $this->dst_file '. PNG '; Imagesavealpha ($this->dimage, True); Imagepng ($this->dimage, $this->dst_file);} function Outalpha () {Imagesavealpha ($this->dimage, True); header (' content-type:image/png '); Imagepng ($this, DIMAGE);} function Destory () {Imagedestroy ($this->simage); Imagedestroy ($this->dimage);} function Crop ($dst _width, $dst _height, $mode =1, $dst _file= ") {if ($dst _file) $this->dst_file= $dst _file; $this DIMAGE = Imagecreatetruecolor ($dst _width, $dst _height); $bg = Imagecolorallocatealpha ($this->dimage, 255,255,255,127); Imagefill ($this->dimage, 0, 0, $BG) imagecolortransparent ($this->dimage, $BG); $ratio _w=1.0 * $DST _width/$this->src_width; $ratio _h=1.0 * $dst _height/$this->src_height; $ratio =1.0;switch ($mode) {Case 1:// Always Cropif (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1)) {$ratio = $ratio _w < $ratio _h $ratio _h: $ratio _w; $tmp _w = (int) ($DST _width/$ratio); $tmp _h = (int) ($DST _height/$r Atio); $tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h); $src _x = (int) ($this->src_width-$tmp _w)/2); $src _y = (int) (($this->src_height-$tmp _h)/2) imagecopy ($tmp _img, $this->simage, 0,0, $src _x,$ Src_y, $tmp _w, $tmp _h) imagecopyresampled ($this->dimage, $tmp _img,0,0,0,0, $dst _width, $dst _height, $tmp _w, $tmp _h ); Imagedestroy ($tmp _img);} else{$ratio = $ratio _w < $ratio _h $ratio _h: $ratio _w; $tmp _w = (int) ($this->src_width * $ratio); $tmp _h = (int) ($th Is->src_height * $ratio); $tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h); imagecopyresampled ($tmp _img, $this simage,0,0,0,0, $tmp _w, $tmp _h, $this->src_width, $this->src_height); $src _x = (int) ($tmp _w-$dst _width)/2; $src _ y = (int) ($tmp _h-$dst _height)/2; Imagecopy ($this->dimage, $tmp _img, 0,0, $src _x, $src _y, $dst _width, $dst _height); Imagedestroy ($tmp _img);} Break;case 2://Only Smallif ($ratio _w < 1 && $ratio _h < 1) {$ratio = $ratio _w < $ratio _h? $ratio _h: $ra tio_w; $tmp _w = (int) ($DST _width/$ratio) $tmp _h = (int) ($DST _height/$ratio); $tmp _img=imagecreatetruecolor ($tmp _w, $ TMP_H); $src _x =(int) ($this->src_width-$tmp _w)/2; $src _y = (int) ($this->src_height-$tmp _h)/2 imagecopy ($tmp _img, $this->simage , 0,0, $src _x, $src _y, $tmp _w, $tmp _h); imagecopyresampled ($this->dimage, $tmp _img,0,0,0,0, $dst _width, $dst _height, $tmp _w, $tmp _h); Imagedestroy ($tmp _img);} ElseIf ($ratio _w > 1 && $ratio _h > 1) {$dst _x = (int) abs ($DST _width-$this->src_width)/2; $DST _y = (int ) ABS ($DST _height-$this->src_height)/2;imagecopy ($this->dimage, $this->simage, $dst _x, $dst _y,0,0, $this- >src_width, $this->src_height);} else{$src _x=0, $dst _x=0, $src _y=0, $dst _y=0;if (($dst _width-$this->src_width) < 0) {$src _x = (int) ($this->src_ width-$dst _width)/2; $dst _x = 0;} else{$src _x =0; $DST _x = (int) ($DST _width-$this->src_width)/2;} if (($dst _height-$this->src_height) < 0) {$src _y = (int) ($this->src_height-$dst _height)/2; $dst _y = 0;} else{$src _y = 0; $DST _y = (int) ($DST _height-$this->src_height)/2;} Imagecopy ($this->dimage, $this->simage, $dst _x,$dst _y, $src _x, $src _y, $this->src_width, $this->src_height);} Break;case 3://Keep all image size and create need Sizeif ($ratio _w > 1 && $ratio _h > 1) {$dst _x = (int) (ABS ($DST _width-$this->src_width)/2) $DST _y = (int) (ABS ($DST _height-$this->src_height)/2); Imagecopy ($this, DIMAGE, $this->simage, $dst _x, $dst _y,0,0, $this->src_width, $this->src_height);} else{$ratio = $ratio _w > $ratio _h $ratio _h: $ratio _w; $tmp _w = (int) ($this->src_width * $ratio); $tmp _h = (int) ($th Is->src_height * $ratio); $tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h); imagecopyresampled ($tmp _img, $this simage,0,0,0,0, $tmp _w, $tmp _h, $this->src_width, $this->src_height); $DST _x = (int) (ABS ($tmp _w-$DST _width)/2); $DST _y = (int) (ABS ($tmp _h-$DST _height)/2) imagecopy ($this->dimage, $tmp _img, $dst _x, $dst _y,0,0, $tmp _w, $tmp _h); Imagedestroy ($tmp _img);} Break;case 4://Keep all image but create actually Sizeif ($ratio _w > 1 && $ratio _h > 1) {$this->dimage = Imagecreatetruecolor ($this->src_width, $this->src_height); Imagecopy ($this->dimage, $this->simage, 0,0,0,0, $this->src_width, $this->src_height);} else{$ratio = $ratio _w > $ratio _h $ratio _h: $ratio _w; $tmp _w = (int) ($this->src_width * $ratio); $tmp _h = (int) ($th Is->src_height * $ratio); $this->dimage = Imagecreatetruecolor ($tmp _w, $tmp _h); imagecopyresampled ($this DIMAGE, $this->simage,0,0,0,0, $tmp _w, $tmp _h, $this->src_width, $this->src_height);} Break;}} End crop/**** Trim method * 2016-01-07 15:05:44* lixiaoyu** @param $dst _width target length * @param $dst _height Target high * @param $dst _x clipping part and original The distance from the left of the graph * @param $dst _y The distance from the right side of the original image * @param int $mode mode * @param string $dst _file destination file path */function Cut ($dst _width, $dst _heig HT, $DST _x, $dst _y, $dst _file= ") {if ($dst _file) $this->dst_file = $DST _file;//Set destination file location $this->dimage = Imagecreatetruecolor ($dst _width, $dst _height); The canvas that created the size of the destination file $BG = Imagecolorallocatealpha ($this->dimage, 255, 255, 255, 127); Assign a color imagefill to the canvas ($this-&Gt;dimage, 0, 0, $BG); Fill the image with Color imagecolortransparent ($this->dimage, $BG); The background is defined as transparent color $ratio_w = 1.0 * $DST _width/$this->src_width; Scaled scale $ratio_h = 1.0 * $DST _height/$this->src_height; Scale//var_dump ($this);//Do not scale, directly crop the image $ratio = 1.0; $tmp _w = (int) ($DST _width/$ratio); $tmp _h = (int) ($DST _ height/$ratio); $tmp _img = Imagecreatetruecolor ($dst _width, $dst _height); Create a temporary saved canvas imagecopy ($tmp _img, $this->simage, 0,0, $dst _x, $dst _y, $dst _width, $dst _height); Copy out part of the image and make a cut imagecopyresampled ($this->dimage, $tmp _img,0,0,0,0, $dst _width, $dst _height, $tmp _w, $tmp _h); Put the temporarily cached picture in the target file Imagedestroy ($tmp _img);}}? >

Use

Cropping an image

$ic =new Imagecrop ($pathToFile, './pic/aftercrop '. Time (). JPG '), $ic->cut (40,30,120,130), $ic->saveimage ();//$ic->savealpha (); Change the filler into transparent pixels save $ic->destory ();

Achieve results

Original


The graph after cropping

Zoom image

Original

Thumbnail images

This article focuses on using image processing functions imagecopy and imagecopyresampled

BOOL Imagecopy (resource dstim,resourcesrc_im, int dstx,intdst_y, int srcx,intsrc_y, int srcw,intsrc_h)
Starts the coordinates in the src_im image from Src_x,src_y, with a width of src_w, and a portion of the height of Src_h copied to the Dst_im and dst_x locations in the dst_y image.

The above introduces the PHP image clipping class source code and the use of the method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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