首页 文章

CodeIgniter图像上传和调整大小失败

提问于
浏览
1

我正在努力使用我的CodeIgniter脚本来为用户上传,调整大小和裁剪 Profiles 图像 . 最后,我想上传带有加密名称的图像,调整大小并保存两次(一次作为缩略图,一次作为中等大小的图像),然后删除原始文件 . 但是,我想我真的错过了一些东西,因为我无法让第一个上传调整大小的过程发挥作用 . 我在代码中的某个地方肯定有错误(跟随代码的错误),但我认为我对逻辑的理解可能存在差距 . 谁知道?任何反馈都非常感谢 .

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'] . '
'; echo 'new: ' . $config['new_image'] . '
'; } 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/

3 回答

  • 1

    之前从未真正使用过CI,也无法查看代码的详细信息 .

    • 首先,检查您要尝试上传到的文件夹上设置的权限 . 还尝试使用完整的服务器路径 .

    • 其次,查看是否有启用了ImageMagick的PHP图像扩展名 .

  • 0

    我试过这个tutorial

    它适用于图像上传和为调整大小图像创建拇指文件夹...

  • 1

    请通过包含文件名来更新new_image值 .

    $config['new_image'] = 'image/profile/' . $filename;
    

相关问题