Phpgd library for server-side image cropping and thumbnails

Source: Internet
Author: User
Phpgd library for server-side image cropping and thumbnails

For example, image cropping in php mainly uses the imagecopyresampled method of the gd Library. Image cropping example:

The final cropped image:

In the dotted box, the cropped image is saved as a 100-width image. Code:

  1. $ Src_path = '1.jpg ';
  2. // Create an instance of the source image
  3. $ Src = imagecreatefromstring (file_get_contents ($ src_path ));
  4. // Crop the coordinates of the points in the upper left corner of the area.
  5. $ X = 100;
  6. $ Y = 12;
  7. // Width and height of the cropping area
  8. $ Width = 200;
  9. $ Height = 200;
  10. // The image is saved to the width and height of the image, and the source-to-source ratio. Otherwise, the image will be deformed.
  11. $ Final_width = 100;
  12. $ Final_height = round ($ final_width * $ height/$ width );
  13. // Copy the cropping area to the new image and scale or pull the image up or down based on the width and height of the source and target.
  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 image
  17. Header ('content-Type: image/jpeg ');
  18. Imagejpeg ($ new_image );
  19. Imagedestroy ($ src );
  20. Imagedestroy ($ new_image );

In fact, if the coordinates are (0, 0), the width and height of the cropping area are the same as those of the source image, then the thumbnail is generated. In summary, only examples of php cropping images are listed, which are server-side functions. If the client needs it, we recommend a jquery plug-in imageAreaSelect with excellent 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.