Php image cropping and thumbnail examples

Source: Internet
Author: User
Php image cropping and thumbnail examples

In php programming, the picture size is too large and the specification is not uniform. the display control needs to be completed by JavaScript. when used on mobile devices, the display effect is poor and the traffic is huge, the image of the existing image library needs to be processed once to generate thumbnails that meet the requirements of mobile devices, and transfer the work done by the original client JS to the server side for centralized processing using the GD library of PHP.

Required. Image source and size:

  1. List ($ src_w, $ src_h) = getimagesize ($ src_img); // Obtain the source image size
  2. $ Dst_scale = $ dst_h/$ dst_w; // aspect ratio of the target image
  3. $ Src_scale = $ src_h/$ src_w; // aspect ratio of the source image
  4. If ($ src_scale> = $ dst_scale)
  5. {
  6. // Too high
  7. $ W = intval ($ src_w );
  8. $ H = intval ($ dst_scale * $ w );
  9. $ X = 0;
  10. $ Y = ($ src_h-$ h)/3;
  11. }
  12. Else
  13. {
  14. // Too wide
  15. $ H = intval ($ src_h );
  16. $ W = intval ($ h/$ dst_scale );
  17. $ X = ($ src_w-$ w)/2;
  18. $ Y = 0;
  19. }
  20. // Crop
  21. $ Source = imagecreatefromjpeg ($ src_img );
  22. $ Croped = imagecreatetruecolor ($ w, $ h );
  23. Imagecopy ($ croped, $ source, 0, 0, $ x, $ y, $ src_w, $ src_h );
  24. // Zoom
  25. $ Scale = $ dst_w/$ w;
  26. $ Target = imagecreatetruecolor ($ dst_w, $ dst_h );
  27. $ Final_w = intval ($ w * $ scale );
  28. $ Final_h = intval ($ h * $ scale );
  29. Imagecopyresampled ($ target, $ croped, 0, 0, 0, $ final_w, $ final_h, $ w, $ h );
  30. // Save
  31. $ Timestamp = time ();
  32. Imagejpeg ($ target, "success ");
  33. Imagedestroy ($ target );
  34. ?>

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.