Image Resize function for PHP


/ Published in: PHP
Save to your folder(s)

Easily make a thumb file, with a fix size, or a cropped center image


Copy this code and paste it in your HTML
  1. function resizeImage( $file, $thumbpath, $max_side = NULL , $fixfor = NULL, $cropped = false ) {
  2.  
  3. // 1 = GIF, 2 = JPEG, 3 = PNG
  4.  
  5. if ( file_exists( $file ) ) {
  6. $type = getimagesize( $file );
  7.  
  8. if (!function_exists( 'imagegif' ) && $type[2] == 1 ) {
  9. $error = __( 'Filetype not supported. Thumbnail not created.' );
  10. }
  11. elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) {
  12. $error = __( 'Filetype not supported. Thumbnail not created.' );
  13. }
  14. elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) {
  15. $error = __( 'Filetype not supported. Thumbnail not created.' );
  16. } else {
  17.  
  18. // create the initial copy from the original file
  19. if ( $type[2] == 1 ) {
  20. $image = imagecreatefromgif( $file );
  21. }
  22. elseif ( $type[2] == 2 ) {
  23. $image = imagecreatefromjpeg( $file );
  24. }
  25. elseif ( $type[2] == 3 ) {
  26. $image = imagecreatefrompng( $file );
  27. }
  28.  
  29. if ( function_exists( 'imageantialias' ))
  30. imageantialias( $image, TRUE );
  31.  
  32. $image_attr = getimagesize( $file );
  33. $max_side = $max_side ? $max_side : $image_attr[0];
  34. // figure out the longest side
  35. if($fixfor){
  36. if($fixfor == 'width'){
  37. $image_width = $image_attr[0];
  38. $image_height = $image_attr[1];
  39. $image_new_width = $max_side;
  40.  
  41. $image_ratio = $image_width / $image_new_width;
  42. $image_new_height = $image_height / $image_ratio;
  43. }elseif($fixfor == 'height'){
  44. $image_width = $image_attr[0];
  45. $image_height = $image_attr[1];
  46. $image_new_height = $max_side;
  47.  
  48. $image_ratio = $image_height / $image_new_height;
  49. $image_new_width = $image_width / $image_ratio;
  50. }
  51. }elseif($cropped){
  52. $image_new_width = $max_side*2;
  53. $image_ratio = $image_attr[0] / $image_new_width;
  54. $image_new_height = $image_attr[1] / $image_ratio;
  55.  
  56. $image_resized_crop = imagecreatetruecolor( $image_new_width, $image_new_height);
  57. @imagecopyresampled( $image_resized_crop, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
  58.  
  59. $cropX = intval((imagesx($image_resized_crop) - $max_side) / 2);
  60. $cropY = intval((imagesy($image_resized_crop) - $max_side) / 2);
  61.  
  62. }else{
  63. if ( $image_attr[0] > $image_attr[1] ) {
  64. $image_width = $image_attr[0];
  65. $image_height = $image_attr[1];
  66. $image_new_width = $max_side;
  67.  
  68. $image_ratio = $image_width / $image_new_width;
  69. $image_new_height = $image_height / $image_ratio;
  70. //width is > height
  71. } else {
  72. $image_width = $image_attr[0];
  73. $image_height = $image_attr[1];
  74. $image_new_height = $max_side;
  75.  
  76. $image_ratio = $image_height / $image_new_height;
  77. $image_new_width = $image_width / $image_ratio;
  78. //height > width
  79. }
  80. }
  81.  
  82.  
  83. if(!$cropped){
  84. $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
  85. @imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
  86. }elseif($cropped){
  87. $thumbnail = imagecreatetruecolor( $max_side, $max_side);
  88. @imagecopyresampled( $thumbnail, $image_resized_crop, 0, 0, $cropX, $cropY, $max_side, $max_side, $max_side, $max_side);
  89. }
  90.  
  91. if (!imagejpeg( $thumbnail, $thumbpath ) ) {
  92. $error = 0;
  93. }
  94. }
  95. } else {
  96. $error = 0;
  97. }
  98.  
  99. if (!empty ( $error ) ) {
  100. return $error;
  101. } else {
  102. return $thumbpath;
  103. }
  104. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.