Codeigniter - How to upload original image with thumbnail image?

白昼怎懂夜的黑 提交于 2019-12-24 00:10:01

问题


I have this code to file upload in codeigniter:

if(!empty($_FILES['userfile'])){
    $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'] = './public/images/campaign-images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
            $config['max_size'] = '10000';
            //$config['max_width'] = '1024';
            //$config['max_height'] = '768';
            $CI->load->library('upload', $config);
            $CI->upload->do_upload();
            $data = $CI->upload->data();
            $name_array[] = $data['file_name'];
        }
    return $name_array;
}

This code is working perfect with single original image upload but how to upload image as thumbnail image(350 x 250) with resize in different folder.

Any idea how to do with codeigniter library?

Thanks


回答1:


First store the original image and afterwards thumbnail image will upload You just need to include gd2 library to generate the thumbnail image

            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/campaign-images/';
                        $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
                        $config['max_size'] = '10000';
                        //$config['max_width'] = '1024';
                        //$config['max_height'] = '768';
                        $CI->load->library('upload', $config);
                        $CI->upload->do_upload();
                        $data = $CI->upload->data();
                        //Original Image Upload - End

                        //Thumbnail Image Upload - Start
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
                        $config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
                        $config['width'] = 350;
                        $config['height'] = 250;

                        //load resize library
                        $this->load->library('image_lib', $config);
                        $this->image_lib->resize();
                        //Thumbnail Image Upload - End

                        $name_array[] = $data['file_name'];
                    }
                return $name_array;
            }



回答2:


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'] = 'PATH_TO_UPLOAD_IMAGE';
                    $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();
                    //Original Size of Image Upload - End

                    //Thumbnail Size of Image Upload - Start
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = 'PATH_TO_IMAGE_UPLOAD'. $value['name'][$s];
                    $config['new_image'] = 'PATH_TO_UPLOAD_THUMB_IMAGE'.$value['name'][$s];
                    $config['width'] = 350;
                    $config['height'] = 250;

                    //CI load resize library
                    $this->load->library('image_lib', $config);
                    $this->image_lib->resize();
                    //Thumbnail SIZED Image Upload - End

                    $name_array[] = $data['file_name'];
                }
            return $name_array;
        }


来源:https://stackoverflow.com/questions/32864056/codeigniter-how-to-upload-original-image-with-thumbnail-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!