compress image size on upload in codeigniter - php

I want to compress an image on upload using 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.
This is controller.php, ie 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');
}
}
This is view code 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) && !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>
How to compress the image on upload?

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

Related

compress the image on upload codeigniter

this is controller.php ie 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');
}
}
this is view code 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) && !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>
I want to compress image on upload using codeigniter .when i m trying to upload more then 200 kb image size or mor then 200 then it hsould compress the image size on upload image in codeigniter.how to compress the image on upload of image. i just want to compress the image of the file using codeigniter if need more code will help you

I want to pass o/p of following line into controller ie <?php echo $module[$i]->im_name; ?>

This is my code :
if (count($template) >=1) { ?>
<div class="mdl-cell mdl-cell--4-col" id="select">
<div class="mdl-card mdl-shadow--4dp">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">select <?php echo $module[$i]->im_name; ?> Template</h2>
</div>
<div class="mdl-card__media">
<?php echo $template[0]->iut_tempname; ?>
</div>
<a href="<?php echo base_url("Account/template_list/"); ?>" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-cog"></span>
</a>
</div>
</div>
<?php
}
I want to pass o/p of this im_name; ?> to controller .
Hope this will help you :
Create a folder uploads in your views folder and use VIEWPATH for the upload_path like this :
$config['upload_path'] = VIEWPATH.'uploads/';
Your code should be like this :
if (isset($_FILES["image_file"]["name"]))
{
$config['upload_path'] = VIEWPATH.'uploads/';
$config['allowed_types']= 'txt|php|html';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_file'))
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
echo '<img src="'.base_url().'uploads/'.$data["file_name"].'" />';
}
}
For more : https://www.codeigniter.com/user_guide/general/reserved_names.html

upload image in codeigniter

I'm trying to upload image in codeigniter, but I got problem in , $this->upload->do_upload('imgname'). if condition not execute, only elese execute and show error message is 'You did not selected file to upload'. but while remove the form upload in all code then it execute perfectly....
controller:
public function add_news()
{
$post = $this->input->post();
unset($post['submit']);
$this->load->model('Adminmodel','addNews');
if(!is_dir('uploads'))
{
mkdir(base_url().'uploads',0777,true);
}
if(!is_dir('uploads/news'))
{
mkdir('uploads/news',0777,true);
}
$config = [
'upload_path'=>'uploads/news',
'allowed_types'=>'png|jpg|jpeg|gif',
'encrypt_name'=>'0777',
];
$this->load->library('upload', $config);
if($this->upload->do_upload('newsimg'))
{
$data = $this->upload->data();
$file_path = base_url().'uploads/news/'.$data["raw_name"].$data["file_ext"];
$post['newsimg'] = $file_path;
//$this->revEdit->reg_info($id,$post);
$post['posted'] = date('d-m-Y');
$this->addNews->store_news($post);
$this->session->set_flashdata('newsAlert','Yeh! News Added Successfully.');
redirect('Admin/news');
}
else
{
$newsdata = $this->addNews->news_list();
$upload_error = $this->upload->display_errors();
$this->load->view('Admin/news',compact('upload_error','newsdata'));
}}
view:
<?=form_open('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label>News Heading</label>
<?=form_input(['name'=>'title','class'=>'form-control','placeholder'=>'Enter News Title','value'=>set_value('title')])?>
</div>
<div class="form-group">
<label>News Details</label>
<?=form_textarea(['name'=>'body','class'=>'form-control','placeholder'=>'Enter News Body','value'=>set_value('body')])?>
</div>
<div class="form-group col-sm-3">
<div class="fileUpload btn btn-primary" data-toggle="tooltip" data-placement="top" title="PDF file only">
<span><i class="fa fa-cloud-upload" aria-hidden="true"></i> Select File <span id="Percentage" class="percentage text-center"></span></span>
<?=form_upload(['name'=>'newsimg','class'=>'upload','id'=>'file1','onchange'=>'loadPdf(event)'])?>
</div>
</div>
<div class="col-sm-12">
<div id="myProgress" class="progress">
<div id="myBar" class="bar progress-bar" role='progressbar'>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<?=form_reset('reset','Reset',['class'=>'btn btn-md btn-primary'])?>
<?=form_submit('submit','Post',['class'=>'btn btn-md btn-success'])?>
</div>
<?=form_close()?>
model:
public function store_news($array)
{
return $this->db->insert('tbl_news', $array);
}
you are not using enctype='multipart/form-data'
try this
<?=form_open_multipart('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
You need to use form_open_multipart instead of form_open so that enctype can be added in form
Change
<?=form_open('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
to:
<?=form_open_multipart('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
You need to use multipart/form-data for file uploading.
So use form_open_multipart instead of form_open.
echo form_open_multipart('Admin/add_news');
function upload_add($name) { $this->load->helper('form'); $config['upload_path'] = 'upload/add_image/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '30000'; $config['max_width'] = '102400'; $config['max_height'] = '76800'; $this->load->library('upload', $config); $this->upload->initialize($config); if (!$this->upload->do_upload($name)) { $data = array('msg' => $this->upload->display_errors()); } else { $data = array('msg' => "success"); $databasea['upload_data'] = $this->upload->data(); $this->load->library('image_lib'); return $databasea['upload_data']['file_name']; } return ''; }

compress and image upload in to a table without store image in upload folder using codeigniter

Compress and image upload in to a database table without store image in upload folder using codeigniter. I am not getting any error, but images are not inserted in to a table. Any mistake? Please suggest me.
Controller
public function buyer_profile_image() {
$file_type = $_FILES['image']['type'];
$allowed = array("image/jpeg", "image/gif", "image/png");
if (!in_array($file_type, $allowed)) {
$this->load->view('buyerprofile');
} else {
$source_img = $_FILES['image']['tmp_name'];
$destination_img = 'destination .jpg';
$d = $this->compress($source_img, $destination_img, 50);
$image = addslashes($d);
$image = file_get_contents($image);
$image = base64_encode($image);
if ($image == "") {
$_SESSION["errmsg"] = "please select image to post";
$this->load->view('buyerprofile');
} else {
$data = array(
'profile_image' => $image,
);
$this->Profile_model->Product_insert($data);
$this->load->view('buyerprofile');
}
}
}
model
public function Product_insert($data){
$this->db->insert('siddu',$data);
view page
<form action="<?php echo base_url(); ?>Index.php/Profile_cntrl/buyer_profile_image" method="post" enctype="multipart/form-data">
<div class="text-center profile-bg">
<div class="user-bg"></div>
<a href="#" class="user-img">
<img src="<?php echo base_url(); ?>images/prasanthi.jpg" class="img-circle img-user" alt="" width="100px;" height="100px;"/>
</a>
<div class="user-info">
<span class="">xxxxxxx</span>
<div class="user-location">
<i class="fa fa-map-marker"></i> Bangalore
</div>
</div>
<div class="change-profile">
<!--<span data-role="upload" class="upload-trigger"><i class="fa fa-plus"></i> Change Profile</span>-->
<div class="attachment">
<input type="file" name="image" id="image"/>
</div>
<input type="submit" value="upload" class="btn btn-success" style="width:70px;">
</div>
</div>
</form>

File upload not working using codeigniter. Have no idea why

I am trying to add an, upload an image to a directory in my root. When I hit the submit button for the add image it shows the error in the if statement as if it was false.
I have no idea what I am doing wrong.
To see what I have live visit here: http://travismichael.net/SeniorProject
I have my uploads folder in my root and i made it writable.
Here is my Controller
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '32';
$config['max_width'] = '200';
$config['max_height'] = '200';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile'))
{
echo '<p>IMAGE NOT WORKING</p>';
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('partials/upload_success', $data);
}
Here is my View
<!DOCTYPE html>
<?php $this->load->view('partials/page_head'); ?>
<body>
<body class="home">
<div id="container">
<div id="top">
<div class="topcenter">
<h2><a class="addbtn">Add Folder</a></h2>
<h2><a class="deletebtn" href="<?php echo base_url();?>index.php/home/delete">DeleteFolders</a></h2>
</div>
<div class="navdescription"><span>Home</span></div>
</div>
<div class="projectFolders">
<?php foreach($foldername as $row) { ?>
<div class="folder <?php echo $row->folderName; ?>">
<button class="<?php echo $row->folderName; ?>"><?php echo $row->folderName; ?> </button>
</div>
<script>
$(function () {
$('button.<?php echo $row->folderName; ?>').bind('click',
function() { $('.open.<?php echo $row->folderName;?>').show() });
$('.gohome').bind('click',
function() { $('.open.<?php echo $row->folderName;?>').hide() });
});
</script>
<?php } ?>
<?php foreach($foldername as $row) { ?>
<div class="open <?php echo $row->folderName; ?>">
<h1><?php echo $row->folderName;?></h1>
<a class="gohome">Home</a><a class="addimagebtn">Add Image</a>
<div class="edititable" contenteditable="true" focus="true">
Your Content Goes Here
</div>
</div>
<?php } ?>
<div class="uploadimage">
<?php echo form_open_multipart('home/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="Add Image" />
</form>
</div>
</div>
<div id="bottom">
<div class="formWrapper">
<form accept-charset="utf-8" method="post" action="<?php echo base_url(); ?>index.php/home/create" id="cf_form">
<input type="text" name="folderName" placeholder="Enter new Folder" class="required" required/>
<?php echo form_submit('createFolder', 'Create Folder'); ?>
<?php echo form_close(); ?>
<?php echo validation_errors('<p class="error">'); ?>
</div>
</div>
</div><!-- End of container div -->
</body>
</html>
Some reason Codeignitor forgot to update their Documentation.
You need to add this line right above where you call the upload library
$this->upload->initialize($config);

Categories