Automatic Resize/Crop Image - CI


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

I know this code isn't perfect (and there's a spot where it kind of repeats itself), but I figured I'd share anyway.

NOTE: This code assumes that you are programming in CodeIgniter and you have uploaded a file and posted to a Controller which then contains this chunk of code.


Copy this code and paste it in your HTML
  1. // Store uploaded file data
  2. $data = array('userfile' => $this->upload->data());
  3. // Set source file path
  4. $source_file = $data['userfile']['file_path'].$data['userfile']['file_name'];
  5. // Set resized file name
  6. $resized_file = $data['userfile']['file_path'].'sm_'.$data['userfile']['file_name'];
  7. // Set cropped file name
  8. $cropped_file = $data['userfile']['file_path'].'thumb_'.$data['userfile']['file_name'];
  9.  
  10. // Load in the image manipulation library
  11. $this->load->library('image_lib');
  12.  
  13. // Determine final width and height
  14. $wished_output_width = 160;
  15. $wished_output_height = 130;
  16.  
  17. // Parameters for file resize
  18. $config['source_image'] = $source_file;
  19. $config['new_image'] = $resized_file;
  20. $config['image_library'] = 'gd2';
  21. $config['create_thumb'] = false;
  22.  
  23. list($width, $height, $type, $attr) = getimagesize($source_file);
  24.  
  25. // Check to see if we want a square image
  26. if($wished_output_width == $wished_output_height)
  27. {
  28. if($width > $height) {
  29. $san = $width / $height;
  30. $san2 = $height / $wished_output_height;
  31. $new_width = round($width / $san2);
  32. $new_height = round($new_width / $san);
  33. }
  34. elseif($width < $height)
  35. {
  36. $san = $height / $width;
  37. $san2 = $width / $wished_output_width;
  38. $new_height = round($height / $san2);
  39. $new_width = round($new_height / $san);
  40. }
  41. else
  42. {
  43. $new_height = $wished_output_height;
  44. $new_width = $wished_output_width;
  45. }
  46. }
  47. else
  48. {
  49. // If it's not square
  50. // run some numbers to determine the right dimensions
  51. if($width > $height) {
  52. $san = $width / $height;
  53. $san2 = $height / $wished_output_height;
  54. $new_width = round($width / $san2);
  55. $new_height = round($new_width / $san);
  56. }
  57. elseif($width < $height)
  58. {
  59. $san = $height / $width;
  60. $san2 = $width / $wished_output_width;
  61. $new_height = round($height / $san2);
  62. $new_width = round($new_height / $san);
  63. }
  64. else
  65. {
  66. // If someone uploads a square image
  67. if($wished_output_width > $wished_output_height)
  68. {
  69. $san = $height / $width;
  70. $san2 = $width / $wished_output_width;
  71. $new_height = round($height / $san2);
  72. $new_width = round($new_height / $san);
  73. }
  74. elseif($wished_output_width < $wished_output_height)
  75. {
  76. $san = $width / $height;
  77. $san2 = $height / $wished_output_height;
  78. $new_width = round($width / $san2);
  79. $new_height = round($new_width / $san);
  80. }
  81. else
  82. {
  83. // ...be completely baffled...
  84. }
  85. }
  86. }
  87.  
  88. // ...more parameters for file resize
  89. $config['width'] = $new_width;
  90. $config['height'] = $new_height;
  91.  
  92. // Initiate file resize
  93. $this->image_lib->initialize($config);
  94. $this->image_lib->resize();
  95.  
  96. // Determine if file is already the right size
  97. if($new_width == $wished_output_width && $new_height == $wished_output_height) {
  98. // Do nothing because it's already the right dimensions
  99. }else {
  100. // Determine where to set crop offset for x and y
  101. $x = (($new_width - $wished_output_width) > 0) ? ($new_width - $wished_output_width)/2 : 0;
  102. $y = (($new_height - $wished_output_height) > 0) ? ($new_height - $wished_output_height)/2 : 0;
  103.  
  104. // Parameters for file cropping
  105. $config['width'] = $wished_output_width;
  106. $config['height'] = $wished_output_height;
  107. $config['x_axis'] = $x;
  108. $config['y_axis'] = $y;
  109. $config['maintain_ratio'] = FALSE;
  110. $config['source_image'] = $resized_file;
  111. $config['new_image'] = $cropped_file;
  112.  
  113. // Initiate file cropping
  114. $this->image_lib->initialize($config);
  115. $this->image_lib->crop();
  116. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.