0

我无法调整在我的上传模块中成功上传的照片的大小。

我可以将照片上传到适当的文件夹,但是我的调整大小不起作用,我还想同时在同一文件夹中创建缩略图副本。

public function upload()
    {
        $this->load->library('session');
        $this->load->helper('url');
        $session_id = $this->session->userdata('id');
        $this->load->model('account_model');
        $user = $this->account_model->user();
        $data['user'] = $user;
        echo $user['id'];
        $user_folder = './uploads/' . $this->session->userdata('id');
        if(!is_dir($user_folder)){
            mkdir($user_folder, 0777);
        }
        $config['image_library'] = 'gd2';
        $config['source_image'] = $user_folder;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 50;
        $config['height']   = 50;

        $this->load->library('image_lib', $config); 

        $this->image_lib->resize();
        $config['upload_path'] = $user_folder;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '165';
        $config['max_width']  = '165';
        $config['max_height']  = '165';
        $config['overwrite'] = TRUE;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $data['main_content'] = '/account/upload';
            $this->load->view('includes/templates/main_page_template', $data);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $data['main_content'] = '/account/success';
            $this->load->view('includes/templates/main_page_template', $data);
        }
    }
4

2 回答 2

1

Try this:

public function upload()
{
    $this->load->library('session');
    $this->load->helper('url');
    $session_id = $this->session->userdata('id');
    $this->load->model('account_model');
    $user = $this->account_model->user();
    $data['user'] = $user;
    echo $user['id'];
    $user_folder = './uploads/' . $this->session->userdata('id');
    if(!is_dir($user_folder)){
        mkdir($user_folder, 0777);
    }

    $this->load->library('image_lib');                      #load the image manipulation library without initiatlising it here.

    $configThumb['image_library']    = 'gd2';
    //$configThumb['source_image'] = $user_folder;
    $configThumb['create_thumb']     = TRUE;
    $configThumb['maintain_ratio']   = TRUE;
    $configThumb['width']            = 50;
    $configThumb['height']           = 50;



    //$this->image_lib->resize();
    $config['upload_path']      = $user_folder;
    $config['allowed_types']    = 'gif|jpg|png';
    $config['max_size']         = '165';
    $config['max_width']        = '165';
    $config['max_height']       = '165';
    $config['overwrite']        = TRUE;

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $data['main_content'] = '/account/upload';
        $this->load->view('includes/templates/main_page_template', $data);
    }
    else
    {
        $configThumb['source_image']    = $uploadedDetails['full_path'];
        $this->image_lib->initialize($configThumb);                         #initialize the library here
        $this->image_lib->resize();                                         #resize is done here    
        $data = array('upload_data' => $this->upload->data());
        $data['main_content'] = '/account/success';
        $this->load->view('includes/templates/main_page_template', $data);
    }
}

Full path refers to the source image that is uploaded to your server. See the else condition you will get the idea. Now, the change is that we are loading the library and initializing it after uploading the image and hence we are getting the full path from $this->upload->data().

EDIT : MY WORKING FUNCTION TO CROP AN IMAGE IN TWO DIFFERENT SIZES

function udpate_profile($userId = 0){
    $data   = array();
    //echo "<pre>";print_r($_POST);echo "</pre>";
    /* Upload Image */
    if($_FILES['image']['name'] != ""){
        //echo "<pre>";print_r($_FILES);echo "</pre>";
        //echo "enter";die;

        /* Check if previous file exists */
        $chkRs  = $this->db->select('image')->where('id', $this->session->userdata['logged_user']['id'])->get('admins');
        //echo $this->db->last_query();die;
        if($chkRs->num_rows() > 0){
            $chkD   = $chkRs->row_array();
            if($chkD['image'] != ""){
                ### delete the previous image ###
                $pathActual = './profile_images/';
                $pathMedium = './profile_images/medium/';
                $pathThumb  = './profile_images/thumbs/';
                if(file_exists($pathActual.$chkD['image'])){    #delete the actual image
                    unlink($pathActual.$chkD['image']);
                }
                if(file_exists($pathMedium.$chkD['image'])){    #delete the medium image
                    unlink($pathMedium.$chkD['image']);
                }
                if(file_exists($pathThumb.$chkD['image'])){    #delete the thumb image
                    unlink($pathThumb.$chkD['image']);
                }
                ### delete the previous image ###
            }
        }
        /* Check if previous file exists */

        //print_r($_FILES['image']);die;
        $this->load->library('image_lib');

        $configUpload['upload_path']    = './profile_images/';
        $configUpload['allowed_types']  = 'gif|jpg|png|bmp|jpeg';
        $configUpload['max_size']       = '0';
        $configUpload['max_width']      = '0';
        $configUpload['max_height']     = '0';
        $configUpload['encrypt_name']   = true;
        $this->load->library('upload', $configUpload);
        /* size 64*72 for comments */
        $configThumb = array();  
        $configThumb['image_library']   = 'gd2';  
        $configThumb['create_thumb']    = TRUE;
        $configThumb['new_image']       = './profile_images/thumbs/';  
        $configThumb['maintain_ratio']  = TRUE;
        $configThumb['width']           = 64;  
        $configThumb['height']          = 72;
        $configThumb['thumb_marker']    = "";
        //$this->load->library('image_lib');
        /* size 64*72 for comments */

        /* size 167*167 for profile page */
        $configThumbMedium = array();  
        $configThumbMedium['image_library']   = 'gd2';  
        $configThumbMedium['create_thumb']    = TRUE;
        $configThumbMedium['new_image']       = './profile_images/medium/';  
        $configThumbMedium['maintain_ratio']  = TRUE;
        $configThumbMedium['width']           = 167;  
        $configThumbMedium['height']          = 167;
        $configThumbMedium['thumb_marker']    = "";
        /* size 167*167 for profile page */

        if(!$this->upload->do_upload('image')){
            return 0;
        }

        $uploadedDetails    = $this->upload->data();
        if($uploadedDetails['is_image'] == 1){
            $configThumb['source_image']        = $uploadedDetails['full_path'];
            $configThumbMedium['source_image']  = $uploadedDetails['full_path'];
            $raw_name                           = $uploadedDetails['raw_name'];
            $file_ext                           = $uploadedDetails['file_ext']; 
            $imgname                            = $raw_name.$file_ext;
            $this->image_lib->initialize($configThumb);
            $this->image_lib->resize();
            $this->image_lib->initialize($configThumbMedium);
            $this->image_lib->resize();
        }
    }
    //die();
    /* Upload Image */

    $data   = $this->input->post(null);
    //echo "<pre>";print_r($data);echo "</pre>";die;
    if(isset($imgname) && $imgname != ""){
        $data['image']  = $imgname;
    }
    $this->db->where('id',$this->session->userdata['logged_user']['id'])->update('admins', $data);
    return 1;
}
于 2013-07-23T09:55:26.397 回答
0

您可能需要将 source_image 传递给服务器上图像的完整路径。因此,如果您尝试使用 /uploads,那么您可能需要将其设置为 /var/www/html/uploads 或该文件夹的完整路径,因为图像处理是相对于服务器的,而不是网站网址

于 2013-07-22T20:14:41.180 回答