Example of a custom image center clipping function implemented by PHP

Source: Internet
Author: User
Tags imagecopy imagejpeg

The general idea of Image center cut:

1. First zoom the image so that the scaled image can cover the cut area exactly. (imagecopyresampled― resample Copy part of image and resize)

2. Place the zoomed image in the middle of the clipping area. (Part of the imagecopy― copy image)

3. Cut the image and save it. (imagejpeg | imagepng | imagegif― output image to browser or file)

Specific code:

<?PHP//================== Scaling clipping function ====================/** * Center crop picture * @param string $source [Original path] * @param int $width [set width] * @param int $height [Set height] * @param string $targ ET [Target path] * @return bool [crop result]*/functionImage_center_crop ($source,$width,$height,$target){    if(!file_exists($source))return false; /*loading images by type*/    Switch(Exif_imagetype ($source)) {         CaseImagetype_jpeg:$image= Imagecreatefromjpeg ($source);  Break;  CaseImagetype_png:$image= Imagecreatefrompng ($source);  Break;  CaseImagetype_gif:$image= Imagecreatefromgif ($source);  Break; }    if(!isset($image))return false; /*Get image size information*/    $target _w=$width; $target _h=$height; $source _w= Imagesx ($image); $source _h= Imagesy ($image); /*Calculate crop width and height*/    $judge= (($source _w/$source _h) > ($target _w/$target _h)); $resize _w=$judge? ($source _w*$target _h) /$source _h:$target _w; $resize _h= !$judge? ($source _h*$target _w) /$source _w:$target _h; $start _x=$judge? ($resize _w-$target _w)/2:0; $start _y= !$judge? ($resize _h-$target _h)/2:0; /*Draw Center Zoom image*/    $resize _img= Imagecreatetruecolor ($resize _w,$resize _h); Imagecopyresampled ($resize _img,$image, 0, 0, 0, 0,$resize _w,$resize _h,$source _w,$source _h); $target _img= Imagecreatetruecolor ($target _w,$target _h); Imagecopy ($target _img,$resize _img, 0, 0,$start _x,$start _y,$resize _w,$resize _h); /*save a picture to a file*/    if(!file_exists(dirname($target)))mkdir(dirname($target), 0777,true); Switch(Exif_imagetype ($source)) {         CaseImagetype_jpeg:imagejpeg ($target _img,$target);  Break;  CaseImagetype_png:Imagepng ($target _img,$target);  Break;  CaseImagetype_gif:Imagegif ($target _img,$target);  Break; }//Return boolval (file_exists ($target));//php5.5 the Boolval () function can be used to get the returned Boolean value    return file_exists($target)?true:false;//compatible with low-version PHP notation}//================== function Usage ====================//The path of the original picture$source= ' Photoclip.jpg ';$width= 480;//the width after cropping$height= 480;//cropped height//cropped image storage directory$target= ' Dd111.jpg ';//Save to destination folder after croppingif(Image_center_crop ($source,$width,$height,$target)) {    Echo"Original 1440*900: $source' > '; Echo"; Echo"Modified picture 480*480: $target' > ';}

Attached: Problems encountered in code testing

Error:call an undefined function exif_imagetype ()

Workaround:

Open extensionextension=php_exif.dll

and extension=php_mbstring.dll put it to the extension=php_exif.dll front

another: boolval() function is a function that can be used above the PHP5.5 version, this article tests the code for a compatible low version, using the following statement instead

return file_exists ($target)? true:false;

Example of a custom image center clipping function implemented by PHP

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.