我正在尝试上传图片并调整大小,到目前为止上传工作正常,但我的图片没有调整大小

时间:2017-06-11 16:19:54

标签: php codeigniter

  1. //我的控制器:

    public function infra_product() { 
      if($this->session->userdata('user_name') == TRUE)  {
      $data['user_name']=$this->session->userdata('user_name');      
    
      if(isset($_POST['sub1']))
      {
        $this->load->model('student_reg');
        $data['cat_id']=$_POST['cat_id'];
    
        if ($data['cat_id']=="1")
        {
          $loc='C:/xampp/htdocs/mahata/uploads/';     $files = $_FILES;       $cpt = count($_FILES['userfile']['name']);
    
          for($i=0; $i<$cpt; $i++)    {
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];    
    
            $name = $_FILES["userfile"]["name"][$i];
            $ext = end((explode(".", $files['userfile']['name'][$i])));    echo $file;
       $id=$this->student_reg->infra_cat1($data);       $file= $id.".".$ext;        $filepath= $loc . $file;
         echo $filepath; 
            $this->load->library('image_lib');
          $config['image_library'] = 'gd2';
          $config['source_image'] = $file;
          $config['create_thumb'] = FALSE;
          $config['maintain_ratio'] = FALSE;
          $config['width']         = 75;
          $config['height']       = 50;
    
       $this->load->library('image_lib', $config);
       $this->image_lib->resize();  
       if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filepath))
       {
           echo "File is valid, and was successfully uploaded.\n";
       }
       else {
               echo "Upload failed";
            }       
        } 
    
        } 
    
    
            }
            $this->load->view('infra_product',$data);
        }
        else
        {
            redirect("index");
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

尝试使用Codeigniter中的image_lib库。请找下面提到的例子。

if ($this->input->server('REQUEST_METHOD') === 'POST'): 
    $this->load->library('image_lib'); 
    $path_parts = pathinfo($_FILES["userfile"]["name"]); 
    $extension = $path_parts['extension'];
    move_uploaded_file($_FILES['userfile']['tmp_name'], "./uploads/img" . "." . $extension);
    $config['image_library'] = 'gd2';
    $config['maintain_ratio'] = TRUE;
    $config['create_thumb'] = TRUE; 
    $config['overwrite'] = TRUE; 
    $config['width'] = 200; 
    $config['height'] = 200; 
    $config['quality'] = 100; 
    $config['source_image'] = "./uploads/img" . "." . $extension;
    $this->image_lib->clear(); 
    $this->image_lib->initialize($config); 
    $this->image_lib->resize();
   endif;