Hi Robert,
This is a controller I am using which does pretty much what I think you are looking for but uses te native CI image library. I also want to store the original image plus one for display on the web and another as a thumbnail So I resize it twice as you can see in the code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pictures extends CI_Controller {
function __construct()
{
parent::__construct();
/* Standard Libraries of codeigniter are required */
$this->load->database();
$this->load->helper('url');
$this->load->helper('text');
$this->load->helper('inflector');
/* ------------------ */
$this->load->library('grocery_CRUD');
}
public function index()
{
echo "<h1>Welcome to Pictures admin</h1>";
die();
}
public function pictures_list()
{
$crud = new grocery_CRUD();
$this->load->config('grocery_crud');
$this->config->set_item('grocery_crud_file_upload_allow_file_types','gif|jpeg|jpg|png');
$crud->set_subject('Pictures');
$crud->set_table('pictures');
// select only pictures belonging to a specific gallery
$gallery = $this->uri->segment(3,0);
if ($gallery != 0)
{
$crud->where('pictures.gallery_id',$gallery);
}
else
{
$crud->where('pictures.gallery_id >',0);
}
$crud->order_by('picture_title');
$crud->columns('gallery_id','picture_title','picture_id','picture_file','picture_status','picture_owner','picture_create_date','picture_creator');
$crud->add_fields('picture_slug','picture_title','picture_description','picture_owner','gallery_id','picture_status','picture_file','picture_create_date','picture_creator');
$crud->edit_fields('picture_slug','picture_title','picture_description','picture_owner','gallery_id','picture_status','picture_create_date','picture_creator');
$crud->set_relation('gallery_id','galleries','gallery_name');
$crud->display_as('gallery_id','Gallery');
$crud->set_field_upload('picture_file','assets/uploads/images/galleries/full');
$crud->field_type('picture_status','true_false');
$crud->field_type('picture_slug','invisible');
$crud->field_type('picture_create_date','invisible');
$crud->field_type('picture_creator','invisible');
$crud->required_fields('picture_title','picture_description','picture_owner','gallery_id','picture_status','picture_file');
//$crud->unique_fields('picture_slug');
$crud->callback_before_insert(array($this,'insert_callback'));
$crud->callback_before_update(array($this,'update_callback'));
$crud->callback_after_upload(array($this,'resize_callback'));
$crud->callback_before_delete(array($this,'delete_images'));
$output = $crud->render();
$this->_picture_output($output);
}
function _picture_output($output = null)
{
$this->load->view('admin/admin_template.php',$output);
}
function insert_callback($post_array, $primary_key = null)
{
$slug = character_limiter($post_array['picture_title'], 32);
$post_array['picture_slug'] = strtolower(underscore($slug));
$post_array['picture_create_date'] = date('Y-m-d H:i:s');
$post_array['picture_creator'] = 'fred'; // get from session
return $post_array;
}
function update_callback($post_array, $primary_key = null)
{
$slug = character_limiter($post_array['picture_title'], 32);
$post_array['picture_slug'] = strtolower(underscore($slug));
return $post_array;
}
function resize_callback($uploader_response,$field_info, $files_to_upload)
{
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$new_display = str_replace('full','display',$file_uploaded);
$new_thumb = str_replace('full','thumb',$file_uploaded);
// Resize image for display
$config['image_library'] = 'gd2';
$config['source_image'] = $file_uploaded;
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 750;
$config['height'] = 500;
$config['new_image'] = $new_display;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
$this->form_validation->set_message($this->image_lib->display_errors());
}
$this->image_lib->clear();
// Resize image for thumbs
$config['image_library'] = 'gd2';
$config['source_image'] = $new_display;
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 160;
$config['height'] = 110;
$config['new_image'] = $new_thumb;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
$this->form_validation->set_message($this->image_lib->display_errors());
}
}
function delete_images($primary_key)
{
$this->db->where('picture_id',$primary_key);
$picture = $this->db->get('pictures')->row();
$file = $picture->picture_file;
$path = 'assets/uploads/images/galleries/';
$dirs = array('full','display','thumb');
foreach($dirs as $path1)
{
if (file_exists($path.$path1.'/'.$file))
{
unlink($path.$path1.'/'.$file);
}
}
return true;
}
}
/* End of file pictures.php */
/* Location: ./application/controllers/admin/pictures.php */
Hope it helps