How does PHP crop an image to a fixed size?

Source: Internet
Author: User
Tags imagecopy imagejpeg
How does PHP crop an image to a fixed size?

Make a homepage call Image, sometimes you need to get a fixed size image, because the image position of the homepage is usually specified by the designer, if it is for the latest published image call, because the customer does not know what proportion of the image will be uploaded, sometimes there is no way to determine the proportion of the image, the front-end page writers usually adopt a fixed img element height and width method to control image overflow. However, if the image proportion is not the necessary proportion, it will cause deformation after the image is called, this greatly affects the appearance of the page. the solution is to scale the page according to the original image proportion. the scaled image will inevitably be blank and the blank space will be filled with color, in this way, although the image is not deformed, there will be many problems. for example, if a user sends an image with a high Image width but a general image width, if the image is compressed into a image, after compression, the image is basically invisible.

Solution: crop any image to a fixed size, the image is not deformed, the blank space is stretched and filled, and the image is always full, leaving no blank. friends who have used bcastr should know, bcastr ensures that the image call is not deformed. for an image frame output at a fixed size, the source image has the following situations: 1: the height and width of the source image to be output are both small. write $ new_width <$ src_width & $ new_height <$ src_width 2: the aspect ratio of the image to be output is big in the aspect ratio of the source image. write $ new_width> $ src_width & $ new_height> $ src_width 3: exclude the second and second types, that is, one side is enlarged, when one side is scaled down, the equivalent judgment is added. for 1 or 2, the function processing code is identical, so it can be summarized into a processing statement.

Php implementation code

  1. /*
  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. * Written by smallchicken
  5. * Time
  6. */
  7. // Obtain an image of any size. The image is stretched out in a few places without deformation or blank space.
  8. Function my_image_resize ($ src_file, $ dst_file, $ new_width, $ new_height ){
  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. Imagecopy ($ inter_img, $ src_img, 0, 0, 0, $ inter_w, $ inter_h );
  56. // Generate a temporary image with the maximum edge length as the size of the target image $ ratio
  57. // Define a new image
  58. $ New_img = imagecreatetruecolor ($ new_width, $ new_height );
  59. Imagecopyresampled ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height, $ inter_w, $ inter_h );
  60. Switch ($ type ){
  61. Case IMAGETYPE_JPEG:
  62. Imagejpeg ($ new_img, $ dst_file, 100); // store images
  63. Break;
  64. Case IMAGETYPE_PNG:
  65. Imagepng ($ new_img, $ dst_file, 100 );
  66. Break;
  67. Case IMAGETYPE_GIF:
  68. Imagegif ($ new_img, $ dst_file, 100 );
  69. Break;
  70. Default:
  71. Break;
  72. }
  73. } // End if 1
  74. // 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.
  75. // = If ($ ratio_w <1 & $ ratio_h> 1) | ($ ratio_w> 1 & $ ratio_h <1 ))
  76. Else {
  77. $ Ratio = $ ratio_h> $ ratio_w? $ Ratio_h: $ ratio_w; // The value with a large proportion.
  78. // Define a large middle image with the same height or width as the target image, and then enlarge the source image
  79. $ Inter_w = (int) ($ w * $ ratio );
  80. $ Inter_h = (int) ($ h * $ ratio );
  81. $ Inter_img = imagecreatetruecolor ($ inter_w, $ inter_h );
  82. // Scale and crop the source image
  83. Imagecopyresampled ($ inter_img, $ src_img, 0, 0, 0, $ inter_w, $ inter_h, $ w, $ h );
  84. // Define a new image
  85. $ New_img = imagecreatetruecolor ($ new_width, $ new_height );
  86. Imagecopy ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height );
  87. Switch ($ type ){
  88. Case IMAGETYPE_JPEG:
  89. Imagejpeg ($ new_img, $ dst_file, 100); // store images
  90. Break;
  91. Case IMAGETYPE_PNG:
  92. Imagepng ($ new_img, $ dst_file, 100 );
  93. Break;
  94. Case IMAGETYPE_GIF:
  95. Imagegif ($ new_img, $ dst_file, 100 );
  96. Break;
  97. Default:
  98. Break;
  99. }
  100. } // If3
  101. } // End function
  102. ?>

Related Article

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.