2016-11-24 71 views
1

我有CI類圖像的問題,圖像不調整...codeiginter上傳和調整

例如:

控制器

private function _do_upload(){ 
    $config['upload_path']  = 'upload/'; 
    $config['allowed_types']  = 'gif|jpg|png'; 
    $config['max_size']   = 1000; //set max size allowed in Kilobyte 
    $config['max_width']   = 1000; // set max width image allowed 
    $config['max_height']   = 1000; // set max height allowed 
    $config['file_name']   = round(microtime(true) * 1000); 

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

    if($this->upload->do_upload('photo')) //upload and validate{ 
     $config2['image_library'] = 'gd2'; 
     $config2['source_image'] = $this->upload->upload_path; 
     $config2['create_thumb'] = TRUE; 
     $config2['maintain_ratio'] = TRUE; 
     $config2['width']   = 450; 
     $config2['height']   = 500; 

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

     if(!$this->image_lib->resize()){ 
      $data['inputerror'][] = 'photo'; 
      $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); 
      $data['status'] = FALSE; 
     } 
     return $this->upload->data('file_name'); 
    }else{ 
     $data['inputerror'][] = 'photo'; 
     $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error 
     $data['status'] = FALSE; 
     echo json_encode($data); 
     exit(); 
    } 
} 

但我沒有錯誤,我不知道錯誤在哪裏?

回答

0

那麼你有幾個問題...

註釋掉你開{這將破壞你的代碼。

如果($這 - > upload-> do_upload( '照片'))//上傳和驗證{

應該是

if($this->upload->do_upload('photo')) { //upload and validate 

2.下一部分:如果你確實有調整大小的錯誤,你沒有對你的消息做任何事情......所以...

 if(!$this->image_lib->resize()){ 
     $data['inputerror'][] = 'photo'; 
     $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); 
     $data['status'] = FALSE; 
    } 

需要添加json_encode如..

 if(!$this->image_lib->resize()){ 
     $data['inputerror'][] = 'photo'; 
     $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); 
     $data['status'] = FALSE; 
     echo json_encode($data); 
     exit(); 
    } 

關於你調整大小,你就需要提供圖像完整的文件名,而不僅僅是路徑,其中「文件」活...

你source_image是文件上傳到的路徑。不是你想調整圖像的路徑/文件名...

$config2['source_image'] = $this->upload->upload_path; 

所以,如果你加入這行,讓你的完整路徑和文件名,並用它...

$file_data = $this->upload->data(); 
$config2['source_image'] = $file_data['full_path']; 

不那工作更好?

+0

謝謝您的回答,我必須解決但仍然可以上傳圖片但無法調整大小,我不知道別人的錯誤在哪裏? – Anonymous

+0

謝謝TimBrownlaw我的網站已成功運行 – Anonymous

+0

歡迎您:)祝您好運。 – TimBrownlaw