PHP code for non-deforming screenshots

Source: Internet
Author: User
  1. function My_image_resize ($src _file, $dst _file, $new _width, $new _height)
  2. {
  3. $src _img=imagecreatefromjpeg ($src _file);
  4. $w =imagesx ($src _img);
  5. $h =imagesy ($src _img);
  6. $ratio _w=1.0 * $new _width/$w;
  7. $ratio _h=1.0 * $new _height/$h;
  8. $ratio = 1.0;
  9. The resulting image of the aspect ratio of the original is small, or all large, the principle is to take a large proportion of magnification, take a large proportion of the reduction (smaller proportion)
  10. if (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1))
  11. {
  12. if ($ratio _w < $ratio _h)
  13. {
  14. $ratio = $ratio _h; Case one, the width of the ratio is smaller than the height direction, according to the height of the proportional standard to cut or enlarge
  15. }else {
  16. $ratio = $ratio _w;
  17. }
  18. Defines an intermediate temporary image that meets the target's aspect ratio exactly.
  19. $inter _w= (int) ($new _width/$ratio);
  20. $inter _h= (int) ($new _height/$ratio);
  21. $inter _img=imagecreatetruecolor ($inter _w, $inter _h);
  22. Imagecopy ($inter _img, $src _img, 0,0,0,0, $inter _w, $inter _h);
  23. Generates a temporary image that is the size of the maximum edge length, which is the $ratio scale of the target image
  24. To define a new image
  25. $new _img=imagecreatetruecolor ($new _width, $new _height);
  26. Imagecopyresampled ($new _img, $inter _img,0,0,0,0, $new _width, $new _height, $inter _w, $inter _h);
  27. Imagejpeg ($new _img, $dst _file,100); Storing images
  28. }
  29. End If 1
  30. 2 One edge of the target image is larger than the original, one edge is smaller than the original, the flat image is enlarged, then cropped
  31. if (($ratio _w < 1 && $ratio _h > 1) | | ($ratio _w >1 && $ratio _h <1))
  32. Else
  33. {
  34. $ratio = $ratio _h> $ratio _w? $ratio _h: $ratio _w; Take a large proportion of that value
  35. Defines an intermediate large image that is equal in height or width of the target image, and then zooms in on the original image
  36. $inter _w= (int) ($W * $ratio);
  37. $inter _h= (int) ($H * $ratio);
  38. $inter _img=imagecreatetruecolor ($inter _w, $inter _h);
  39. Crop after scaling the original
  40. Imagecopyresampled ($inter _img, $src _img,0,0,0,0, $inter _w, $inter _h, $w, $h);
  41. To define a new image
  42. $new _img=imagecreatetruecolor ($new _width, $new _height);
  43. Imagecopy ($new _img, $inter _img, 0,0,0,0, $new _width, $new _height);
  44. }
  45. Imagejpeg ($new _img, $dst _file,100); Storing images
  46. }
  47. $filename = "110.jpg"; The path to the original picture
  48. $fileto = "120.jpg";//after the saved path
  49. $new the width of the _w=300;//set
  50. $new _h=350; Set the high
  51. My_image_resize ($filename, $fileto, $new _w, $new _h);
  52. ?>
Copy Code
No distortion, 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.