ユーザーのプロフィール画像をアップロード、サイズ変更、トリミングするための CodeIgniter スクリプトに本当に苦労しています。最後に、暗号化された名前で画像をアップロードし、サイズを変更して2回保存し(1回はサムネイルとして、もう1回は中サイズの画像として)、元のファイルを削除します。ただし、最初のアップロードのサイズ変更プロセスを機能させることができないため、何かが足りないと思います。私は間違いなくコードのどこかにエラーがあります(コードに従うエラー)が、ここでのロジックの理解にギャップがあると思います。知るか?フィードバックをお待ちしております。
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 のアクセス許可があることを再確認しました (前述のように、アップロードとサイズ変更は実際には 1 点で機能しました)。生成されるエラーは次のとおりです。
Unable to save the image. Please make sure the image and file directory are writable.
source: temp_images/8ee1bab383cf941f34218f7535d5c078.jpg
new: image/profile/