2014-10-11 118 views
0

我想讓用戶上傳他們的個人資料圖片時,它會將其大小調整爲180x180的大小以適合個人資料圖片框。然後,我希望它也創建圖像的縮略圖,所以我可以用它的帖子等,這是我的代碼現在所擁有的:CodeIgniter - 縮放圖像並創建其他縮略圖

function do_upload_profilepicture() 
{ 

    $this->load->model('model_users'); 
    $userID = $this->model_users->getUserID($this->session->userdata('username')); 

    $config['upload_path'] = './img/profilepictures/'; 
    $config['allowed_types'] = 'jpg|png'; 
    $config['overwrite'] = TRUE; 
    $config['file_name'] = $userID; 
    $config['max_size'] = '500'; 
    $config['max_width'] = '1920'; 
    $config['max_height'] = '1028'; 

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


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

     $this->load->view('upload_profilepic_form', $error); 
    } 
    else 
    { 
     $upload_data = $this->upload->data(); 

     $resize['image_library'] = 'gd2'; 
     $resize['source_image'] = $upload_data['full_path']; 
     $resize['maintain_ratio'] = FALSE; 
     $resize['width']  = 180; 
     $resize['height'] = 180; 

     $this->load->library('image_lib', $resize); 
     $this->image_lib->resize();  
     $this->model_users->setProfilePic($userID, $upload_data['orig_name']); 

     redirect('upload/create_thumb/' . $upload_data['orig_name']); 
    } 
} 

function create_thumb() { 
    $this->load->model('model_users'); 
    $userID = $this->model_users->getUserID($this->session->userdata('username')); 

    $imgname = $this->model_users->parseURL($_SERVER['REQUEST_URI'], 1); 

    $source_path = $imgsrc; 
    $config_manip = array(
     'image_library' => 'gd2', 
     'source_image' => 'img/profilepictures/' . $imgname, 
     'new_image' => base_url() . 'img/profilepictures/thumbs/' . $imgname, 
     'maintain_ratio' => TRUE, 
     'width' => 50, 
     'height' => 50 
    ); 
    $this->load->library('image_lib', $config_manip); 

    if (!$this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
    } 
    // clear // 
    $this->image_lib->clear(); 

     redirect('userprofile/home/' . $userID); 
    } 

} 

什麼情況是它不是創建新的文件,這是爲什麼這個?有沒有更簡單的方法來做到這一點?

回答

0

在代碼中發現的一個問題是,您只是爲了創建縮略圖而重定向用戶,您可以在步驟中調用do_upload_profilepicture方法中的該方法並傳遞圖像名稱,無需一次又一次重定向。

function do_upload_profilepicture() 
{ 

    $this->load->model('model_users'); 
    $userID = $this->model_users->getUserID($this->session->userdata('username')); 

    $config['upload_path'] = './img/profilepictures/'; 
    $config['allowed_types'] = 'jpg|png'; 
    $config['overwrite'] = TRUE; 
    $config['file_name'] = $userID; 
    $config['max_size'] = '500'; 
    $config['max_width'] = '1920'; 
    $config['max_height'] = '1028'; 

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


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

     $this->load->view('upload_profilepic_form', $error); 
    } 
    else 
    { 
     $upload_data = $this->upload->data(); 

     $resize['image_library'] = 'gd2'; 
     $resize['source_image'] = $upload_data['full_path']; 
     $resize['maintain_ratio'] = FALSE; 
     $resize['width']  = 180; 
     $resize['height'] = 180; 

     $this->load->library('image_lib', $resize); 
     $this->image_lib->resize();  
     $this->image_lib->clear(); 
     $this->model_users->setProfilePic($userID, $upload_data['orig_name']); 


     $this->create_thumb($upload_data['orig_name']); 

     redirect('userprofile/home/' . $userID); 
    } 
} 

function create_thumb($imgname) { 

    $source_path = $imgsrc; 
    $config_manip = array(
     'image_library' => 'gd2', 
     'source_image' => 'img/profilepictures/' . $imgname, 
     'new_image' => 'img/profilepictures/thumbs/' . $imgname, 
     'maintain_ratio' => TRUE, 
     'width' => 50, 
     'height' => 50 
    ); 
    $this->load->library('image_lib', $config_manip); 

    if (!$this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
    } 
    // clear // 
    $this->image_lib->clear(); 

    } 

} 

你也在new_image中使用base_url拇指你需要給文件路徑不是http路徑。我建議你使用這個庫在codeigniter中動態創建圖像codeigniter-advanced-images