CI Upload_image library


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



Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Upload_image
  3. {
  4. private $ci;
  5. private $upload_path;
  6. private $copies;
  7. private $allowed_types;
  8. private $max_size;
  9. private $input;
  10.  
  11. function __construct($props = array())
  12. {
  13. $this->ci =& get_instance();
  14. $this->ci->load->library('upload');
  15. $this->ci->load->library('image_lib');
  16. if (count($props) > 0)
  17. {
  18. $this->initialize($props);
  19. }
  20. }
  21.  
  22. function initialize($config = array())
  23. {
  24. $defaults = array('copies' => false,
  25. 'allowed_types' => 'jpg|jpeg|gif|png',
  26. 'max_size' => '2000',
  27. 'input' => 'picture',
  28. 'copies' => array(),
  29. 'upload_path' => ''
  30. );
  31. foreach ($defaults as $key => $val)
  32. {
  33. $this->$key = isset($config[$key]) ? $config[$key] : $val;
  34. }
  35. }
  36.  
  37. function upload()
  38. {
  39. $config['upload_path'] = $this->upload_path;
  40. $config['allowed_types'] = $this->allowed_types;
  41. $config['max_size'] = $this->max_size;
  42. $this->ci->upload->initialize($config);
  43.  
  44. if ($this->ci->upload->do_upload($this->input))
  45. {
  46. $upload = $this->ci->upload->data();
  47.  
  48. // Set image name
  49. while (true)
  50. {
  51. $img_name = uniqid().$upload['file_ext'];
  52. if (!file_exists($upload['file_path'].$img_name)) break;
  53. }
  54. rename($upload['full_path'], $upload['file_path'].$img_name);
  55.  
  56. // Creating image copies
  57. if (count($this->copies) > 0)
  58. {
  59. foreach ($this->copies as $copy)
  60. {
  61. $resize_config = array('file_name' => $img_name,
  62. 'source_path' => $upload['file_path'],
  63. 'copy' => $copy);
  64. $this->resize($resize_config);
  65. }
  66. }
  67. return $img_name;
  68. }
  69. return false;
  70. }
  71.  
  72. function resize($init)
  73. {
  74. $config = array('source_image' => $init['source_path'].$init['file_name'],
  75. 'new_image' => $init['copy']['upload_path'].$init['file_name']);
  76. list($img['image_width'], $img['image_height'], $img['type'], $img['attr']) = getimagesize($config['source_image']);
  77.  
  78. if ($init['copy']['crop'])
  79. {
  80. // Resize to make smaller edge = selected size
  81. if ($img['image_width'] > $img['image_height'])
  82. {
  83. $config['height'] = $init['copy']['height'];
  84. $ratio = $init['copy']['height'] / $img['image_height'];
  85. $config['width'] = $img['image_width'] * $ratio;
  86. }
  87. else
  88. {
  89. $config['width'] = $init['copy']['width'];
  90. $ratio = $init['copy']['width'] / $img['image_width'];
  91. $config['height'] = $img['image_height'] * $ratio;
  92. }
  93. $this->ci->image_lib->initialize($config);
  94. $this->ci->image_lib->resize();
  95. $config['width'] = $init['copy']['width'];
  96. $config['height'] = $init['copy']['height'];
  97. $config['maintain_ratio'] = false;
  98. $config['source_image'] = $config['new_image'];
  99. unset($config['new_image']);
  100. $this->ci->image_lib->initialize($config);
  101. $this->ci->image_lib->crop();
  102. }
  103. else
  104. {
  105. // Width adjustment
  106. if ($img['image_width'] > $init['copy']['width'])
  107. {
  108. $config['width'] = $init['copy']['width'];
  109. $ratio = $init['copy']['width'] / $img['image_width'];
  110. $config['height'] = $img['image_height'] * $ratio;
  111. }
  112.  
  113. // Height adjustment
  114. if ($config['height'] > $init['copy']['height'])
  115. {
  116. $config['height'] = $init['copy']['height'];
  117. $ratio = $init['copy']['height'] / $img['image_height'];
  118. $config['width'] = $img['image_width'] * $ratio;
  119. }
  120.  
  121. $this->ci->image_lib->initialize($config);
  122. $this->ci->image_lib->resize();
  123. }
  124. }
  125.  
  126. }
  127.  
  128. // End of file
  129.  
  130. // Using
  131.  
  132. $this->load->library('upload_image');
  133. $upload_path = './uploads/property/';
  134. $copies[] = array('upload_path' => $upload_path.'mid/', 'width' => 90, 'height' => 75, 'crop' => false);
  135. $copies[] = array('upload_path' => $upload_path.'small/', 'width' => 50, 'height' => 50, 'crop' => true);
  136. $config = array('upload_path' => $upload_path.'original/', 'input' => 'picture', 'copies' => $copies);
  137. $this->upload_image->initialize($config);
  138. $result = $this->upload_image->upload();
  139. echo $result ? 'Image '.$result.' was uploaded successfully' : 'Image was not uploaded';

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.