我真的在为我的 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/