作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 CodeIgniter 压缩上传的图像。
当我尝试上传大小超过 200 kb 的图像时,它应该压缩 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" />';
}
关于php - 在 codeigniter 中上传时压缩图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55808257/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!