2010-12-01 105 views
3

我花了好幾天的時間嘗試基於文檔中的示例進行這項工作,但我錯過了一些東西或者我只是笨蛋!CodeIgniter上傳和調整大小問題

我有一個CMS應用程序,其中用戶上傳圖像以非常固定的佈局顯示。我們不希望限制上傳圖像的文件大小,而是希望在它到達後「處理」它。

圖像需要寬度爲615px,但是一些從數碼相機直接上傳的圖像是2500X2000或更大,所以這是非常重要的。

我將手冊中的代碼拼湊在一起,並且圖像已成功上傳到CMS應用程序內的文件夾中。但是,圖像不被調整大小。

如果我把它重新調整大小,我的計劃是將圖像呈現給用戶使用jCrop裁剪(最終圖像爲615X275,並且可能必須在調整大小後裁剪高度),然後使用codeigniter將圖像通過原始名稱FTP到其網站的amenities文件夾中。

我將不勝感激任何幫助!

這裏是我的代碼:

 
function do_feature_upload() { 
     $imageName = $this->uri->segment(3); 
     //echo $imageName; 

     // Where the file is going to be placed 
     $config['upload_path'] = "./uploads/".$_SESSION['dbPropNumber']; 
     $config['allowed_types'] = 'jpg|jpeg'; 
     $config['max_size'] = '0'; 
     $config['file_name'] = $imageName.'.jpg'; 
     $config['overwrite'] = 'TRUE'; 

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

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

      $error['propertyDropdown'] = $_SESSION['propertyDropdown']; 
      $error['username'] = $_SESSION['username']; 
      $error['dbPropNumber'] = $_SESSION['dbPropNumber']; 
      $error['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); 

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

      $origWidth = $image_data['image_width']; 
      $origHeight = $image_data['image_height']; 
      $newWidth = 615; 
      $newHeight = $newWidth*$origHeight/$origWidth; 

      $resize = array(
       'image_library'=>'gd2', 
       'source_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'.jpg', 
       'new_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'1.jpg', 
       'create_thumb' => FALSE, 
       'maintain_ratio'=>FALSE, 
       'width'=>$newWidth, 
       'height'=>$newHeight 
      ); 

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

      $data = array('upload_data' => $this->upload->data()); 
      $data['propertyDropdown'] = $_SESSION['propertyDropdown']; 
      $data['username'] = $_SESSION['username']; 
      $data['dbPropNumber'] = $_SESSION['dbPropNumber']; 
      $data['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); 

      //Present jCrop option after image is resized 

      // FTP to final destination 

      $this->load->view('upload_success', $data); 
     } // end if 
    } // end function 
+0

嘗試刪除調整大小函數中的base_url()。如果我是正確的路徑(source_image和new_image)必須是相對不絕對 – trix 2010-12-01 17:51:47

+0

工作像一個魅力! !!!謝謝!!! – jgravois 2010-12-01 18:02:39

回答

6

我不完全知道是什麼你的代碼會錯,但這裏是我寫的調整圖像大小,以適應一個確切的目標高度目標寬度的借鑑作用。通讀它,看看你是否無法找出解決方案。

$this->prefix是我使用的類中的一個屬性,所以我不必一直寫出文件的目錄。它看起來像這樣:

$this->prefix = FCPATH.'uploads'.DIRECTORY_SEPARATOR;

圖像縮放

/** 
* Resizes an image to fit exact dimensions 
* 
* @param string filename 
* @param int  target_width 
* @param int  target_height 
* 
* @return array('success' ? null : 'error') 
*/ 
function resizeImageToDimensions($filename, $target_width=700, $target_height=399) 
{ 
    $file_type = $this->getFileType($this->prefix.$filename); 

    if (!$file_type || $file_type != 'image') 
     return array('success'=>false, 'error'=>"This file doesn't exist or isn't an image"); 

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

    list($width, $height) = getimagesize($this->prefix.$filename); 
    $current_ratio = $width/$height; 
    $target_ratio = $target_width/$target_height; 
    $config['source_image'] = $this->prefix.$filename; 

    if ($current_ratio > $target_ratio) 
    { 
     //resize first to height, maintain ratio 
     $config['height'] = $target_height; 
     $config['width'] = $target_height * $current_ratio; 
     $this->image_lib->initialize($config); 

     if (!$this->image_lib->resize()) 
      return array('success'=>false, 'error'=>"There was an error while resizing this image"); 

     //then crop off width 
     $config['width'] = $target_width; 
     $config['maintain_ratio'] = false; 
     $this->image_lib->initialize($config); 

     if ($this->image_lib->crop()) 
      return array('success'=>true); 
     else 
      return array('success'=>false, 'error'=>"There was an error while cropping this image"); 
    } 
    else if ($current_ratio < $target_ratio) 
    { 
     //resize first to width, maintain ratio 
     $config['width'] = $target_width; 
     $config['height'] = $target_width/$current_ratio; 
     $this->image_lib->initialize($config); 

     if (!$this->image_lib->resize()) 
      return array('success'=>false, 'error'=>"There was an error while resizing this image"); 

     //then crop off height 
     $config['height'] = $target_height; 
     $config['maintain_ratio'] = false; 
     $this->image_lib->initialize($config); 

     if ($this->image_lib->crop()) 
      return array('success'=>true); 
     else 
      return array('success'=>false, 'error'=>"There was an error while cropping this image"); 
    } 
    else { 
     $config['width'] = $target_width; 
     $config['height'] = $target_height; 
     $this->image_lib->initialize($config); 

     if ($this->image_lib->resize()) 
      return array('success'=>true); 
     else 
      return array('success'=>false, 'error'=>"There was an error while resizing this image"); 
    } 
} 
1

我有同樣的問題,幾個月前和可悲的是,我無法弄清楚。我已發佈相同的問題hereCodeIgniter's forums,但沒有人能幫助我。

我結束了使用timthumb腳本,這是偉大的,但遠不理想:(

所以,如果你在趕時間,我會強烈建議使用timthumb。如果你有一段時間投資我希望你最好,並請分享!