Posted By

xkeshav on 04/25/12


Tagged

php resize image


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

xkeshav


Image resize


 / Published in: PHP
 

Resize image without losing proportion ratio. no stretching of images

  1. <?
  2. function smart_resize_image($file,
  3. $width = 0,
  4. $height = 0,
  5. $proportional = false,
  6. $output = 'file',
  7. $delete_original = true,
  8. $use_linux_commands = false ) {
  9.  
  10. if ( $height <= 0 && $width <= 0 ) return false;
  11.  
  12. # Setting defaults and meta
  13. $info = getimagesize($file);
  14. $image = '';
  15. $final_width = 0;
  16. $final_height = 0;
  17. list($width_old, $height_old) = $info;
  18.  
  19. # Calculating proportionality
  20. if ($proportional) {
  21. if ($width == 0) $factor = $height/$height_old;
  22. elseif ($height == 0) $factor = $width/$width_old;
  23. else $factor = min( $width / $width_old, $height / $height_old );
  24.  
  25. $final_width = round( $width_old * $factor );
  26. $final_height = round( $height_old * $factor );
  27. }
  28. else {
  29. $final_width = ( $width <= 0 ) ? $width_old : $width;
  30. $final_height = ( $height <= 0 ) ? $height_old : $height;
  31. }
  32.  
  33. # Loading image to memory according to type
  34. switch ( $info[2] ) {
  35. case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break;
  36. case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break;
  37. case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break;
  38. default: return false;
  39. }
  40.  
  41.  
  42. # This is the resizing/resampling/transparency-preserving magic
  43. $image_resized = imagecreatetruecolor( $final_width, $final_height );
  44. if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  45. $transparency = imagecolortransparent($image);
  46.  
  47. if ($transparency >= 0) {
  48. $transparent_color = imagecolorsforindex($image, $trnprt_indx);
  49. $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  50. imagefill($image_resized, 0, 0, $transparency);
  51. imagecolortransparent($image_resized, $transparency);
  52. }
  53. elseif ($info[2] == IMAGETYPE_PNG) {
  54. imagealphablending($image_resized, false);
  55. $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  56. imagefill($image_resized, 0, 0, $color);
  57. imagesavealpha($image_resized, true);
  58. }
  59. }
  60. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
  61.  
  62. # Taking care of original, if needed
  63. if ( $delete_original ) {
  64. if ( $use_linux_commands ) exec('rm '.$file);
  65. else @unlink($file);
  66. }
  67.  
  68. # Preparing a method of providing result
  69. switch ( strtolower($output) ) {
  70. case 'browser':
  71. $mime = image_type_to_mime_type($info[2]);
  72. header("Content-type: $mime");
  73. $output = NULL;
  74. break;
  75. case 'file':
  76. $output = $file;
  77. break;
  78. case 'return':
  79. return $image_resized;
  80. break;
  81. default:
  82. break;
  83. }
  84.  
  85. # Writing image according to type to the output destination
  86. switch ( $info[2] ) {
  87. case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
  88. case IMAGETYPE_JPEG: imagejpeg($image_resized, $output); break;
  89. case IMAGETYPE_PNG: imagepng($image_resized, $output); break;
  90. default: return false;
  91. }
  92.  
  93. return true;
  94. }
  95.  
  96. smart_resize_image('life.jpg',130,160,1,'browser',0);
  97. ?>

Report this snippet  

You need to login to post a comment.