I am uploading max 5 images and i want to create the thumbnails of those 5 images i am successful in uploading and saving image name in database but can't able to make thumbnails.

//controller

        $files = $_FILES;
        $cpt = count($_FILES['uploadfile']['name']);
        for($i=0; $i<$cpt; $i++)
                {
                $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
                $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
                $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
                $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
                $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload('uploadfile');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name']; //success till here now inserting in database and creating thumbnails
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
                }
              $fileName = $images;
              var_dump($images);
              @$form['picture1']=$images[0];
              @$form['picture2']=$images[1];
              @$form['picture3']=$images[2];
              @$form['picture4']=$images[3];
              @$form['picture5']=$images[4];

//

private function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
         $config['remove_spaces']=TRUE;
         $config['encrypt_name'] = TRUE; // for encrypting the name
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size'] = '78000';
         $config['overwrite'] = FALSE;
         return $config;
  }

now i want as 5 images are uploaded in folder at the same time thumbnails of those 5 images are created.

I tried creating thumnails but it only works for single image.Any helps??

Recommended Answers

All 3 Replies

If you're using the resize() method of the image library, it happens because of:

Image_lib class already loaded. Second attempt ignored.

You can see that from the log of CodeIgniter. I don't know if they added something to avoid this problem, I just use this solution:

see the last comment, in practice when you load the library you assign a dynamic name and use that to load the new instance:

private function _resize($source, $filename, $num = 1)
{
    $config['image_library']  = 'gd2';
    $config['source_image']   = $source;
    $config['new_image']      = FCPATH . 'thumbs/' . $filename;
    $config['create_thumb']   = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']          = 75;
    $config['height']         = 50;

    $instance = 'image_lib_'.$num;

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

    return $this->$instance->resize();
}

Then, you can insert this _resize() method in the for loop:

$this->_resize($upload_data['full_path'], $upload_data['file_name'], $i);

And it should work fine.

Just a note: imagine what can happen if N users start to upload images at the same time, they could kill the server, since this is an intensive task for the CPU, if you can, you should consider to go asynchronous and let the server start just few resize processes (no more than a resize process per core). It can be done with tools like beanstalkd or gearman:

This requires access to a ssh session and the privilege to install apps. See example here:

commented: thnks for reply plz see below it's not working yet +0

i just added your code but it still i am getting thumb of one image see below

 public function _resize($source, $filename, $num = 5)
{
    $config['image_library']  = 'gd2';
    $config['source_image']   = $source;
    $config['new_image']      = THUMBPATH.$filename;
    $config['create_thumb']   = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']          = 75;
    $config['height']         = 50;
    $instance = 'image_lib_'.$num;
    var_dump($instance);
    $this->load->library('image_lib', $config, $instance);
    return $this->$instance->resize();


}

then in for loop i called it as

$fileName = $images;
              var_dump($images);
              @$form['picture1']=$images[0];
              @$form['picture2']=$images[1];
              @$form['picture3']=$images[2];
              @$form['picture4']=$images[3];
              @$form['picture5']=$images[4];

` for($i=0; $i<$cpt; $i++) {

                  $this->_resize($upload_data['full_path'], $upload_data['file_name'], $i);
                  var_dump($this->_resize($upload_data['full_path'], $upload_data['file_name'], $i));

                  }
    `

i Solved it thnks for remarkable help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.