2012-02-01 105 views
0

我正在爲網站的媒體庫目前工作,其中一個功能是用戶可以創建他們上傳的圖像的裁剪版本。圖像裁剪與codeigniter否GD庫 - GD2已安裝

但是我的問題是這樣的,當我嘗試裁剪圖像,我得到以下錯誤,

The path to the image is not correct. 

這裏是我的代碼,

$config['image_library'] = 'gd2'; 
    $config['source_image'] = "/media/images/products/".$this->data['image']; 
    //die($config['source_image']); 
    $config['maintain_ratio'] = FALSE; 
    $config['x_axis'] = $this->input->post('x'); 
    $config['y_axis'] = $this->input->post('y'); 
    $config['width'] = $this->input->post('w'); 
    $config['height'] = $this->input->post('h'); 
    $config['dynamic_output'] = FALSE; 
    $config['create_thumb'] = TRUE; 
    $this->load->library('image_lib', $config); 

    if(!$this->image_lib->crop()) { 
     if($this->input->post('isAjax') == "1") { 
      echo json_encode($this->image_lib->display_errors()); 
     } else { 
      $this->data['image_error'] = $this->image_lib->display_errors(); 
      $this->template->build('/admin/media/crop', $this->data); 
     }  
    } else { 
     $filename = base_url()."media/images/products/".$this->data['image']; 
     $extension_pos = strrpos($filename, '.'); // find position of the last dot, so where the extension starts 
     $thumb = substr($filename, 0, $extension_pos) . '_thumb' . substr($filename, $extension_pos); 
     if($this->input->post('isAjax') == 1) { 
      echo json_encode($success = array('message' => 'Image Cropped')); 
     } else { 
      $this->data['success'] = "Image Cropped"; 
      $this->template->build('/admin/media/crop', $this->data); 
     } 
    } 

所以我雖然我會改變$config['source_image']以下內容,

$config['source_image'] = "./media/images/products/".$this->data['image']; 

然而,僅僅留下與此消息,

Your server does not support the GD function required to process this type of image. 

我在做一些根本性的錯誤嗎?我只是試圖裁剪一個簡單的.png,並且該文件當然存在於我的服務器上,而且我最明確地安裝了GD2。

+0

GD2可能已安裝,但它是否支持PNG編譯?僅僅因爲GD在那裏並不意味着它可以處理所有通常的文件格式。 – 2012-02-01 17:40:24

+0

您是否嘗試過從文檔根目錄的絕對路徑?試試'$ _SERVER ['DOCUMENT_ROOT']。 'rest/of/path/to/file'' – 2012-02-01 17:41:12

+0

GD_INFO會告訴它支持什麼?我在這臺機器上預先裁剪過PNG。 – Udders 2012-02-01 17:41:49

回答

0

這很可能只是一個文件路徑問題(CI對於準確的相關錯誤消息非常好)。下面是我將採取的步驟來解決它:

  1. var_dump(file_exists($config['source_image'])看看PHP是否找到該文件。這裏我會被一個「真實」的迴應震驚。
  2. 如果文件系統是大小寫敏感的,確保這不是問題
  3. 檢查包括路徑(我假設定期包括如果CI得到這一步工作正常...)
  4. 更多的反步,但請注意,不要使用$_SERVER['DOCUMENT_ROOT']FC_PATH(或其他CI定義的常量)應該爲您提供所需的基本路徑。

快樂的狩獵。

+0

file_exists返回TRUE,還有其他想法嗎? – Udders 2012-02-02 10:37:20

+0

真是莫名其妙。也許可以在相關的圖像處理函數中加入一些逐步的調試輸出來找出它到達錯誤狀態的位置? – landons 2012-02-02 23:05:30

+0

想到了,我提交表單到'controller/view',但'$ this-> data ['image']'是從URI段構建的,所以需要將表單提交給'current_url()' – Udders 2012-02-03 09:43:49