Codeigniter文件在上传时调整大小

时间:2014-10-11 14:43:29

标签: php codeigniter

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

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

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

然后在我的功能中,我调用了调整大小并上传

    $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 :(得分: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

解决方案肯定会帮助你

答案 1 :(得分:0)

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

$config['create_thumb'] = TRUE;

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

要解决此问题,我将上述内容更改为。

$config['create_thumb'] = FALSE;