Home > Backend Development > PHP Tutorial > PHP image cropping function (image does not deform)

PHP image cropping function (image does not deform)

WBOY
Release: 2016-07-25 08:53:32
Original
888 people have browsed it
  1. *exif_imagetype -- Determine the type of an image
  2. *Description: The function is to crop an image into 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, generate the height of the new image
  4. */
  5. // Get an image of any size, stretch the missing parts, no deformation, no 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 height and width of the generated image are smaller or larger than the original ones. The principle is to enlarge by a large ratio and reduce by a large ratio (the reduced ratio will be smaller)
  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 ratio of width to height is small, crop or enlarge it according to the height ratio standard
  48. }else {
  49. $ratio = $ratio_w ;
  50. }
  51. // Define an intermediate temporary image whose aspect ratio just 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,0,$inter_w,$inter_h);
  57. // Generate a temporary image with the maximum side 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,0,$new_width,$new_height,$ inter_w,$inter_h);
  62. switch($type) {
  63. case IMAGETYPE_JPEG :
  64. imagejpeg($new_img, $dst_file,100); // Store image
  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 One side of the target image is larger than the original Picture, one side is smaller than the original picture, first enlarge the ordinary image, and then crop
  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; //Take the value with the larger ratio
  80. //Define a large image in the middle, the height or width of the image is equal to the target image, and then Enlarge the original image
  81. $inter_w=(int)($w * $ratio);
  82. $inter_h=(int) ($h * $ratio);
  83. $inter_img=imagecreatetruecolor($inter_w , $inter_h);
  84. // Scale the original image and then crop it
  85. imagecopyresampled($inter_img,$src_img,0,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,0,$new_width,$new_height);
  89. switch($type) {
  90. case IMAGETYPE_JPEG :
  91. imagejpeg($new_img, $ dst_file,100); // Store image
  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. ?>
Copy the code

Editor’s summary: The PHP image cropping function implemented above flexibly applies the relevant functions of the PHP gd library, including the usage of imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, imagecreatetruecolor and other functions. Therefore, flexibly mastering the usage of each function in the PHP gd library is crucial for the realization of functions such as cropping pictures, watermarks, and thumbnails.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template