dormbaf90464 2011-02-16 09:14
浏览 9
已采纳

无法在CodeIgniter中调整图像大小

I just started programming a week or two ago, so I apologize for the potential terrible code, but I've been having a hard time figuring this one thing out.
I've managed to get the files to upload to the designated destination, but I just can't get the resize/image manipulation tool to work.

The code that's giving me the problem is the commented out section, and I've already made sure to give the folders the proper privileges. Any help would be outstanding.

I've also been curious about how I would use absolute paths for something like this:

function avatar_update() {
    $config['upload_path'] = './avatars/';
    $config['allowed_types'] = 'jpg';
    $config['max_size'] = '100';
    $config['max_width'] = '100';
    $config['max_height'] = '100';
    $config['file_name'] = time();

    $filename = $config['file_name'];

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload()) {
        $error = $this->upload->display_errors();
        $this->session->set_flashdata('msg', $error);
        $this->load->view('General/header');
        $this->load->view('profile_view', $error);
        $this->load->view('General/footer');

    } else {
        // $this->load->library('image_lib');

        // $resize['image_library'] = 'gd2';
        // $resize['source_image'] = './avatars/'.$filename.'.jpg';
        // $resize['maintain_ratio'] = TRUE;
        // $resize['create_thumb'] =TRUE;
        // $resize['width'] = 50;
        // $resize['height'] = 50;

        // $this->load->library('image_lib', $resize);

        // $this->image_lib->resize();

        $username = $this->session->userdata('username');
        $avatar = array(
            'avatar' => $filename
        );

        $this->db->where('username', $username);
        $this->db->update('users', $avatar);

        $this->db->select();
        $this->db->where('username', $username);
        $query = $this->db->get('users');

        if ($query->num_rows() > 0) {
            $user_details = $query->row_array();

        }

        $this->session->set_userdata($user_details);

        $this->load->view('General/header');
        $this->load->view('profile_view');
        $this->load->view('General/footer');
    }
}
  • 写回答

1条回答 默认 最新

  • duanbi8529 2011-02-22 09:48
    关注

    Your code looks ok to me (except for loading the image library twice!). Try adding a path for the new image into your config array.

    $resize['new_image'] = '/path/to/new_image.jpg';
    

    You shouldn't need it, but it might be worth a try! Also, as per my comment you should wrap the resize() function in conditionals so you can test and echo any errors.

    PS. When you get more used to CI and the MVC model think about moving all your db logic to a model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog