Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: Unable to create thumbnail for multiple images

I am trying upload multiple images(3) at a time and create thumbnail for each image. But my code upload 3 images and create only 1 thumbnail(thumbnail of 1st image). How to create thumbnail of multiple images? Controller: uploadImage function and create thumbnail function

function uploadImage()
{
  if($this->validate()==TRUE) {
    $config['upload_path']   =   "images/uploads/";
    $config['allowed_types'] =   "gif|jpg|jpeg|png"; 
    $config['max_size']      =   "5000";
    $config['max_width']     =   "1907";
    $config['max_height']    =   "1280";

    $this->load->library('upload', $config);

    foreach ($_FILES as $key => $value) {

      if (!empty($value['tmp_name'])) {
        if ( ! $this->upload->do_upload($key)) {
          $error = array('error' => $this->upload->display_errors());
          //failed display the errors
        }     
        else {
          //success
          $finfo=$this->upload->data();
          $this->_createThumbnail($finfo['file_name']);
          $data['uploadInfo'] = $finfo;
          $data['thumbnail_name'] = $finfo['raw_name']. '_thumb' .$finfo['file_ext']; 
        }
      }
    }
  }
}

//Create Thumbnail function
function _createThumbnail($filename)
{
  $config['image_library']    = "gd2";      
  $config['source_image']     = "images/uploads/" .$filename;      
  $config['create_thumb']     = TRUE;      
  $config['maintain_ratio']   = TRUE;      
  $config['width'] = "80";      
  $config['height'] = "80";
  $this->load->library('image_lib',$config);

  if(!$this->image_lib->resize()) {
    echo $this->image_lib->display_errors();
  }      
}
like image 563
mridul Avatar asked Mar 20 '23 08:03

mridul


1 Answers

Do some changes in createthumbnail function according to this link.

Instead of

$this->load->library('image_lib',$config); use

$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();

New createThumbnail function:

//Create Thumbnail function
function _createThumbnail($filename)
{
    $this->load->library('image_lib');
    // Set your config up
    $config['image_library']    = "gd2";      
    $config['source_image']     = "images/uploads/" .$filename;      
    $config['create_thumb']     = TRUE;      
    $config['maintain_ratio']   = TRUE;      
    $config['width'] = "80";      
    $config['height'] = "80";

    $this->image_lib->initialize($config);
    // Do your manipulation

    if(!$this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
    } 
    $this->image_lib->clear();     
}
like image 166
mridul Avatar answered Apr 10 '23 08:04

mridul