PHP proportional scaling picture and cut picture code sharing _php instance

Source: Internet
Author: User
Tags imagejpeg

Scale picture and cut picture code sharing with PHP

/** * Picture scaling function (can be set height fixed, width fixed or maximum width, support gif/jpg/png three types) * author:specs * * @param string $source _path source picture * @param int $target _width Target Width * @param int $target _height Target Height * @param string $fixed _orig lock width (optional parameter width, height, or null value) * @return s Tring/function Myimageresize ($source _path, $target _width =, $target _height =, $fixed _orig = ') {$source _inf
  o = getimagesize ($source _path);
  $source _width = $source _info[0];
  $source _height = $source _info[1];
  $source _mime = $source _info[' mime ');
  $ratio _orig = $source _width/$source _height;
  if ($fixed _orig = = ' width ') {//width fixed $target _height = $target _width/$ratio _orig;
  }elseif ($fixed _orig = = ' height ') {//height fixed $target _width = $target _height * $ratio _orig; }else{//Max width or maximum high if ($target _width/$target _height > $ratio _orig) {$target _width = $target _height * $rat
    Io_orig;
    }else{$target _height = $target _width/$ratio _orig;
 Switch ($source _mime) {case ' image/gif ':     $source _image = imagecreatefromgif ($source _path);
    
    Break
      Case ' image/jpeg ': $source _image = imagecreatefromjpeg ($source _path);
    
    Break
      Case ' image/png ': $source _image = imagecreatefrompng ($source _path);
    
    Break
      Default:return false;
  Break
  } $target _image = Imagecreatetruecolor ($target _width, $target _height); Imagecopyresampled ($target _image, $source _image, 0, 0, 0, 0, $target _width, $target _height, $source _width, $source _
  height);
  Header (' Content-type:image/jpeg ');
  $IMGARR = Explode ('. ', $source _path); $target _path = $imgArr [0]. ' _new. '
  $IMGARR [1];
Imagejpeg ($target _image, $target _path, 100);
 }

Usage:

    1. Myimageresize ($filename, 200, 200); Max width and height
    2. Myimageresize ($filename, M, ' width '); Width fixed
    3. Myimageresize ($filename, M, ' height '); Highly fixed

Cut picture as fixed size:

function Imagecropper ($source _path, $target _width, $target _height) {$source _info = getimagesize ($source _path);
  $source _width = $source _info[0];
  $source _height = $source _info[1];
  $source _mime = $source _info[' mime ');
  $source _ratio = $source _height/$source _width;
  
  $target _ratio = $target _height/$target _width;
    Source graph is too high if ($source _ratio > $target _ratio) {$cropped _width = $source _width;
    $cropped _height = $source _width * $target _ratio;
    $source _x = 0;
  $source _y = ($source _height-$cropped _height)/2;
    }elseif ($source _ratio < $target _ratio) {//source graph is too wide $cropped _width = $source _height/$target _ratio;
    $cropped _height = $source _height;
    $source _x = ($source _width-$cropped _width)/2;
  $source _y = 0;
    }else{//source map Moderate $cropped _width = $source _width;
    $cropped _height = $source _height;
    $source _x = 0;
  $source _y = 0;
 Switch ($source _mime) {case ' image/gif ': $source _image = imagecreatefromgif ($source _path);     Break
      Case ' image/jpeg ': $source _image = imagecreatefromjpeg ($source _path);
    
    Break
      Case ' image/png ': $source _image = imagecreatefrompng ($source _path);
    
    Break
      Default:return false;
  Break
  } $target _image = Imagecreatetruecolor ($target _width, $target _height);
  
  $cropped _image = Imagecreatetruecolor ($cropped _width, $cropped _height);
  Crop Imagecopy ($cropped _image, $source _image, 0, 0, $source _x, $source _y, $cropped _width, $cropped _height); Zoom imagecopyresampled ($target _image, $cropped _image, 0, 0, 0, 0, $target _width, $target _height, $cropped _width, $crop
  Ped_height);
  $dotpos = Strrpos ($source _path, '. ');
  $imgName = substr ($source _path, 0, $dotpos);
  $suffix = substr ($source _path, $dotpos); $imgNew = $imgName. ' _small '.
  $suffix;
  Imagejpeg ($target _image, $imgNew, 100);
  Imagedestroy ($source _image);
  Imagedestroy ($target _image);
Imagedestroy ($cropped _image);
 }

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.