Adding the following method as custom helper file and check the below sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
//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