How does php crop a picture? PHP Picture Clipping code

Source: Internet
Author: User
    1. $src _path = ' 1.jpg ';
    2. Create a source diagram instance
    3. $SRC = imagecreatefromstring (file_get_contents ($src _path));
    4. Crop the coordinates of the point in the upper-left corner of the open area
    5. $x = 100;
    6. $y = 12;
    7. Width and height of clipping area
    8. $width = 200;
    9. $height = 200;
    10. Finally save the width and height of the picture, and the source to equal proportions, otherwise it will deform
    11. $final _width = 100;
    12. $final _height = Round ($final _width * $height/$width);
    13. Copy the cropping area to a new picture and zoom in or out based on the width of the source and destination
    14. $new _image = Imagecreatetruecolor ($final _width, $final _height);
    15. Imagecopyresampled ($new _image, $src, 0, 0, $x, $y, $final _width, $final _height, $width, $height);
    16. Output picture
    17. Header (' Content-type:image/jpeg ');
    18. Imagejpeg ($new _image);
    19. Imagedestroy ($SRC);
    20. Imagedestroy ($new _image);
Copy Code

Picture after cropping:

In fact, if the coordinates are (0,0), the width and height of the cropping area are consistent with the width of the source graph, then it is the function of generating thumbnails.

The above is only the example of the PHP cropped image, which belongs to the function of the server. If the client needs, you can use jquery plug-in imageareaselect, good compatibility.

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