首页 文章

Codeigniter文件在上传时调整大小

提问于
浏览
1

我一直在尝试上传和调整图片大小 . 上传似乎终于有效,但它没有调整大小 .

在我的构造函数中,我正在加载库

$this->load->library('image_lib');

然后在我的函数中我调用resize并上传

$this->require_auth();      
    $img = $this->input->post('photo1');

    $config['upload_path'] = './HTML/files/';
    $config['file_name'] = 'photo1.jpg';
    $config['file_path'] = './HTML/files/photo1.jpg';
    $config['max-size'] = 2000;
    $config['image_library'] = 'gd2';
    $config['source_image'] = $config['file_path'];
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = FALSE;
    $config['width']    = 549;
    $config['height']   = 549;
    $config['overwrite'] = TRUE;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['new_image'] = $config['file_path'];
    $this->load->library('image_lib', $config); 
    $this->image_lib->resize();
    $this->load->library('upload', $config);                


    if ( ! $this->upload->do_upload('photo1')){        
       $error = array('error' => $this->upload->display_errors());
       var_dump($error);
     }else{
         $this->blog_model->editServiceImg1($config['file_path']); 
         redirect('/blog');
     }

我也尝试过这个

$this->require_auth();      
    $img = $this->input->post('service_photo1');

    $config['upload_path'] = './HTML/files/';
    $config['file_name'] = 'service_photo1.jpg';
    $config['file_path'] = './HTML/files/service_photo1.jpg';
    $config['max-size'] = 2000;
    $config['overwrite'] = TRUE;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);                


    if ( ! $this->upload->do_upload('service_photo1')){        
       $error = array('error' => $this->upload->display_errors());
       var_dump($error);
     }else{
        $config['image_library'] = 'gd2';
        $config['source_image'] = $config['file_path'];
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['width']    = 549;
        $config['height']   = 549;
        $config['new_image'] = $config['file_path'];
        $this->image_lib->initialize($config);
        $this->image_lib->resize();


         $this->blog_model->editServiceImg1($config['file_path']); 
         redirect('/blog');
     }

有什么我想念的吗?我也尝试过在初始上传后调整大小,这似乎也没有做任何事情 . 任何帮助将非常感激 .

2 回答

  • 0
    $this->load->library('image_lib');
    $config['image_library'] = 'gd2';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['upload_path'] = './HTML/files/';
    $config['file_path'] = './HTML/files/photo1.jpg';
    $config['source_image'] = $config['file_path'];
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']     = 75;
    $config['height']   = 50;
    
    $this->image_lib->clear();
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    

    如果上述代码无效,请检查上传图像的文件夹权限 .

    您还可以通过以下方式为该文件夹授予777权限: -

    sudo chmod -R 777 path

    解决方案一定会对您有所帮助

  • 0

    我多次查看这段代码,最后解决了这个问题 . 看来,当你有

    $config['create_thumb'] = TRUE;
    

    在你的代码中,它将创建一个缩放比例的缩略图,如果你不告诉它去哪里,它会将它隐藏在一个文件夹中 . 因此调整大小正在工作,而不是正确的图像 .

    要解决这个问题,我将上面改为 .

    $config['create_thumb'] = FALSE;
    

相关问题