Recently in the Web page drag-and-drop verification Code of the Open source project, need to generate a picture in the service side of the corresponding removable color block, but the resources on the web is to do thumbnails, the entire picture scaling, so do-it-yourself, finished the picture to cut the small pieces of the tool
<?php namespace App\libs; /** * 2016-01-07 15:54:58 * Lixiaoyu * Mode 1: Forced cropping, create pictures strictly according to need, not enough to enlarge, more than cut, the picture is always paved with * Mode 2: and 1 similar, but insufficient time not to put the assembly to produce filler, can use PN
G Elimination. * Mode 3: Only scaling, not cropping, retain all picture information, will produce filler, * Mode 4: Only zoom, do not crop, keep all picture information, generate picture size for the final zoom picture of the actual size of effective information, do not produce filler * default filler for white, if you want to make filler into transparent pixels, please use SA Vealpha () method replaces 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);
Imagepng ($this->dimage, $this->dst_file);
Break
Case Imagetype_gif:imagegif ($this->dimage, $this->dst_file);
Break
Default:break; The 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 crop if ($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/$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); }else{$ratio = $ratio _w < $ratio _h $ratio _h: $ratio _w; $tmp _w = (int) ($this->src_width * $ratio); $tmp _h = (int)
($this->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 small if ($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/$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 size if ($ratio _w > 1 && $ratio _h > 1) {$dst _x = (int) (ABS ($ds
t_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)
($this->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 size if ($ratio _w > 1 && $ratio _h > 1) {$this->dimage = imag
Ecreatetruecolor ($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)
($this->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/** * * 2016-01-07 15:05:44 * Lixiaoyu * * @param $dst _width Target long * @param $DST _height Target high * @param $ Dst_x the distance between the cropped part and the left side of the original image * @param $dst the distance between the _y clipping part and the original image * @param int $mode mode * @param string $dst _file destination file path * * Function cut ($ds T_width, $dst _height, $dst _x, $dst _y, $dst _file= ') {if ($dst _file) $this->dst_file = $dst _file; Set Target file location $this->dimage = Imagecreatetruecolor ($dst _width, $dst _height); The canvas that created the size of the target file $BG = Imagecolorallocatealpha ($this->dimage, 255, 255, 255, 127); Assign color to canvas Imagefill ($this->dimage, 0, 0, $BG); Fill the image with Color imagecolortransparent ($this->dimage, $BG); The background is defined as a transparent color $ratio _w = 1.0 * $DST _width/$this->src_width; Scaling horizontally $ratio _h = 1.0 * $DST _height/$this->src_height;
The ratio of vertical scaling//var_dump ($this);
Without scaling, the image is cropped directly $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 part of the image to cut imagecopyresampled ($this->dimage, $tmp _img,0,0,0,0, $dst _width, $dst _height, $tmp _w, $tmp _h);
Put the temporarily cached picture inside the target file Imagedestroy ($tmp _img); }}?>
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)
The coordinates of the src_im image are started from the src_x,src_y, the width is src_w, and the height is part of the src_h copy to the Dst_im image in the location of Dst_x and dst_y.