Codeigniter thumbnail array not working

137 views Asked by At

Im doing multiple file uploading, which is working fine. im getting both images but the other image which is thumnail not getting their given path and also not changing in to thumb size.

I have just issues with thumbnail in "thumbnail creation start" area i hope. check my array and let me know my mistake.

controller codes

public function addimage($room_id)
{
    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value)
    for($s=0; $s<=$count-1; $s++) 
    {
        $_FILES['userfile']['name']=$value['name'][$s];
        $_FILES['userfile']['type']    = $value['type'][$s];
        $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
        $_FILES['userfile']['error']       = $value['error'][$s];
        $_FILES['userfile']['size']    = $value['size'][$s];  
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['encrypt_name']  = TRUE;
        $this->load->library('upload', $config);

        $this->upload->do_upload();
        $data = $this->upload->data();
        $name_array[] = $data['file_name']; // get file names for first file

        //thumbnail creation start
        $config1['image_library'] = 'gd2';
        $config1['source_image'] = $image['full_path'];
        $config1['create_thumb'] = TRUE;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config1['maintain_ratio'] = TRUE;
        $config1['upload_path'] = './uploads/thumbs/';
        $config1['width'] = 150;
        $config1['height'] = 150;

        $this->load->library('image_lib', $config1);
        $this->image_lib->resize();
        $this->upload->do_upload();
        $data = $this->upload->data();
        $name_array1[] = $data['file_name']; // file names for thumb
        //thumbnail creation end
    }

    $names= implode(',', $name_array);  
    $names2= implode(',', $name_array1);    
    //print_r($names);
    $options = array( 
        'id' => '0',
        'org_image' => $names,
        'thumbnail' => $names2,
        'room_id' => $room_id,
        'created' => '1',
        'status' => '1'
    );

    $this->rooms_model->room_images_insert($options);
    redirect('/admin/rooms', 'location');
} //end of function
3

There are 3 answers

4
kazimt9 On BEST ANSWER

Replace

$config1['source_image'] = $image['full_path'];    

To

$config1['source_image'] = $data['full_path'];

Also remove the following commented lines

$this->image_lib->resize();
//$this->upload->do_upload();
//$data = $this->upload->data();
3
Sulthan Allaudeen On

After you do config for your image i.e., setting your image attributes then you should clear and initizliae the image_lib to get effect.

$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();

Note :

  1. Make sure that you need it for $config or $config1

  2. Make sure that you have the source_image path right

0
Noushad Ranani On
 public function addimage($room_id)
    {
        $name_array = array();
        $count = count($_FILES['userfile']['size']);
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++) 
        {
            $_FILES['userfile']['name']=$value['name'][$s];
            $_FILES['userfile']['type']    = $value['type'][$s];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['userfile']['error']    = $value['error'][$s];
            $_FILES['userfile']['size']    = $value['size'][$s];  
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['encrypt_name']  = TRUE;
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();
            $name_array[] = $data['file_name']; // get file names for first file

  //thumbnail creation start
            $config1['image_library'] = 'gd2';
            $config1['source_image'] = $data['full_path'];
            $config1['create_thumb'] = TRUE;
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config1['maintain_ratio'] = TRUE;
            $config1['upload_path'] = './uploads/thumbs/';
            $config1['width'] = 500;
            $config1['height'] = 150;

            $this->load->library('image_lib', $config1);
            $this->image_lib->clear();
            $this->image_lib->initialize($config1);
            $this->image_lib->resize();
            $name_array1[] = $data['file_name']; // get file names for thumb file
    //thumbnail creation end
        }

        $names= implode(',', $name_array);  
        $names2= implode(',', $name_array1);    
        //print_r($names);
            $options = array( 
            'id' => '0',
            'org_image' => $names,
            'thumbnail' => $names2,
            'room_id' => $room_id,
            'created' => '1',
            'status' => '1'
                );

            $this->rooms_model->room_images_insert($options);
            redirect('/admin/rooms', 'location');


    } //end of function