图像调整大小codeigniter问题无法正常工作

时间:2013-05-04 17:07:21

标签: codeigniter codeigniter-2

我一直试图调整图片大小,但没有运气!我在上传功能中有调整大小但不确定发生了什么错误!下面的代码显示了我的上传功能(并在其中调整大小):

function do_upload()
    {
        $config['upload_path']   = './uploads/';        
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '2048';




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


        $this->load->library('upload', $config);
        $fullImagePath = '';
              if (! $this->upload->do_upload())
              {
                            $this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');

                   $error = array('file_error' => $this->upload->display_errors());
                  // print_r($error);

                   $this->load->view('layout/header');
                   $this->load->view('add_gallery', $error);
                   $this->load->view('layout/footer');

              }else{
                    //echo "UPLOAD SUCCESS";
                  // set a $_POST value for 'image' that we can use later
                        /*Image Manipulation Class*/

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $fullImagePath;
                    echo $fullImagePath;
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['max_width'] = '480';
                    $config['max_height'] = '640';
                    $this->load->library('image_lib', $config); 
                    $this->image_lib->resize();


                  $upload_data    = $this->upload->data();
                  $fullImagePath = '/uploads/' . $upload_data['file_name']; 
              }



        return $fullImagePath;
    }

上传工作正常,我得到fullImagePath(链接)存储在数据库中。 ANyway,只是不确定如何处理调整大小。

1 个答案:

答案 0 :(得分:0)

resize()函数要求您在调用之前首先配置source_image,以便它可以将调整大小应用于指定的图像。

您正在发送一条空路径。 在这里,您将路径声明为空字符串$fullImagePath = '';,然后在此处将其定义为配置选项$config['source_image'] = $fullImagePath;,然后调用$this->image_lib->resize();,这样就无法调整大小为空图像

此外,您正在加载两次不需要的image_lib

我修改了你的代码。测试它,看它是否有效

function do_upload()
{
    $config['upload_path']   = './uploads/';        
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2048';
    $this->load->library('upload', $config);
    $fullImagePath = '';
  if (! $this->upload->do_upload()){
        $this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
        $error = array('file_error' => $this->upload->display_errors());
        $this->load->view('layout/header');
        $this->load->view('add_gallery', $error);
        $this->load->view('layout/footer');
  }else{
        $upload_data = $this->upload->data();
        $fullImagePath = '/uploads/' . $upload_data['file_name']; 
        $config['image_library'] = 'gd2';
        $config['source_image'] = $fullImagePath;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['max_width'] = '480';
        $config['max_height'] = '640';
        $config['width'] = 75;
        $config['height'] = 50;
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize();
  }

return $fullImagePath;
}