compress the image on upload codeigniter - php

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

Related

MySQL on PHP need 2 reloads to update the values

I made a message deleter button, but I need 2 reloads to appear the changes...
(The rest of the code work, so it's normal that I don't show you the rest of the code...)
<?php while($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<div class="profile">
<img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
<h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
</div>
<div class="content">
<div class="date">
<?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
</div>
<br><br>
<?= htmlspecialchars_decode($r['content']) ?>
<form method="POST"><button name="delete<?= $r['id'] ?>">Test</button></form>
<?php
$test = "delete".$r['id'];
if(isset($_POST[$test])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($r['id']));
$success = "Your message was successfully removed !";
}
?>
</div>
</div>
<br>
<?php } ?>
UPDATE:
I added the deleting code at the top of my php code, and it's working, thanks to Ray Andison
By the way thanks to keidakida too; he helped me to find a solution to my value problem. (And I think he don't know that)
Your form doesn't contain any data (the id to be deleted) or action (page to submit data to)?
<form method="POST" action="thispage.php">
<input id="test" name="test" type="hidden" value="<?= $r['id'] ?>">
<input type="submit">
</form>
UPDATED:
<?
if(isset($_POST[id])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST[id]));
$success = "Your message was successfully removed !";
}
while($r = $replies->fetch()){
echo '
<div class="message" id="'.$r[id].'">
<div class="profile">
<img class="avatar" src="members/avatars/';
if(empty(get_avatar($r[id_author]))){
echo "default.png";
}else{
echo get_avatar($r[id_author]);
}
echo '
" width="150" height="150">
<h3 style="text-align:center;">
'.get_username($r[id_author]).'
</h3>
</div>
<div class="content">
<div class="date">
'.date('d F Y - g:iA', strtotime($r[date_hour_post])).'
</div>
<br>
<br>
'.htmlspecialchars_decode($r[content]).'
<form method="POST" action="thispage.php">
<input id="id" name="id" type="hidden" value="'.$r[id].'">
<input type="submit">
</form>
</div>
</div>';
}
?>
This is how I would code this, you need to change the action="thispage.php" to be the name of itself so it posts to itself, replace with the actual name of your php file
It is because the delete PHP code is at the bottom. Actions such as delete should be at the top of the HTML or while loops before presenting the data. SO try this:
<?php
if(isset($_POST["delete"])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST['delete']));
$success = "Your message was successfully removed !";
}
while($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<div class="profile">
<img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
<h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
</div>
<div class="content">
<div class="date">
<?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
</div>
<br><br>
<?= htmlspecialchars_decode($r['content']) ?>
<form method="POST">
<button type="button" name="delete" value="<?php echo $r['id']; ?>">Test</button>
</form>
</div>
</div>
<br>
<?php
}
?>
But you can do the same functionality without any page reload. Check AJAX PHP
Since it would be better and easier with AJAX, this is how it goes:
main.php
<?php
while ($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<?php echo htmlspecialchars_decode($r['content']) ?>
<button onclick="delete('<?php echo $r['id']; ?>')">Delete</button>
</div>
<br>
<?php } ?>
<script>
function delete(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert("Delete successfully");
location.reload();
}
};
xmlhttp.open("POST", "delete.php", true);
// Mandatory for simple POST request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Send id you want to delete
xmlhttp.send("id=" + id);
}
</script>
And make another PHP file name is delete.php like this:
<?php
include 'YOUR_DB_Connect.php';
if(isset($_POST["delete"])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST['delete']));
}
?>

compress image size on upload in codeigniter

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

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);

CodeIgniter - last else if statement fail to meet the condition

I think it's just a simple mistake I've made, but until now I still can't get the solution why my last else if {} statement does not execute when it meets the condition.
It's my form. I have 3 form elements as options for users to enter value. They can choose either to upload which are text, photo, or movie. Here is my PHP:
if ( $this->input->post('text') !== '') {
//this executes fine when it meets the condition
} else if ( $this->input->post('photo') !== '' ) {
// this also works fine
} else if ( $this->input->post('video') !== '' ) {
/* however, I can't get to this condition when
the user chooses to upload the movie.
It always go the second condition.
*/
}
Edit:
Here's my view:
<form action="<?php echo base_url()?>action/post" method="post" enctype="multipart/form-data">
<ul class="nav nav-tabs">
<li class="active">
Update Status
</li>
<li>
Post Photos
</li>
<li>
Post Videos
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="status">
<textarea name="text"></textarea>
</div>
<div class="tab-pane" id="photos">
<input type="file" name="photo" class="input" />
<input type="text" name="photos-detail" placeholder="description" />
</div>
<div class="tab-pane" id="videos">
<input type="file" name="video" class="input" />
<input type="text" name="videos-detail" placeholder="description" />
</div>
</div>
<button type="submit" class="btn btn-primary" name="post">Post</button>
</form>
Any idea would be really appreciated.
You cant check file inputs reliably like that. I usually check the file size to see if anything has been uploaded, so you could do something like this (should work):
if ( $this->input->post('text') !== '') {
//code
} else if((isset($_FILES["photo"]["size"])) && ($_FILES["photo"]["size"] > 0)){
// code 2
} else if((isset($_FILES["video"]["size"])) && ($_FILES["video"]["size"] > 0)){
// code 3
}
NOTE
You can't just check the file size or you will get undefined index error - hence the isset()
If I use the same code the result that I get is that my view show me the entire code line.
if ( $usuario->party !== '') {
<img src="<?php echo base_url('assets/img/link/sprite1_13.png') ?>">
} else {
<img src="<?php echo base_url('assets/img/link/party.png') ?>">
}
If I don´t use my code works fine:
<?php foreach ($usuarios->result() as $usuario) { ?>
<div id="cuerpo" class="cuerpoMyPerfil">
<div id="columna1">
<div id="contenColumn1" class="corner">
<div class="myFoto">
<img src="<?php echo base_url('assets/img/testimonials/user.jpg') ?>" width="100%" height="auto" alt="nombre" /> </div>
<div class="myNombre">
<?php
echo $usuario->nombre . "<br/>";
echo $usuario->apellidos;
?>
</div>
<div class="myDescripcion">
<?php echo $usuario->acerca; ?>
</div>
if ( $usuario->party !== '') {
<img src="<?php echo base_url('assets/img/link/sprite1_13.png') ?>">
} else {
<img src="<?php echo base_url('assets/img/link/party.png') ?>">
}
<dl class="dl-vertical">
<!-- Verificar que tenga valores en la tabla vincular
Si lo trae, poner el ícono prendido, de lo contrario el apagado-->
<dt>
<img class="pull-right" src="<?php echo base_url('assets/img/link/love.png') ?>"></dt>
<dt><img src="<?php echo base_url('assets/img/link/network.png') ?>">
<img class="pull-right" src="<?php echo base_url('assets/img/link/friends.png') ?>"></dt>
</dl>
</div>
<div class='myTextInvi'>
<div class='textInvitacionActual floatL'>Tienes Invitaciones disponibles</div>
<div class='numInvitacionActual floatL' ><?php echo $usuario->disponibles; ?></div>
</div>
<div class='myBtnInvi'>
<a href="<?php echo base_url('oneperfil/invite') ?>">
<div class='textBtnInvitacion'>ENVIAR INVITACIÓN</div>
</a>
</div>
</div>
</div>
<?php } ?>

Categories