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

后端 未结 2 727
南方客
南方客 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 '
    error
    '; echo '
    '. $this->upload->display_errors() .'
    '; } 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 '
    success
    '; //then output your message (optional) echo '
    '. $data['upload_data']['file_name'].$aa.' Successfully uploaded.
    '; //pass the data to js echo '
    '. json_encode($data) . '
    '; } }

提交回复
热议问题