PHP implementation of the picture is not variant crop and picture by proportional cropping method _php skills

Source: Internet
Author: User
Tags imagecopy imagejpeg php programming

The example of this article describes the implementation of the graphics of the PHP picture is not variant cropping and picture scaling method. Share to everyone for your reference, specific as follows:

Picture No variant cropping

<?php/** * imagecropper * @param string $source _path * @param string $target _width * @param string $target _height
  * * 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;
    if ($source _ratio > $target _ratio) {//image-to-height $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) {//image-to-widht $cropped _width = $source _height/$target _ratio;
    $cropped _height = $source _height;
    $source _x = ($source _width-$cropped _width)/2;
  $source _y = 0;
    }else{//image-size-ok $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;
  Break
  } $target _image = Imagecreatetruecolor ($target _width, $target _height);
  $cropped _image = Imagecreatetruecolor ($cropped _width, $cropped _height);
  Copy 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, $CR
  Opped_height);
  Header (' Content-type:image/jpeg ');
  Imagejpeg ($target _image);
  Imagedestroy ($source _image);
  Imagedestroy ($target _image);
Imagedestroy ($cropped _image);
} $filename = "8fcb7a0831b79c61.jpg"; ImAgecropper ($filename, 200,200);?>

 

picture cropping

proportionally

<?php/** * imagezoom * @param string $file * @param double $zoom */function Imagezoom ($filename, $zoom =0.6) {//
  Baseinfo $sourceImageInfo = getimagesize ($filename);
  $sourceWidth = $sourceImageInfo [0];
  $sourceHeight = $sourceImageInfo [1];
  $sourceMine = $sourceImageInfo [' MIME '];
  $sourceRatio = $sourceWidth/$sourceHeight;
  $sourceX = 0;
  $sourceY = 0;
  Zoom $targetRatio = $zoom;
  Target-widht-height $targetWidth = $sourceWidth * $targetRatio;
  $targetHeight = $sourceHeight * $targetRatio;
  Init-params $sourceImage = null;
      Switch ($sourceMine) {case ' image/gif ': $sourceImage = Imagecreatefromgif ($filename);
    Break
      Case ' image/jpeg ': $sourceImage = Imagecreatefromjpeg ($filename);
    Break
      Case ' image/png ': $sourceImage = Imagecreatefrompng ($filename);
    Break
      Default:return;
  Break
  //temp-target-image $tempSourceImage = Imagecreatetruecolor ($sourceWidth, $sourceHeight); $targetImage = Imagecreatetruecolor ($targetWidth, $targetHeight);
  Copy imagecopy ($tempSourceImage, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight); Zoom imagecopyresampled ($targetImage, $tempSourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $source
  Height);
  Header header (' Content-type:image/jpeg ');
  Image-loading imagejpeg ($targetImage);
  Destroy Imagedestroy ($tempSourceImage);
  Imagedestroy ($sourceImage);
Imagedestroy ($targetImage);
} $filename = "8fcb7a0831b79c61.jpg";

 Imagezoom ($filename);?>

I hope this article will help you with your PHP programming.

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.