1

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

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(链接)存储在数据库中。无论如何,只是不确定如何处理调整大小。

4

1 回答 1

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;
}
于 2013-05-04T17:26:01.507 回答