PHP implements the center crop and zoom image

Source: Internet
Author: User
1
 Getimagewidth (); Ten $height = $Image->getimageheight (); 11//Determine if the photo is long and wide equal to the IF ($width! = $height) {13//unequal) first truncate the image center portion with the minimum edge ($width > $height) {1 5 $x = ($width-$height)/2; $y = 0; $width = $height;         }else{$y = ($height-$width)/2; $x = 0; $height = $width; 22 } $Image->crop ($file, $width, $height, $x, $y, $file); 24} 25 26//Proportional zoom picture $dst _w = 100; $DST _h = 100; $Image->reduce ($file, $dst _w, $dst _h, $file); 31?>
 _width = $info [0]; $this->_height = $info [1]; $type = $info [' MIME ']; $type = explode ('/', $type); $this->_type = $type [1]; 44} 45//Return picture type Getimagetype Public function () {$this->_type; 48} 49/  /Return picture width public Function getimagewidth () {return $this->_width; 52} 53//Return picture height function Getimageheight () {return $this->_height; 56} 57 58/* 59 * * Picture Clipping * * $tmp _image  SOURCE file * * * $DST _w the width of the cropped picture * * * $DST the height of the cropped image of _h * * $x at the source map $x coordinates start clipping * * * $y at the source map $y coordinates start clipping 65 */ Crop Public Function ($tmp _image, $dst _w, $dst _h, $x, $y, $path) {$this->_type) {. E ' jpeg ': $src = imagecreatefromjpeg ($tmp _image); a break; Case ' gif ': $src = imagecreatefromgif ($tmp _image); a break;      74       Case ' png ': $src = imagecreatefrompng ($tmp _image); break; $DST = Imagecreatetruecolor ($dst _w, $dst _h); $color = Imagecolorallocate ($DST, 255,255,255); Bayi imagecolortransparent ($DST, $color); Imagefill ($DST, 0,0, $color); $bool = imagecopyresampled ($DST, $SRC, 0,0, $x, $y, $dst _w, $dst _h, $dst _w, $dst _h);                 100 switch ($this->_type) {$ Case ' JPEG ': imagejpeg ($DST, $path, 87); Break Case ' gif ': Imagegif ($DST, $path); a break; "PNG": Imagepng ($DST, $path); a break; 94} Imagedestroy ($SRC); Imagedestroy ($DST); 98 return $bool;     99}100 101/*102 * * Scale picture 103 * * $tmp _image SOURCE Map 104 * * $DST _w scaled picture Width * * $dst _h scaled image Height 106 */107 public Function reduce ($tmp _imAge, $dst _w, $dst _h, $path) {108 switch ($this->_type) {109 case ' jpeg ': $SRC = Imagec Reatefromjpeg ($tmp _image); 111 break;112 case ' gif ': 113 $src = imagecreatefromg if ($tmp _image); break;115 case ' png ': $src = imagecreatefrompng ($tmp _ima         GE); 117 break;118}119 $imagex = Imagesx ($SRC); $imagey = Imagesy ($SRC); 121 $DST = Imagecreatetruecolor ($dst _w, $dst _h), 122 $color = Imagecolorallocate ($DST, 255,255,255); 123 imag Ecolortransparent ($DST, $color); 124 Imagefill ($DST, 0,0, $color); $bool = imagecopyresampled ($DST, $SRC, 0,0 , 0,0, $dst _w, $dst _h, $imagex, $imagey) 126 switch ($this->_type) {127 case ' jpeg ': + IM                 Agejpeg ($DST, $path, +); 129 break;130 case ' gif ': 131 imagegif ($DST, $path); 132 Break;133 case ' PNG ': 134 imagepng ($DST, $path); 135 break;136}137 image Destroy ($SRC); 138 Imagedestroy ($DST); 139 return $bool; 140}141}
  • 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.