Codeigniter - Facing issue with multiple thumbnail creation - php

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

Related

setting width and height of image in codeigniter php not working

i have a codeigniter website where user can upload multiple images, i want all the images to be of the same size, i have done the following code in controller:
if (isset($_POST['addblog'])) {
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for ($i = 0; $i < $ImageCount; $i++) {
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
$uploadPath = './uploads/blog/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['width'] = 200;
$config['height'] = 250;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('file')) {
$imageData = $this->upload->data();
$uploadImgData[] = $imageData['file_name'];
}
}
$blogimage = $uploadImgData;
}
as u can see i have set the height and width in config, the images are still uploaded in ther original size, can anyone please tell me what is wrong in here, thanks in advance
You has just made a little mistake in your config :
$config['max_width'] = 200;
$config['max_height'] = 250;
Sadly "min_width" and "min_height" seems doesn't exist yet

Rename image name after upload in codeigniter

I need to rename image name after upload because my requirement is I need to add id with it so it's possible after store image name in DB.
I have done code for uploading image with and without watermark into different folder but now I want to change it's name like:
$imageName = $imgId.''.randomstring.''.$ext;
I have tried with below code but same image stored in both folder with watermark. I want to store original image in original_image folder and with watermark image in watermark_folder. Please help me to resolve this issue.
$getImgs = $this->photographer_model->get_uploaded_image($result);
$getExt = substr($getImgs[0]['img_name'], strrpos($getImgs[0]['img_name'],'.')+0);
$randomStr = $this->generate_random_string(20);
$fileName = $result."-".$randomStr."".$getExt;
$originalImgPath = base_url()."assets/original_image/".$fileName;
$watermarkImgPath = base_url()."assets/watermark_image/".$fileName;
$uploadOrgImg = 'assets/original_image/'.$fileName;
$uploadWaterImg = 'assets/watermark_image/'.$fileName;
$updateImg = $this->photographer_model->update_img_path($result, $fileName, $originalImgPath, $watermarkImgPath);
if(file_exists('assets/original_image/'.$getImgs[0]['img_name']) || file_exists('assets/watermark_image/'.$getImgs[0]['img_name'])) {
unlink('assets/original_image/'.$getImgs[0]['img_name']);
unlink('assets/watermark_image/'.$getImgs[0]['img_name']);
$config1['upload_path'] = 'assets/original_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['file_name'] = $fileName; // set the name here
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('image');
$config['upload_path'] = 'assets/watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['file_name'] = $fileName; // set the name here
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('image');
}
Update Your Code Below With this
$config1['upload_path'] = 'assets/original_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['encrypt_name'] = TRUE;
And Update for the second one
$config['upload_path'] = 'assets/watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['encrypt_name'] = TRUE;

Codeigniter - Two images upload did not work

I tried to upload two images using a form. To do that I wrote a image function.
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}
And I access that function this way,
// allocate image name to data set
if(!empty($_FILES["thumb_image"]['name'])){
// thumb upload
$thumb_image_new_name = $this->imageUploadResize($_FILES["thumb_image"]['name'],'thumb_image',THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, '_thumb');
$data['thumb_image'] = $thumb_image_new_name;
}else{
$data['thumb_image'] = "";
}
// allocate image name to data set
if(!empty($_FILES["banner_image"]['name'])){
// banner upload
$banner_image_new_name = $this->imageUploadResize($_FILES["banner_image"]['name'],'banner_image',BANNER_IMAGE_WIDTH, BANNER_IMAGE_HEIGHT, '_banner');
$data['banner_image'] = $banner_image_new_name;
}else{
$data['banner_image'] = "";
}
When I upload one image (thumb_image or banner_image) above function is worked properly.
But when I upload both images thumb_image uploaded properly but banner_image did not upload properly. As a example assume I am going to upload Hydrangeas.jpg and Desert.jpg. Then it is working this way,
Then file uploaded this way (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Hydrangeas1.jpg - image name also wrong
But expected output is (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Desert.jpg
1493025280_Desert_banner.jpg
Can someone please help me, thank you..
A different approach that i use for file uploads is below.
I rearrange the $_FILES array because the original has some difficulties with keys and objects.
$files = array();
foreach ($_FILES['files']['name'] as $num_key => $dummy) {
foreach ($_FILES['files'] as $txt_key => $dummy) {
$files[$num_key][$txt_key] = $_FILES['files'][$txt_key][$num_key];
}
}
In new array, loop through each image and do what you need:
foreach ($files as $file) {
if ($file['error'] == 0) {
//Your code here...
//Call your function imageUploadResize
}
}
Finally found the solution, In codeigniter when upload image, we have to call,
$this->load->library('upload', $config);
Then we have to initialized the configurations.
$this->upload->initialize($config);
And when resize image we have to call,
$this->load->library('image_lib', $config);
Then have to initialized the configurations.
$this->image_lib->initialize($config);
Therefore completed function is,
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}

Codeigniter - How to upload original image with thumbnail image?

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

Codeigniter image manipulator Unable to save the image

Well i have the following problem:
I'm using CodeIgniter 2.1.3 and want to upload an image and create two scaled images. one that is resized to the size of around 50x50 and one that is resized to around 200x200.
Uploading is doing fine.
But when i want to resize the following error occures:
Unable to save the image. Please make sure the image and file directory are writable.
I've set my permission on the server to 777, but it still doesn't compute
Here is the code of the classes:
public function uploadimage()
{
$head = array('title' => 'Upload Image');
$error = array('error' => '');
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
public function changeimage()
{
$config = array();
$config['upload_path'] = 'img/Upload';
$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
$config['max_size'] = '100';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['overwrite'] = TRUE;
$config['file_name'] = md5($this->session->userdata('username')).".png";
$head = array('title' => 'Upload Image');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
else
{
$uploaddata = $this->upload->data();
//Create Thumb
$config2 = array();
$config2['image_library'] = 'gd';
$config2['source_image'] = $uploaddata['full_path'];
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 50;
$config2['height'] = 50;
$config2['new_image'] = '/~3612546/DateSite/img/ProfileThumbs/'.$uploaddata['file_name'];
$this->image_lib->initialize($config2);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
//Create resized original image so images < 200x200 get resized to get as close to 200x200
$config3 = array();
$config3['image_library'] = 'gd';
$config3['source_image'] =$uploaddata['full_path'];
$config3['maintain_ratio'] = TRUE;
$config3['width'] = 200;
$config3['height'] = 200;
$config3['new_image'] = '/~3612546/DateSite/img/ProfileImages/'.$uploaddata['file_name'];
$this->image_lib->initialize($config3);
if ( ! $this->image_lib->resize())
{
echo $uploaddata['full_path'];
echo $this->image_lib->display_errors();
}
}
}
i really don't think tilde is good for upload urls:
/~3612546/DateSite/img/ProfileImages/
then, all your urls are upper/lowercase, put them all to lowercase like this:
/~3612546/dateSite/img/profileimages/
in the end post what error do you receive now that you chmod 777 your upload folder, do you added 777 only to dir or dir+all his content? (it's important dir+all his content to 777)

Categories