Codeigniter - How to upload original image with thumbnail image? - php

I have this code to file upload in codeigniter:
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++){
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
$name_array[] = $data['file_name'];
}
return $name_array;
}
This code is working perfect with single original image upload but how to upload image as thumbnail image(350 x 250) with resize in different folder.
Any idea how to do with codeigniter library?
Thanks

First store the original image and afterwards thumbnail image will upload
You just need to include gd2 library to generate the thumbnail image
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Image Upload - End
//Thumbnail Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
$config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}

if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = 'PATH_TO_UPLOAD_IMAGE';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Size of Image Upload - End
//Thumbnail Size of Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = 'PATH_TO_IMAGE_UPLOAD'. $value['name'][$s];
$config['new_image'] = 'PATH_TO_UPLOAD_THUMB_IMAGE'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//CI load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail SIZED Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}

Related

Upload Resize Image CI

I'm setting up the upload file before add resize code. And its normally process, but after adding resize code my controller isn't working. Can you help me with this? I'm trying but it didn't work.
And my another problem, I can't upload image file size more than 2MB.
This for my work on online shop.
public function submit_image()
{
$input_name = $_POST['input_name'];
$input_email = $_POST['input_email'];
$input_code_transaction = $_POST['input_code_transaction'];
$config['file_name'] = $input_code_transaction;
$config['overwrite'] = TRUE;
$config['upload_path'] = './img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 50000000;
$config['max_width'] = 6000;
$config['max_height'] = 4000;
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_picture = $this->upload->do_upload('doc');
}
}
}
The upload library has no inbuilt features for image resizing.
This whole section:
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_gambar = $this->upload->do_upload('doc');
needs to reference image_lib the image resizer codeigniter uses: https://www.codeigniter.com/user_guide/libraries/image_lib.html
So:
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$imlib['image_library'] = 'gd2';
$imlib['source_image'] = './img/' . $gbr['file_name'];
$imlib['create_thumb'] = FALSE;
$imlib['maintain_ratio'] = FALSE;
$imlib['quality'] = '50%';
$imlib['width'] = 1280;
$imlib['height'] = 720;
$imlib['new_image'] = './img/resized_' . $gbr['file_name'];
$this->load->library('image_lib', $imlib);
if (!$this->image_lib->resize()) {
show_error($this->image_lib->display_errors());
}
$yourouputimage = './img/resized_' . $gbr['file_name'];
}

Upload multiple image doesn't store to databased and path folder

I'm making upload multiple image and stored to one field. But I have a proble with my code, images can't store to database and to path folder. May be you can help me please
This is my controller
public function post() {
if(empty($_FILES['file']['name'])) {
$data = array( 'id_merk' => $this->input->post('id_merk'),
'createdAt' => $this->input->post('createdAt')
);
// var_dump($data);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'success');
redirect('admin/merk');
} else {
$count = count($_FILES['file']['size']);
foreach($_FILES as $value){
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $value['name'][$s];
$_FILES['file']['type'] = $value['type'][$s];
$_FILES['file']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['file']['error'] = $value['error'][$s];
$_FILES['file']['size'] = $value['size'][$s];
// $config['file_name'] = 'pict_'.date('Y_m_d_H_i_s').'.jpg';
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}
$names = implode(', ', $name_array);
$data = array( 'id_merk' => $this->input->post('id_merk'),
'photo_barang' => $names
);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'gambar ada');
redirect('admin/merk');
}
}
and this is my view
<input class="form-control" name="file[]" id="files" type="file" multiple="multiple">
Please, can you help me how to fix my problem with my code?
First, Use only 1 loop to upload the multiple images.
Second, give name attribute of <input type="file"> to $this->upload->do_upload()
Third, if uploading path inside the application folder then use APPPATH.'upload/be/barang' or if outside the application folder use FCPATH.'upload/be/barang'
$count = count($_FILES['file']['size']);
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $_FILES['file']['name'][$s];
$_FILES['file']['type'] = $_FILES['file']['type'][$s];
$_FILES['file']['tmp_name'] = $_FILES['file']['tmp_name'][$s];
$_FILES['file']['error'] = $_FILES['file']['error'][$s];
$_FILES['file']['size'] = $_FILES['file']['size'][$s];
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file')){
//image uploading error
}else{
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}

Multiple ImageUpload in Code Igniter

I have problem in uploading multiple Image in codeigniter
myview
<tr>
<td> Picture</td>
<td>:</td>
<td><input type="file" name="userfile[]" multiple /></input></td>
</tr>
and my model
function multi_upload_files($imagepath=''){
$number_of_files_uploaded = count($_FILES['userfile']['name']);
for ($i = 0; $i < $number_of_files_uploaded; $i++) {
$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['userfile']['size'][$i];
$config['upload_path'] = $imagepath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 900;
$config['max_width'] = 2024;
$config['max_height'] = 1068;
$this->upload->initialize($config);
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$images[]= $uploadData[$i]['file_name'];
return $images;
} }
I have testing it and the problem this code just read my upload Image just 1 image, not equal to number of my file that just upload (for ex: 2 image)
If you look at the following lines, you just overwrote everything with the first item in the array. Thus, only the first item is processed and the rest are overwritten. Plus, you have a return at the end of the loop. The function returns before the loop can finish.
$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['userfile']['size'][$i];
...
return $images;
You miss two things.
First,variable $imagepath is not defined.
Second, You miss to load upload libaray.
So,Solution is here try it.Hope it will works...
function multi_upload_files($imagepath=''){
$data = array();
$number_of_files_uploaded = count($_FILES['userfile']['name']);
for ($i = 0; $i < $number_of_files_uploaded; $i++) {
$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['userfile']['size'][$i];
$imagepath = 'Define upload path here';
$config['upload_path'] = $imagepath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 900;
$config['max_width'] = 2024;
$config['max_height'] = 1068;
//Load library first then use
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('userfile'))
{
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$images[]= $uploadData[$i]['file_name'];
}
return $images;
}

Codeigniter - Facing issue with multiple thumbnail creation

I am playing with Codeigniter image upload library and uploading multiple image at once and I am uploading original images with thumbs.
Now If I will upload three images, it will upload all three original images to this folder: project-images. and thumbnail image goes to thumbs folder. Everything is good but I am getting only one image in thumbs folder. 2 are not uploading inside thumbs folder.
What's wrong with my current code.
Any Idea?
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
// Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/project-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
$name_array[] = $data['file_name'];
// Original Image Upload - End
// Thumbnail Image Upload - Start
$config2['image_library'] = 'gd2';
$config2['source_image'] = $CI->upload->upload_path.$CI->upload->file_name;
$config2['new_image'] = './public/images/project-images/thumbs';
$config2['maintain_ratio'] = TRUE;
$config2['create_thumb'] = TRUE;
$config2['thumb_marker'] = '_thumb';
$config2['width'] = 370;
$config2['height'] = 200;
$CI->load->library('image_lib',$config2);
if(!$CI->image_lib->resize()){
$CI->session->set_flashdata('errors', $CI->image_lib->display_errors('', ''));
}
// [ MAIN IMAGE ]
$config['image_library'] = 'gd2';
$config['source_image'] = $CI->upload->upload_path.$CI->upload->file_name;
$config['maintain_ratio'] = TRUE;
$CI->load->library('image_lib',$config);
// Thumbnail Image Upload - End
}
return $name_array;
}
finally I got solution by adding this code before $CI->image_lib->resize():
$CI->image_lib->clear();
$CI->image_lib->initialize($config2);

codeigniter thumbnail created but original image problem

I have the code below, if i don't use watermark it's fine but when i use watermark, both the original pics and thumbnail become very small and , watermark happens on thumbnail, I want watermark on the original image, pls help
//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/blog';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload main image
if(!$this->upload->do_upload('photo')){
$e = $this->upload->display_errors();
print_r($e);
}
$image = $this->upload->data();
//thumbnail creation start
$config1['image_library'] = 'gd2';
$config1['source_image'] = $image['full_path'];
$config1['create_thumb'] = TRUE;
$config1['maintain_ratio'] = TRUE;
$config1['width'] = 75;
$config1['height'] = 50;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
//thumbnail creation ends
$this->image_lib->clear();
//$image = $this->upload->data();
//start watermarking
$config2['source_image'] = $image['full_path'];
$config2['wm_text'] = 'Copyright 2011 myself';
$config2['wm_type'] = 'text';
$config2['wm_font_path'] = './system/fonts/FromWhereYouAre.ttf';
$config2['wm_font_size'] = '16';
$config2['wm_font_color'] = 'ffffff';
$config2['wm_vrt_alignment'] = 'middle';
$config2['wm_hor_alignment'] = 'center';
$config2['wm_padding'] = '20';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->watermark();
//end watermarking
please try adding
$config2['new_image'] = '';
in the watermarking code

Categories