PHP image cropping function (image not deformed)

Source: Internet
Author: User
Tags imagecopy imagejpeg
PHP image cropping function (image not deformed)

  1. * Exif_imagetype -- determines the type of an image.
  2. * Description: The function is to crop an image of any size without deformation.
  3. * Parameter description: Enter the file name of the image to be processed, generate the save file name of the new image, generate the width of the new image, and generate the height of the new image.
  4. */
  5. // Obtain an image of any size. The image is stretched out in a few places without deformation or blank space.
  6. Function my_image_resize ($ src_file, $ dst_file, $ new_width, $ new_height ){
  7. $ New_width = intval ($ new_width );
  8. $ New_height = intval ($ new_width );
  9. If ($ new_width <1 | $ new_height <1 ){
  10. Echo "params width or height error! ";
  11. Exit ();
  12. }
  13. If (! File_exists ($ src_file )){
  14. Echo $ src_file. "is not exists! ";
  15. Exit ();
  16. }
  17. // Image type
  18. $ Type = exif_imagetype ($ src_file );
  19. $ Support_type = array (IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF );
  20. If (! In_array ($ type, $ support_type, true )){
  21. Echo "this type of image does not support! Only support jpg, gif or png ";
  22. Exit ();
  23. }
  24. // Load image
  25. Switch ($ type ){
  26. Case IMAGETYPE_JPEG:
  27. $ Src_img = imagecreatefromjpeg ($ src_file );
  28. Break;
  29. Case IMAGETYPE_PNG:
  30. $ Src_img = imagecreatefrompng ($ src_file );
  31. Break;
  32. Case IMAGETYPE_GIF:
  33. $ Src_img = imagecreatefromgif ($ src_file );
  34. Break;
  35. Default:
  36. Echo "Load image error! ";
  37. Exit ();
  38. }
  39. $ W = imagesx ($ src_img );
  40. $ H = imagesy ($ src_img );
  41. $ Ratio_w = 1.0 * $ new_width/$ w;
  42. $ Ratio_h = 1.0 * $ new_height/$ h;
  43. $ Ratio = 1.0;
  44. // The aspect ratio of the generated image is small or big. The principle is to enlarge the image by a large proportion and reduce the image by a large proportion (the scaled down ratio is relatively small)
  45. If ($ ratio_w <1 & $ ratio_h <1) | ($ ratio_w> 1 & $ ratio_h> 1 )){
  46. If ($ ratio_w <$ ratio_h ){
  47. $ Ratio = $ ratio_h; // Case 1: The width ratio is smaller than the height direction. crop or zoom in according to the height ratio standard.
  48. } Else {
  49. $ Ratio = $ ratio_w;
  50. }
  51. // Define an intermediate temporary Image. the aspect ratio of the image meets the target requirements.
  52. $ Inter_w = (int) ($ new_width/$ ratio );
  53. $ Inter_h = (int) ($ new_height/$ ratio );
  54. $ Inter_img = imagecreatetruecolor ($ inter_w, $ inter_h );
  55. // Var_dump ($ inter_img );
  56. Imagecopy ($ inter_img, $ src_img, 0, 0, 0, $ inter_w, $ inter_h );
  57. // Generate a temporary image with the maximum edge length as the size of the target image $ ratio
  58. // Define a new image
  59. $ New_img = imagecreatetruecolor ($ new_width, $ new_height );
  60. // Var_dump ($ new_img); exit ();
  61. Imagecopyresampled ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height, $ inter_w, $ inter_h );
  62. Switch ($ type ){
  63. Case IMAGETYPE_JPEG:
  64. Imagejpeg ($ new_img, $ dst_file, 100); // store images
  65. Break;
  66. Case IMAGETYPE_PNG:
  67. Imagepng ($ new_img, $ dst_file, 100 );
  68. Break;
  69. Case IMAGETYPE_GIF:
  70. Imagegif ($ new_img, $ dst_file, 100 );
  71. Break;
  72. Default:
  73. Break;
  74. }
  75. } // End if 1
  76. // 2. if one side of the target image is greater than the source image and the other side is smaller than the source image, the image is enlarged and then cropped.
  77. // = If ($ ratio_w <1 & $ ratio_h> 1) | ($ ratio_w> 1 & $ ratio_h <1 ))
  78. Else {
  79. $ Ratio = $ ratio_h> $ ratio_w? $ Ratio_h: $ ratio_w; // The value with a large proportion.
  80. // Define a large middle image with the same height or width as the target image, and then enlarge the source image
  81. $ Inter_w = (int) ($ w * $ ratio );
  82. $ Inter_h = (int) ($ h * $ ratio );
  83. $ Inter_img = imagecreatetruecolor ($ inter_w, $ inter_h );
  84. // Scale and crop the source image
  85. Imagecopyresampled ($ inter_img, $ src_img, 0, 0, 0, $ inter_w, $ inter_h, $ w, $ h );
  86. // Define a new image
  87. $ New_img = imagecreatetruecolor ($ new_width, $ new_height );
  88. Imagecopy ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height );
  89. Switch ($ type ){
  90. Case IMAGETYPE_JPEG:
  91. Imagejpeg ($ new_img, $ dst_file, 100); // store images
  92. Break;
  93. Case IMAGETYPE_PNG:
  94. Imagepng ($ new_img, $ dst_file, 100 );
  95. Break;
  96. Case IMAGETYPE_GIF:
  97. Imagegif ($ new_img, $ dst_file, 100 );
  98. Break;
  99. Default:
  100. Break;
  101. }
  102. } // If3
  103. } // End function
  104. My_image_resize('test.gif ', '11111.gif', '100px ', '100px ');
  105. ?>

Summary: The above php image cropping function flexibly applies related functions of the php gd library, including imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, imagecreatetruecolor and other functions. Therefore, flexible understanding of the usage of various functions in the php gd Library is crucial for the implementation of image cropping, watermarks, thumbnails, and other functions.

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.