在codeigniter中上传时压缩图像大小

在codeigniter中上传时压缩图像大小

本文介绍了在codeigniter中上传时压缩图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CodeIgniter在上传时压缩图像。

I want to compress an image on upload using CodeIgniter.

当我尝试上传大于200 kb的图像时,它应该在CodeIgniter中上传的图像上压缩图像大小。

When I'm trying to upload an image larger than 200 kb in size then it should compress the image size on upload image in 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');
          }
     }

这是查看代码,即 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) && !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>

如何压缩上载的图像?

推荐答案

$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" />';
            }

这篇关于在codeigniter中上传时压缩图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:31