Hi guys, I would like to upload an image that is cropped in different sizes, all at once, I did some tests and this is my controller.
I would therefore like the 16x16 measure to go to the upload/16 directory and so on.
I loaded the library "image_lib" in the autoload.
I would therefore like the 16x16 measure to go to the upload/16 directory and so on.
Code:
function __construct() {
parent::__construct();
//load this to validate the inputs in upload form
$this->load->library('form_validation');
$this->original_path = './upload';
$this->resized_path = './upload/16';
$this->thumbs_path = './upload/24';
}
public function index() {
if ($this->input->post('image_upload')) {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 16,
'height' => 16
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 24,
'height' => 24
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
//load the view along with data
$this->load->view('index');
}I loaded the library "image_lib" in the autoload.