PHP Full-featured non-deformable picture clipping operation class and usage

Source: Internet
Author: User
Tags imagecopy imagejpeg
This article mainly introduces the PHP full-featured non-deformed picture clipping operation class and usage, combined with the example form analysis of PHP operation picture Cutting, scaling and other related skills, the need for friends can refer to the next

Specific as follows:

If you need to crop the picture, or generate thumbnails, this is basically no problem, all the necessary functions are in it, all of them are non-deformed.

Here we are divided into four modes:

1. Any image cropped to a specified size, over, in the play, and maximized zoom, not enough stretching,
2. Over the play in the cut, but insufficient, do not stretch, that is, only shrink, do not enlarge the crop, will produce fillers, you can use PNG transparency to eliminate
3. Keep all picture information. Do not crop, only scale, insufficient filler.
4. Keep all picture information. Do not crop, only scale, insufficient not filler, the resulting picture for the actual effective pixel size, for example, there is a picture is 600x600 now to generate 120X100 then the actual effective pixels after scaling is 100x100, so only the 100x100 image is generated, The third mode generates the size of the 120x100 and has a filler

The code is as follows (here the code is formatted with the online tool Http://tools.jb51.net/code/jb51_php_format):

<?php/*** author:smallchicken* Mode 1: Forced cropping, generate images in strict accordance with the needs, not enlarge, over cropping, the picture is always covered * Mode 2: and 1 similar, but insufficient time does not put the General Assembly to produce 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, Use the Savealpha () method instead of the SaveImage () method to invoke the method: * * $ic =new imagecrop (' old.jpg ', ' aftercrop.jpg '); * $ic->crop (120,80,2); * $  Ic->saveimage (); *//$ic->savealpha (); Change filler to transparent pixel save * $ic->destory (); ***/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 Imagecrop ($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/JP  Eg ');    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? $r        Atio_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-&GT;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 ($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) ($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-&        Gt;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) ($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}?>

The above is the whole content of this article, I hope that everyone's study has helped.


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.