сжатие размера изображения при загрузке в codeigniter

#php #mysql #codeigniter

#php #mysql #codeigniter

Вопрос:

Я хочу сжать изображение при загрузке с помощью CodeIgniter.

Когда я пытаюсь загрузить изображение размером более 200 кб, оно должно сжимать размер изображения при загрузке изображения в CodeIgniter.

Это controller.php , т.е. college_panel :

         public function college_logo($collg_id='')
        {   
            $this->data['active']='manage_logo';
            if($this->session->userdata("user_login")){
            if($this->input->post()){
                $config['upload_path']='./uploads/college_logo';
                $config['allowed_types'] = 'jpg|png|jpeg';
                $data1['img_name']  = $_FILES['logo_img']['name'];
                $this->load->library('upload',$config);
                 if (!$this->upload->do_upload('logo_img'))
                 {
            $upload_error=array('error'=>$this->upload->display_errors());
                 }
                 else
                 {
                     echo "<script>alert('College Logo upload successfully...');</script>";
                      $this->session->set_flashdata('College Logo upload successfully', 'updated');
                 }
                $data['collg_id']=$this->input->post('collg_id');
                $data['logo_img']=$data1['img_name'];
                $row=0;
                if($row==0)
                {
            $result1=$this->front->update_table('tbl_college',array('collg_id'=>$collg_id),$data); 

                }   
            }
             $result=$this->front->get_data_where('tbl_college',array('collg_id'=>$collg_id));
                 $data['result']=$result; 
            $data['email']=$this->input->post('email');
            $data['password']=$this->input->post('password');
            $data['isactive']=1;
            $old_data=$this->front->get_data_where('tbl_login',array('isactive'=>1));
            $data['old_data']=$old_data;
            $record=$this->front->get_data_where('tbl_college',array('collg_id'=>$collg_id));
            $data['record']=$record[0];
            $this->load->view('collg_admin/header',$data,$this->data);
            $this->load->view('collg_admin/logo',$data);
            }
            else
            {

            redirect(base_url().'login');
          }
     }
  

Это просмотр кода ie logo.php :

 <div class="container">
  <div class="panel panel-default upload">
    <div class="panel-heading text-center"><h2>College Logo</h2></div>
      <br><br>
    <div class="panel-body">
      <div class="row">
        <center>
          <div class="col-md-12">
          <?php if (isset($result[0]->logo_img) amp;amp; !empty($result[0]->logo_img)) { ?>
            <img class=" img-circle" src="<?php echo base_url();?>uploads/college_logo/<?php echo $result[0]->logo_img;?>" height="200" width="200"><br>
            <?php } else {?>
              <img style="" src="<?php echo base_url();?>assets/images/default_logo.png" height="200" width="600"><br>
             <!-- <p>College Logo are not available.</p>-->
              <?php } ?>
          </div>
        </center>
        <div class="col-md-offset-2 col-md-7">
        <form action="<?php echo base_url();?>access/college_panel/college_logo/<?php echo $result[0]->collg_id;?>" method="post" enctype="multipart/form-data" role="form">
          <div class="form-group">
        <!--   <?php //print_r($result);?>-->
            <input type="hidden" name="collg_id" id="id_hh" value="<?php echo $result[0]->collg_id;?>" autofocus="" class="form-control" style="width: 100px;" >
              <br><br>
            <input class="form-control" type="file" accept="image/*" name="logo_img" value="<?php  echo $result[0]->logo_img; ?> " required/><br>
              <div class="col-md-offset-2 col-md-8 text-center">
                <button class="btn btn-info" onclick="">Submit</button>

              </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div> 
</div>
  

Как сжать изображение при загрузке?

Ответ №1:

 $config['upload_path'] = './upload/';  
            $config['allowed_types'] = 'jpg|jpeg|png|gif';  
            $this->load->library('upload', $config);  
            if(!$this->upload->do_upload('image_file'))  
            {  
                 echo $this->upload->display_errors();  
            }  
            else  
            {  
                 $data = $this->upload->data();  
                 $config['image_library'] = 'gd2';  
                 $config['source_image'] = './upload/'.$data["file_name"];  
                 $config['create_thumb'] = FALSE;  
                 $config['maintain_ratio'] = FALSE;  
                 $config['quality'] = '60%';  
                 $config['width'] = 200;  
                 $config['height'] = 200;  
                 $config['new_image'] = './upload/'.$data["file_name"];  
                 $this->load->library('image_lib', $config);  
                 $this->image_lib->resize();  
                 $this->load->model('main_model');  
                 $image_data = array(  
                      'name'          =>     $data["file_name"]  
                      );  
                 $this->main_model->insert_image($image_data);  
                 echo $this->main_model->fetch_image();  
                 //echo '<img src="'.base_url().'upload/'.$data["file_name"].'" width="300" height="225" class="img-thumbnail" />';  
            }