我真的很努力與我的CodeIgniter腳本爲用戶上傳,調整大小和裁剪配置文件圖像。最後,我想用一個加密名稱上傳圖像,調整大小並保存兩次(一次作爲縮略圖,一次作爲中等大小的圖像),然後刪除原始文件。但是,我想我真的錯過了一些東西,因爲我無法獲得第一個上傳調整大小的過程。我肯定在代碼的某個地方有錯誤(跟在代碼後面的錯誤),但我想我可能在我對這裏的邏輯的理解上存在差距。誰知道?任何反饋都非常感謝。CodeIgniter的圖片上傳和調整大小失敗
public function image() {
$config['upload_path'] = './temp_images/'; // This directory has 777 access
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0'; // For unlimited width
$config['max_height'] = '0'; // For unlimited height
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if (! $this->upload->do_upload())
{
$data['error'] = array('error' => $this->upload->display_errors());
$this->load->view('upload', $data);
}
else
{
$image_data_array = array('upload_data' => $this->upload->data());
foreach ($image_data_array as $image_data){
$raw_name = $image_data['raw_name'];
$file_ext = $image_data['file_ext'];
}
$file_name = $raw_name . $file_ext;
$image_path = 'temp_images/' . $file_name;
$new_path = 'image/profile/'; // This directory has 777 access
$config['image_library'] = 'gd';
$config['source_image'] = $image_path;
$config['new_image'] = $new_path;
$config['maintain_ratio'] = FALSE;
$config['width'] = 140;
$config['height'] = 140;
$config['quality'] = '80%';
$new_name = $new_path . $raw_name . $file_ext;
$this->load->library('image_lib', $config);
// The following is just to test that it worked:
if (! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
echo 'source: ' . $config['source_image'] . '<br />';
echo 'new: ' . $config['new_image'] . '<br />';
}
else {
echo "<img src='" . base_url() . $new_name . "' />";
}
$this->image_lib->clear();
}
}
上傳過程正常。即使調整大小,我剛剛打電話$this->image_lib->resize()
。當我嘗試錯誤捕捉時,它開始對我大喊大叫。我雙重驗證了temp_images和image/profile都具有777權限(如前所述,上載和調整大小實際上在一個點上工作)。這裏是產生的錯誤:
Unable to save the image. Please make sure the image and file directory are writable.
source: temp_images/8ee1bab383cf941f34218f7535d5c078.jpg
new: image/profile/
當140x140建立映像$配置[「new_image」 ]必須是圖像的完整路徑,包括文件名「image/profile/$ filename」。 $ filename就像「image.jpg」 – user2844810 2015-05-07 19:25:00