图片调整大小codeigniter问题不起作用

问题描述:

我一直在尝试调整图像大小,但没有运气!我在上传功能中调整大小,但不知道发生了什么错误!下面的代码显示了我的上传功能(和内调整其大小):图片调整大小codeigniter问题不起作用

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,只是不知道如何处理调整大小。

+0

你的代码看起来是正确的..让粟特'$ fullImagePath'是正确的,并指向您的imahe文件.. – bipen 2013-05-04 17:25:21

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; 
} 
+0

都能跟得上,不执行任何操作。与上传的尺寸完全相同。 – 2013-05-04 17:29:00

+0

确保'$ fullImagePath'指向正确的路径。 – 2013-05-04 17:31:16

+0

它与图像的src使用的路径相同。所以,是它是正确的,而不是空 – 2013-05-04 17:36:15