Multiple Image Resizes at once in Codeigniter , won't work

后端 未结 2 728
南方客
南方客 2021-01-17 02:54

I am trying to upload images and re-size them in different dimensions within the same function. but what happens is that only one re-size works and the others don\'t . My co

相关标签:
2条回答
  • 2021-01-17 03:14

    this actually worked for me. What i was doing was loading the library multiple times. i should have loaded the library once and initialize the configurations for each re-sizes. here is the code that worked for me.

        function do_upload()
    {
        $this_user = $this->auth->info;
        if(!is_dir('./uploads/'.$this_user->username)){
            mkdir('./uploads/'.$this_user->username);
            mkdir('./uploads/'.$this_user->username.'/photos');
            mkdir('./uploads/'.$this_user->username.'/photos/master');
            mkdir('./uploads/'.$this_user->username.'/photos/small');
            mkdir('./uploads/'.$this_user->username.'/photos/medium');
            mkdir('./uploads/'.$this_user->username.'/photos/large');
            mkdir('./uploads/'.$this_user->username.'/photos/xlarge');
        }
        $config['upload_path'] = './uploads/'.$this_user->username.'/photos/master/';
        $config['allowed_types'] = 'gif|jpg';
        $title = $this->input->post('title');
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload())
        {
            echo '<div id="status">error</div>';
            echo '<div id="message">'. $this->upload->display_errors() .'</div>';
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            //resizing begins
                $image_width = $data['upload_data']['image_width'];
                $image_height = $data['upload_data']['image_height'];
                $full_path = $data['upload_data']['full_path'];
                //checking for width
                $this->load->library('image_lib');
                if($image_width>5000){
                    $config['source_image'] = $full_path;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 5000;
                    $config['new_image'] = './uploads/'.$this_user->username.'/photos/xlarge';
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    $aa = 'xlarge ';
                }
                if($image_width>=4500){
                    $config['source_image'] = $full_path;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 4500;
                    $config['new_image'] = './uploads/'.$this_user->username.'/photos/large';
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    $aa .= 'large';
                }
                if($image_width>=2000){
                    $config['source_image'] = $full_path;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 2000;
                    $config['new_image'] = './uploads/'.$this_user->username.'/photos/medium';
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    $aa .= 'medium';
                }
                if($image_width>=800){
                    $config['source_image'] = $full_path;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 800;
                    $config['new_image'] = './uploads/'.$this_user->username.'/photos/small';
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    $aa .= 'small';
                }
            //resizing ends
            echo '<div id="status">success</div>';
            //then output your message (optional)
            echo '<div id="message">'. $data['upload_data']['file_name'].$aa.' Successfully uploaded.</div>';
            //pass the data to js
            echo '<div id="upload_data">'. json_encode($data) . '</div>';
    
        }
    }
    
    0 讨论(0)
  • 2021-01-17 03:22

    You will need to make sure that you call $this->image_lib->clear(); as it resets the initialization of the image manipulation class. See: http://codeigniter.com/user_guide/libraries/image_lib.html

    0 讨论(0)
提交回复
热议问题