Create thumbnail image in codeigniter

Adding the following method as custom helper file and check the below sample.
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
function image_thumb($folder_name, $image_name, $width, $height)
{
    // Get the CodeIgniter super object
    $CI =& get_instance();
 
    // Path to image thumbnail
    $image_thumb = dirname('uploads/' . $folder_name . '/' . $image_name) . '/' . base64_encode($image_name) . '_' . $width . '_' . $height . strrchr($image_name, '.');
     
    if( ! file_exists($image_thumb))
    {
        // LOAD LIBRARY
        $CI->load->library('image_lib');
 
        // CONFIGURE IMAGE LIBRARY
        $config['image_library']    = 'gd2';
        $config['source_image']     = 'uploads/' . $folder_name. '/' . $image_name;
        $config['new_image']        = $image_thumb;
        $config['maintain_ratio']   = TRUE;
        $config['height']           = $height;
        $config['width']            = $width;
        $CI->image_lib->initialize($config);
        $CI->image_lib->resize();
        $CI->image_lib->clear();
    }
 
    // return '<img src="' . dirname($_SERVER['SCRIPT_NAME']) . '/' . $image_thumb . '" />';
}
 
 
/* End of file image_helper.php */
/* Location: system/application/helpers/image_helper.php */

Sample:

//uploading logo
public function logo($project_id)
{
    $this->load->library(array('form_validation','upload'));
    $this->load->helper(array('image_helper'));
             
    $this->form_validation->set_rules('logo_path', 'logo', 'trim|required');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
     
    if ($this->form_validation->run() == TRUE)
    {
        $target = UPLODPATH . 'uploads/'.$this->session->userdata('member_user_id').'/logo/'.$project_id;
        $this->_mkdir($target);
         
        $config['upload_path'] = $target;
        $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
        $this->upload->initialize($config);
        if(!$this->upload->do_upload('logo_path'))
        {
            $this->session->set_flashdata('error', IMAGE_EXTENSION_ERROR_MESSAGE);
            redirect('project/logo/'.$project_id, 'refresh');
            exit;
        }
         
        $upload = array('upload_data' => $this->upload->data());
        $this->project_model->update_project_logo($project_id, $upload['upload_data']['file_name']);
             
        //Thumbnail creation
        image_thumb($this->session->userdata('member_user_id').'/project_logo/'.$project_id, $upload['upload_data']['file_name'], 107, 72); //dashboard
     
         
        //Redirect
        $this->session->set_flashdata('message', LOGO_UPDATED_SUCCESS_MESSAGE);
        redirect('project/'.$project_id, 'refresh');
    }
     
    $this->load->view('logo');
}  
 
 
//Make directory for image uploading
function _mkdir($target)
{
    // from php.net/mkdir user contributed notes
    if(file_exists($target))
    {
        if( ! @is_dir($target))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Attempting to create the directory may clutter up our display.
    if(@mkdir($target))
    {
        $stat = @stat(dirname($target));
        $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
        @chmod($target, $dir_perms);
        return true;
    }
    else
    {
        if(is_dir(dirname($target)))
        {
            return false;
        }
    }
 
    // If the above failed, attempt to create the parent node, then try again.
    if ($this->_mkdir(dirname($target)))
    {
        return $this->_mkdir($target);
    }
 
    return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *