Upload image and thumbnail generation in Code Igniter
Posted in post on June 15th, 2010 by Aditya – 3 CommentsController
function index()
{
$this->load->model('Gallery_model');
if($this->input->post('upload')){
$this->Gallery_model->do_upload();
}
$this->load->view('gallery');
}
view
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile'); //userfile is default... dont bother changing
echo form_submit('upload', 'Upload bebs!');
echo form_close();
?>
class Gallery_model extends Model {
var $gallery_path;
var $gallery_path_url;
function Gallery_model()
{
parent::Model(); // a must for constructor
//APPPATH IS AVAILABLE IN THE ENTIRE CODEIG
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/'; //dunno for what
}
function do_upload()
{
$config = array(
'allowed_types' => "jpg|jpeg|gif|png",
'upload_path' => $this->gallery_path,
'max_size'=>2000
);
$this->load->library('upload', $config);
$this->upload->do_upload(); //do upload
//want to create thumbnail
$image_data = $this->upload->data(); //get image data
$config = array(
'source_image' => $image_data['full_path'], //get original image
'new_image' => $this->gallery_path . '/thumbs', //save as new image //need to create thumbs first
'maintain_ratio' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config); //load library
$this->image_lib->resize(); //do whatever specified in config
}
}