Monday 24 September 2012

Uploading image and generating thumbnail in Codeigniter

In this page Iam explaining how to upload image using codeigniter.
Before doing the code make sure that you created afolder named uploads in the rootfolder. and also a folder named thumbs inside this uploads folder.
So in the file uploading html page code should be like this:-

<?php echo form_open_multipart('admin/imageadd'); ?>
 <div class="row">
                            <div class="row150">
                                <h6>Image
                                <span class="req">*</span></h6>
                            </div>
                            <div class="row350">
                                <input type="file" name="userfile"  size="40" maxlength="90" tabindex="3" class="browse"/>
                            
                            </div>
                        </div>

<input name="saveForm" type="submit" value="." class="iconSave" />

                        
In the controller page code should be like this:-

public function imageadd(){
$save=$this->input->post('saveForm');
if($save){
$config['upload_path'] = 'uploads/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';

$this->load->library('upload', $config); 
if(!$this->upload->do_upload()) 
$this->upload->display_errors(); 
else { 
$fInfo = $this->upload->data(); //uploading
  $this->gallery_path = realpath(APPPATH . '../uploads');//fetching path

$config1 = array(
      'source_image' => $fInfo['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', $config1); //load library
    $this->image_lib->resize(); //generating thumb
}

$imagename=$fInfo['file_name'];// we will get image name here
}

After this if u look at the uploads folder you can see the image file there and its thubnail image inside the thumb folder...

No comments:

Post a Comment