Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - Facing issue with multiple thumbnail creation

I am playing with Codeigniter image upload library and uploading multiple image at once and I am uploading original images with thumbs.

Now If I will upload three images, it will upload all three original images to this folder: project-images. and thumbnail image goes to thumbs folder. Everything is good but I am getting only one image in thumbs folder. 2 are not uploading inside thumbs folder.

What's wrong with my current code.

Any Idea?

if(!empty($_FILES['userfile'])){
    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key => $value)
        for ($s=0; $s<=$count-1; $s++)
        {
            // Original Image Upload - Start
            $_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'] = './public/images/project-images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
            $config['max_size'] = '10000';
            $CI->load->library('upload', $config);
            $CI->upload->do_upload();
            $data = $CI->upload->data();
            $name_array[] = $data['file_name'];
            // Original Image Upload - End

            // Thumbnail Image Upload - Start
            $config2['image_library'] = 'gd2';
            $config2['source_image'] = $CI->upload->upload_path.$CI->upload->file_name;
            $config2['new_image'] = './public/images/project-images/thumbs';
            $config2['maintain_ratio'] = TRUE;
            $config2['create_thumb'] = TRUE;
            $config2['thumb_marker'] = '_thumb';
            $config2['width'] = 370;
            $config2['height'] = 200;
            $CI->load->library('image_lib',$config2);

            if(!$CI->image_lib->resize()){
                $CI->session->set_flashdata('errors', $CI->image_lib->display_errors('', ''));
            }

            // [ MAIN IMAGE ]
            $config['image_library'] = 'gd2';
            $config['source_image'] = $CI->upload->upload_path.$CI->upload->file_name;
            $config['maintain_ratio'] = TRUE;
            $CI->load->library('image_lib',$config);
            // Thumbnail Image Upload - End
        }
    return $name_array;
}
like image 429
Mr.Happy Avatar asked Dec 11 '25 12:12

Mr.Happy


1 Answers

finally I got solution by adding this code before $CI->image_lib->resize():

$CI->image_lib->clear();
$CI->image_lib->initialize($config2);
like image 85
Mr.Happy Avatar answered Dec 14 '25 04:12

Mr.Happy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!