codeiginter上传和调整大小

时间:2016-11-24 20:21:51

标签: ajax codeigniter continuous-integration

CI类图像有问题,图片没有调整大小......

例如:

控制器

private function _do_upload(){
    $config['upload_path']        = 'upload/';
    $config['allowed_types']      = 'gif|jpg|png';
    $config['max_size']           = 1000; //set max size allowed in Kilobyte
    $config['max_width']          = 1000; // set max width image allowed
    $config['max_height']         = 1000; // set max height allowed
    $config['file_name']          = round(microtime(true) * 1000);

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

    if($this->upload->do_upload('photo')) //upload and validate{
         $config2['image_library']   = 'gd2';
         $config2['source_image']   = $this->upload->upload_path;
         $config2['create_thumb']    = TRUE;
         $config2['maintain_ratio']  = TRUE;
         $config2['width']           = 450;
         $config2['height']          = 500;

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

         if(!$this->image_lib->resize()){
             $data['inputerror'][] = 'photo';
             $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
             $data['status'] = FALSE;
        }
        return $this->upload->data('file_name');   
   }else{
        $data['inputerror'][] = 'photo';
        $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
        $data['status'] = FALSE;
        echo json_encode($data);
        exit();
   }
}

但我没有错误,我不知道错误在哪里?

1 个答案:

答案 0 :(得分:0)

你有几个问题......

1。您已注释掉了您的代码{,这会破坏您的代码。

  

if($ this-> upload-> do_upload(' photo'))//上传并验证{

应该是

if($this->upload->do_upload('photo')) { //upload and validate

2。下一部分:如果您确实出现了调整大小错误,则表示您没有对您的消息做任何事情......所以......

     if(!$this->image_lib->resize()){
         $data['inputerror'][] = 'photo';
         $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
         $data['status'] = FALSE;
    }

需要添加json_encode,如..

     if(!$this->image_lib->resize()){
         $data['inputerror'][] = 'photo';
         $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
         $data['status'] = FALSE;
         echo json_encode($data);
         exit();
    }

在您调整大小时,您需要提供图像完整文件名,而不仅仅是文件"文件"住...

您的source_image是文件上传到的路径。不是要调整大小的图像的路径/文件名...

$config2['source_image']   = $this->upload->upload_path;

因此,如果您添加此行,为您提供完整路径和文件名并使用它...

$file_data = $this->upload->data();
$config2['source_image']   = $file_data['full_path'];

那会更好吗?