PHP picture clipping, zooming function _php tutorial

Source: Internet
Author: User
Based on the custom target graph height, the source graph is cropped and scaled by the target graph aspect.


Incoming parameters:
$source _path String Source map path
$target _width integer Target graph width
$target _height integer target graph height

The source map supports Mimetype:image/gif, Image/jpeg, Image/png.

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 = $s ource_height/$source _width, $target _ratio = $target _height/$target _width;//Source graph too high if ($source _ratio > $target _ratio) {$cropped _width = $source _width; $cropped _height = $source _width * $target _ratio; $source _x = 0; $source _y = ($source _heigh T $cropped _height)/2;} Source map too wide ElseIf ($source _ratio < $target _ratio) {$cropped _width = $source _height/$target _ratio; $cropped _height = $sour ce_height; $source _x = ($source _width-$cropped _width)/2; $source _y = 0;} SOURCE Graph Moderate else{$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, $ Cropped_height); header (' content-type:image/jpeg '); imagejpeg ($target _image); Imagedestroy ($source _image); Imagedestroy ($target _image); Imagedestroy ($cropped _image);}

http://www.bkjia.com/PHPjc/739133.html www.bkjia.com true http://www.bkjia.com/PHPjc/739133.html techarticle According to the custom target graph height width, according to the target graph aspect than cropping, the scale source graph. Incoming parameters: $source _pathstring Source map path $target _widthinteger target width $target _heightinteger ...

  • 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.