Advertisement
Guest User

Untitled

a guest
May 4th, 2012
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. Codeigniter Image Manipulation Class : Resize and Crop on multiple files
  2. resize_img()
  3.  
  4. crop_img().
  5.  
  6. <?PHP
  7.  
  8. class Imagetest extends MY_Controller {
  9.  
  10. function __construct()
  11. {
  12. parent::__construct();
  13. $this->load->library('image_lib');
  14. }
  15.  
  16. function index()
  17. {
  18. $testimg1 = 'uploads/test/1.png';
  19. $testimg2 = 'uploads/test/2.png';
  20. $testimg3 = 'uploads/test/3.png';
  21.  
  22. $this->resize_img($testimg1);
  23. $this->crop_img($testimg1);
  24.  
  25. $this->resize_img($testimg2);
  26. $this->crop_img($testimg2);
  27.  
  28. $this->resize_img($testimg3);
  29. $this->crop_img($testimg3);
  30. }
  31.  
  32.  
  33. function image_thumb_name($img = null)
  34. {
  35. if(!empty($img)) {
  36. $exploded = explode('.', $img);
  37. return $exploded['0'] . '_thumb.' . $exploded['1'];
  38. }
  39. }
  40.  
  41. function get_axis($img)
  42. {
  43. // Default values
  44. $output['x_axis'] = 0;
  45. $output['y_axis'] = 0;
  46.  
  47. // Settings
  48. $config['height'] = 132;
  49. $config['width'] = 176;
  50.  
  51. if ($img_dim = getimagesize($img)) {
  52. list($thumb_width, $thumb_height) = $img_dim;
  53. } else {
  54. echo '<h1> ERROR HERE TOO</h1>';
  55. return false;
  56. }
  57.  
  58. if ($thumb_width > $config['width']) {
  59.  
  60. $output['x_axis'] = (($thumb_width - $config['width']) / 2) ;
  61.  
  62. } else if ($thumb_height > $config['height']) {
  63.  
  64. $output['y_axis'] = (($thumb_height - $config['height']) / 2);
  65. }
  66. return $output;
  67. }
  68.  
  69. function resize_img($img)
  70. {
  71. $config = array();
  72. echo 'Processing: '. $img .'<br/>'; // Debug
  73.  
  74. if ($img_dim = getimagesize($img)) {
  75. list($image_width, $image_height) = $img_dim;
  76. } else {
  77. echo '<h1> ERROR HERE </h1>';
  78. }
  79.  
  80. // create a thumbnail
  81. $config['image_library'] = 'GD2';
  82. $config['source_image'] = $img;
  83. $config['quality'] = 100;
  84. $config['height'] = 132;
  85. $config['width'] = 176;
  86. $config['create_thumb'] = TRUE;
  87. $config['maintain_ratio']= TRUE;
  88. $config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
  89. $this->image_lib->initialize($config);
  90.  
  91. if (!$this->image_lib->resize()) {
  92. echo $this->image_lib->display_errors();
  93. }
  94.  
  95. echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug
  96.  
  97. $this->image_lib->clear();
  98. unset($config);
  99. }
  100.  
  101.  
  102.  
  103.  
  104. function crop_img($img)
  105. {
  106. $config2 = array();
  107.  
  108. // Crop that thumbnail
  109. $config2['image_library'] = 'GD2';
  110. $config2['quality'] = 100;
  111. $config2['height'] = 132;
  112. $config2['width'] = 176;
  113. $config2['source_image'] = $this->image_thumb_name($img);
  114. $axis = $this->get_axis($config2['source_image']);
  115. $config2['x_axis'] = $axis['x_axis'];
  116. $config2['y_axis'] = $axis['y_axis'];
  117. $config2['maintain_ratio'] = FALSE;
  118. $config2['create_thumb'] = FALSE;
  119. $this->image_lib->initialize($config2);
  120.  
  121. if (!$this->image_lib->crop()) {
  122. echo $this->image_lib->display_errors();
  123. }
  124.  
  125. echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug
  126.  
  127. $this->image_lib->clear();
  128. unset($config2);
  129. }
  130.  
  131. }
  132.  
  133. $config['create_thumb'] = FALSE;
  134. $config['new_image'] = $this->image_thumb_name($img);
  135.  
  136. var_dump($config['source_image']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement