Codeigniter image resize() thumbnail name should have same name - codeigniter

Here my controller file this working fine except in thumbs folder name get changed to converted_thumb
example:
my original image name is converted.jpg but in thumbs folder it save as converted_thumb so i want to remove _thumb from image name please solve this issue
public function do_upload() {
$config['upload_path'] = './uploads'; // uploaded file store here
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = ' 2097152';
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = $this->upload->data();
//create copy of image
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['width'] = '250';
$configs['height'] = '250';
$this->load->library('image_lib', $configs);
$this->image_lib->resize();
$image_name = $data['file_name'];
//$full_path = $data['full_path'];
$post = array(
'product_name' => $image_name,
'product_path' => $configs['new_image'].$image_name
);
$this->db->insert('project', $post);
} else {
echo $this->upload->display_errors();
}
}
}

Why wouldn't you like the _thumbs from that? It is what it is for. Anyway do this.
rename("<?php echo base_url()?>/uploads/thumbs/YOUR_FILE_NAME_THUMBS", "<?php echo base_url()?>/uploads/thumbs/YOUR_FILE_NAME");

Try thumb_marker in your $configs array.
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['thumb_marker'] = ''; //Add this in your config array empty string
Then '_thumb' will not add in your newly created files. Source for more detail.

Just add thumb_marker to your config array and set it to FALSE
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['thumb_marker'] = FALSE; //this will remove the "_thumb" to your thumb image name
$configs['width'] = '250';
$configs['height'] = '250';

Related

How to resize image on codeigniter?

I get a module like a blog, and I must upload an image to my website. but when I upload my image not be resized/cropped automatically.
CONTROLLER
function simpan_campaign(){
$config['upload_path'] = './assets/images/upload'; //path folder
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang dapat diakses bisa anda sesuaikan
$config['encrypt_name'] = TRUE; //Enkripsi nama yang terupload
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])){
if ($this->upload->do_upload('filefoto')){
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./assets/images/upload'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 380;
$config['height']= 264;
$config['new_image']= './assets/images/upload'.$gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$image=$gbr['file_name'];
$title=$this->input->post('title');
$cashtarget=$this->input->post('cashtarget');
$campcode=$this->input->post('campcode');
$datefrom=$this->input->post('datefrom');
$dateend=$this->input->post('dateend');
$category=$this->input->post('category');
$desc=$this->input->post('description');
$this->main_model->save_campaign($title,$desc,$image,$cashtarget,$campcode,$datefrom,$dateend,$category);
echo "Image berhasil diupload";
redirect('account/add');
}
}else{
echo "Image yang diupload kosong";
}
}
and my model like :
MODEL
function save_campaign
($title,$desc,$image,$cashtarget,$campcode,$datefrom,$dateend,$category){
$hsl=$this->db->query("INSERT INTO tcampaign (title,description,pathimage,cashtarget,campcode,datefrom,dateend,category) VALUES ('$title','$desc','$image','$cashtarget','$campcode','$datefrom','$dateend','$category')");
return $hsl;
}
I can upload but i cant resize or crop on my view
I suspect that the biggest problem was this declaration:
'./assets/images/upload'.$gbr['file_name']; if you notice there is no ending slash before the filename...
Also you used the same variable $config for both upload and resizing. This can also cause some unexpected results. Just use a different variable for both. Here we have $upconfig and $config.
I've also cleaned up the function a bit and added in the error methods so you can see what is going wrong if something does. You should handle the errors more elegantly than just echoing them.
function simpan_campaign() {
$this->load->library('upload'); // not sure if you already autoloaded this
// this way $config won't overwrite or add on to the upload config
$upconfig['upload_path'] = './assets/images/upload/'; // missing end slash
$upconfig['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$upconfig['encrypt_name'] = TRUE;
$this->upload->initialize($upconfig);
if (!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('filefoto')) {
$filename = $this->upload->data('file_name');
//Compress Image
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path'); // missing slash before name
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '50%';
$config['width'] = 380;
$config['height'] = 264;
// not required as you have declared the same filename
// the original image will be targeted for resize
//$config['new_image'] = './assets/images/upload/' . $filename;
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
exit;
}
$title = $this->input->post('title');
$cashtarget = $this->input->post('cashtarget');
$campcode = $this->input->post('campcode');
$datefrom = $this->input->post('datefrom');
$dateend = $this->input->post('dateend');
$category = $this->input->post('category');
$desc = $this->input->post('description');
$this->main_model->save_campaign($title, $desc, $filename, $cashtarget, $campcode, $datefrom, $dateend, $category);
echo "Image berhasil diupload";
redirect('account/add');
} else {
echo $this->upload->display_errors();
exit;
}
} else {
echo "Image yang diupload kosong";
}
}

how to do image upload in codeigniter?

I am trying to upload an image in root folder and its file name in database. here is what I did for the upload function:
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
print_r($picture); exit;
}
}
print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
problem here is i am being able to insert other data except image. I get the image name with that print_r($config[file_name]) if i do print_r() and exit, if not it will just insert other data except image. But the image is neither uploaded in root folder nor its name in database. If I give the non existing upload path, then also its not throwing any error. I think code inside If is not executed. How can i solve this ? Thanks in advance.
private function _upload_image( ) {
$this->load->library( 'upload' );
if ($_FILES && $_FILES['picture']['name'] !== ""){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|bmp';
$config['max_size'] = 10000;
/*the picture name must be unique, use function now()*/
$config['file_name'] = $_FILES['picture']['name'] . now();
$config['file_ext_tolower'] = TRUE;
$this->upload->initialize( $config );
if ( $this->upload->do_upload( 'picture' ) ){
$file_name = $this->upload->data()['file_name'];
$full_path = $this->upload->data()['full_path'];
/*If you want create a thumb, use this part*/
$this->load->library('image_lib');
$config = array(
'source_image' => $path,
'new_image' => $this->_image_path,
'maintain_ratio' => true,
'width' => 128,
'height' => 128,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
);
$this->image_lib->initialize( $config );
$this->image_lib->resize();
/*Save in database*/
$this->db->insert('blog', [
'file_name' => $file_name,
'full_path' => $full_path
]);
} else {
//if picture is empty, do something
}
}
}
You do not need to use $_FILES && $_FILES ['picture']['name']! == "" only if your form has the picture field as an optional field, $this->upload->do_upload('picture') and get data from $this->upload->data(), read the manual
public function add_blog()
{
$config['upload_path'] = '.uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('picture'))
{//Do something with errors
$errors = $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$this->db->insert('blog', [
'file_name' => $data['file_name'],
'full_path' => $data['full_path']
]);
}
}
I just didn't mention the file size to be uploaded. I did this in my above code and worked.
EDIT
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
// print_r($picture); exit;
}
}
// print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
And this code works for both insert and update.

codeiginter upload and resize

I have a problem with CI class image, image does not resize...
for example :
controller
private function _do_upload(){
$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000; //set max size allowed in Kilobyte
$config['max_width'] = 1000; // set max width image allowed
$config['max_height'] = 1000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000);
$this->load->library('upload', $config);
if($this->upload->do_upload('photo')) //upload and validate{
$config2['image_library'] = 'gd2';
$config2['source_image'] = $this->upload->upload_path;
$config2['create_thumb'] = TRUE;
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 450;
$config2['height'] = 500;
$this->load->library('image_lib', $config2);
if(!$this->image_lib->resize()){
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
$data['status'] = FALSE;
}
return $this->upload->data('file_name');
}else{
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
}
But i got no error, I dont know where is the mistake?
Well you have a few issues...
1. You have commented out your opening { which would break your code.
if($this->upload->do_upload('photo')) //upload and validate {
Should be
if($this->upload->do_upload('photo')) { //upload and validate
2. The next part: If you do have a resize error you are not doing anything with your messages... So...
if(!$this->image_lib->resize()){
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
$data['status'] = FALSE;
}
Needs the added json_encode like..
if(!$this->image_lib->resize()){
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('','');
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
In reference to your resizing, you do need to supply the images full filename and not just the path where "files" live...
Your source_image is a Path to where the files are uploaded to. Not the Path/Filename of the image you want to resize...
$config2['source_image'] = $this->upload->upload_path;
So if you add this line that gives you the full path and filename and use it...
$file_data = $this->upload->data();
$config2['source_image'] = $file_data['full_path'];
Does that work better?

I want to download file in codeigniter

on this way i can upload image and pdf..
but i want to download the pdf or image from my view...please someone give me the code of downloading pdf or image from view..give me the full code
public function save_about_1() {
$about_1_image_info = $this->w_model->select_about_1_image();
$image_path = explode(base_url(), $about_1_info->about_1_link, 2);
unlink($image_path[1]);
$this->sa_model->delete_about_1($about_1_info->about_1_id);
$data = array();
/* Uplod start */
$config['upload_path'] = 'images/about_1/';
$config['allowed_types'] = 'gif|jpg|png|pdf|doc|xml';
$config[ 'overwrite'] = TRUE;
$config['max_size'] = '10000kb';
$config['max_width'] = '100240';
$config['max_height'] = '76800';
$error = array();
$fdata = array();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('about_1_link')) {
$error = $this->upload->display_errors();
$edata = array();
$edata['error_message'] = $error;
$this->session->set_userdata($edata);
redirect('super_admin/about_1');
} else {
$fdata = $this->upload->data();
$data['about_1_link'] = base_url() . $config['upload_path'] . $fdata['file_name'];
$this->sa_model->save_about_1_info($data);
$sdata = array();
$sdata['message'] = "Saved Image Successfully";
$this->session->set_userdata($sdata);
redirect('super_admin/about_1');
}
}
You can use the CI download helper for this.
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';
force_download($name, $data);
From the CI User Guide.
https://ellislab.com/codeigniter/user-guide/helpers/download_helper.html

Multiple File from Same Form with Different Name Uploading

I am Trying to Upload two file with from Same form with Different File name.
The First File is always Uploading But the Other one never Uploading.
code for Model is:
public function add_imgup()
{
$this->uppdf();
$this->upphoto();
}
public function upphoto()
{
$upload = array();
$current_timestamp = time();
$image_name = $current_timestamp.'_image';
$config['upload_path'] = $this->config->item('file_upload_absolute_path')."pdf/";
$config['allowed_types'] = 'pdf|PDF|DOC|doc|docx';
$config['overwrite'] = true;
$config['file_name'] = $image_name;
$this->load->library('upload',$config);
$file_image = $this->upload->do_upload('ccv');
$image = array('upload_data' => $this->upload->data());
}
public function uppdf()
{
$upload = array();
$current_timestamp = time();
$image_name = $current_timestamp.'_image';
$config['upload_path'] = $this->config->item('file_upload_absolute_path')."photo/";
$config['allowed_types'] = 'jpg|jpeg|png|gif|bmp';
$config['overwrite'] = true;
$config['file_name'] = $image_name;
$this->load->library('upload',$config);
$file_image = $this->upload->do_upload('iimg');
$image = array('upload_data' => $this->upload->data());
}
Check the print_r($_FILE) after submitting the form to confirm the you submitted correct form, also check your form is enctype="multipart/form-data"

Resources