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

标签 php image codeigniter file-upload image-manipulation

我真的很难用我的 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() 时,调整大小也起作用了。当我 try catch 错误时,它开始对我大喊大叫。我双重验证了 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/

最佳答案

以前从未真正使用过 CI,无法看到代码的详细信息。

  • 首先,检查您要上传到的文件夹上设置的权限。还要尝试使用完整的服务器路径。
  • 其次,查看您是否启用了 PHP 图像扩展,例如 GD、ImageMagick。

关于php - CodeIgniter 图像上传和调整大小失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497827/

相关文章:

php - 将两个列值合并为一个

css - 如何预加载 css :hover background images

来自 MySQL 的 Android 图片请求

php - 将变量从 Controller 传递到 CodeIgniter 中的 View

php - 无法通过 join with where 子句获取记录

php - 条件开关的问题

php - 为什么我的 MySql 查询运行两次?

php - 如何计算净现值

php - Htaccess 和 php Session 奇怪的结果似乎 [L] 无法正常工作

php - 我无法从 Controller 页面检索值