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

これを試して:

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);
    }
}

フル パスは、サーバーにアップロードされるソース イメージを指します。あなたがアイデアを得るelse条件を見てください。ここでの変更点は、ライブラリをロードし、画像をアップロードした後に初期化することです。したがって、からフル パス$this->upload->data()を取得しています。

編集:2つの異なるサイズで画像をトリミングする私の作業機能

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 に答える