PHP image processing using the imagecopyresampled function to crop picture examples _php Tips

Source: Internet
Author: User

Picture clipping refers to a large background picture cropped out a designated area of the picture, the common application is in the user set personal avatar, can be uploaded from the picture, cut out a suitable area as their personal portrait picture. Image cropping is similar to picture scaling, so it is also accomplished with the help of the imagecopyresampled () function. Also, take the JPEG image format as an example, declaring an image clipping function cut (), as shown in the following code:

Copy Code code as follows:

<?php
To crop a picture in a large background picture, in the JPEG image format, as an example
function Cut ($filename, $x, $y, $width, $height) {
$back = Imagecreatetruecolor ($width, $height);
Create a resource that can save cropped pictures
$cutimg = Imagecreatetruecolor ($width, $height);
To crop a picture using the imagecopyresampled () function
Imagecopyresampled ($cutimg, $back, 0,0, $x, $y, $width, $height, $width, $height);
Save the cropped picture and prefix the cropped picture if you don't want to overwrite the picture
Imagejpeg ($cutimg, $filename);
Imagedestroy ($CUTIMG);
Imagedestroy ($back);
}

Cut ("Brophp.jpg", 50, 50, 200, 200);
?>

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.