How can I process images in loop codeigniter? - image

I have two resize functions that I would like to perform one after the other using codeigniters image manipulation class:http://codeigniter.com/user_guide/libraries/image_lib.html
At the moment when I try running the function only the first one works, it ignores the second.
I have added $this->image_lib->clear() which according to codeigniters user guide: 'resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.'
Why can't I run the two seperate resize functions? How would I go about doing this??
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $upload_path . '/thumbs/',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
$config = array(
'source_image' => $image_data['full_path'],
'maintain_ration' => true,
'width' => 620,
'height' => 410
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();

If you don't want to use clear then store configs in two different arrays.
$this->image_lib->clear(); is used to flush previous settings & to add new.

Related

How to create thumbnail and store it in another folder using CI3.1.2?

I am using CI version 3.1.2 to process an image and using Windows 8. My problem is every time i process an image and trying to create a thumbnail, i am countering a message "Unable to save the image. Please make sure the image and file directory are writable". This is occurred when trying to save the copy of thumbnail into different location. Here are my codes processing the image:
$this->original_path = realpath(APPPATH.'../assets/galery/full');
// Uploading image
$config = [
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->original_path,
'max_size' => 2048,
'maintain_ratio' => true,
'file_name' => 'img_'.strtotime(date('d-m-y-H-i-s')), // miss type
'overwrite' => true,
'remove_spaces' => true
];
$this->load->library('upload', $config);
$this->upload->do_upload('galery');
$file_name = $this->upload->file_name;
// Processing image, success processing image
$this->load->library('image_lib');
$img = [
'image_library' => 'gd2',
'source_image' => $this->original_path.'/'.$file_name,
'maintain_ratio' => true,
'width' => 600
];
$this->image_lib->initialize($img);
$this->image_lib->resize();
// This is fail, with message "Unable to save the image... using(var_dump())
$thumb = [
'source_image' => $this->original_path.'/'.$file_name,
'new_image' => '/assets/galery/small',
'maintain_ratio' => true,
'width' => 200
];
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
I've tried different config, like using realpath(APPPATH.'../assets/galery/small'), but still no luck. It is success, by the way, when create config using 'create_thumb'.
But my purpose is, saving that thumbnail in another folder. How can i achieve that?
UPDATE
This change seem to work well. I change realpath(APPPATH.'../assets/galery/full') to realpath(APPPATH.'../assets/galery') and make several change in:
...
'upload_path' => $this->original_path.'/full'
...
'source_image' => $this->original_path.'/full/'.$file_name
...
'new_image' => $this->original_path.'/small/thumb_'.$file_name
Until now i'm still trying to make my code simple and put it in MY_Model. Thanks for all comment(s). If some one may have better solution, it would be nice to share here.

Codeigniter image resize not work properly

I want to resize the uploaded image as the ratio of 400 x 350. I use below code for this.
$config = array(
'image_library' => 'gd2',
'source_image' => $img_data['full_path'],
'new_image' => $this->upload_path. '/thumbs',
'maintain_ration' => FALSE,
'width' => 350,
'height' => 400
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Codeigniter give me the image as the ratio of 400 x 340.
see if this could help.
Here is the link to the class required to accomplish the task you looking for
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

codeigniter update image not reuploading

I'm running over this problem which I was trying for the last few hours
I'm having an image upload with some details to store in db.
I store the details and image path, working like a charm. Now comes the edit part.
I'm trying to check if the input file is empty, if so update just the details, else delete the image and reupload new image. The problem is this:
If the input file is empty it updates everything no problem, if is not empty it is updating the details, but the image is the same, doesn't get deleted or reuploaded.
here is the code
$image_input = $this->input->post('image');
if(isset($image_input) && !empty($image_input))
{
$this->db->where('id', $id);
$img = $this->db->get('menus_category', 1);
if($img->num_rows() > 0)
{
$row = $img->row();
$original_image = $row->image;
$desktop_image = $row->desk_img;
$mobile_image = $row->mob_img;
$thumb_image = $row->thumb;
$unlink_image = unlink('./uploads/menus/' . $original_image);
$unlink_desk = unlink('./uploads/menus/desk/' . $desktop_image);
$unlink_mob = unlink('./uploads/menus/mobile/' . $mobile_image);
$unlink_thumb = unlink('./uploads/menus/thumbs/' . $thumb_image);
if($unlink_desk && $unlink_image && $unlink_mob && $unlink_thumb)
{
$config = array(
'upload_path' => './uploads/menus',
'allowed_types' => 'gif|jpg|jpeg|png',
'max_size' => '15000'
);
$this->upload->initialize($config);
if ($this->upload->do_upload('image'))
{
$image_data = $this->upload->data();
$this->load->library('image_lib');
// thumb resize
$thumbnail = 'thumb_' . $image_data['file_name'];
$thumb = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'thumbs/' . $thumbnail,
'maintain_ratio' => TRUE,
'width' => '90',
'height' => '90'
);
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
$this->image_lib->clear();
// mobile resize
$mob = 'mob_' . $image_data['file_name'];
$thumb_mob = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'mobile/' . $mob,
'maintain_ratio' => FALSE,
'width' => '290',
'height' => '83'
);
$this->image_lib->initialize($thumb_mob);
$this->image_lib->resize();
$this->image_lib->clear();
// desktop resize
$desk = 'desk_' . $image_data['file_name'];
$thumb_desk = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'desk/' . $desk,
'maintain_ratio' => FALSE,
'width' => '700',
'height' => '200'
);
$this->image_lib->initialize($thumb_desk);
$this->image_lib->resize();
$this->image_lib->clear();
// insert path and details to database
$data = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['title']),
'description' => $input['description'],
'image' => $image_data['file_name'],
'desk_img' => $desk,
'mob_img' => $mob,
'thumb' => $thumbnail
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data);
}
else
{
echo $this->image_lib->display_errors();
}
}
}
}
else
{
$data2 = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['slug']),
'description' => $input['description']
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data2);
}
Note: the else statement works fine, but the first if the problem. Now I changed the if to just if(isset($image_input)) ... and if file input is not empty is reuploading the picture and updating the details fine, but if I update only the details with no picture, it is deleting the picture that is already uploaded and is not updating. (I think this is the problem but I can't figure out how to fix it).
If you will to give me some help or to put me on right direction I will be thankful.
Thanks guys
First, this would be a comment asking a question, but I don't have enough reputation do add one just yet...
If you could post your HTML form, that would be helpful, but without that...
1) I assume the form you are submitting has enctype="multipart/form-data" in the opening tag.
2) On line 2 of your code here, $this->input->post('image'), is 'image' from a form input of type file? If so, your statement won't work as 'image' is part of the $_FILES array and not $_POST anymore. Meaning when you go to check it, it is always going to be empty (or non-existent).
To verify that your form is submitting the way you think it is, do this just before the first line in the code you have in your post:
var_dump($_POST);
var_dump($_FILES);
exit; // don't let anything run after the dumps.
let me know if that puts you in the right direction or not.

Resize image with Codeigniter with low quality

I am using CodeIgniter to resize image, but the small image I have resized is low quality.
Here is my code:
$config = array(
'image_library' => 'gd2',
'quality' => '100',
'source_image' => $temp_full_path,
'new_image' => $full_temp_path."img_mobile/",
'maintain_ratio' => true,
'create_thumb' => false,
'width' => 50,
'height' => 50
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
How can make resize image with high quality like it's original image?
Here is original image, image resize generate from php code and image resize generate from codeigniter: Image
Looking at the source code, GD2 doesn't use the "quality" parameter.
But some of the other libraries do use it.
Give ImageMagick a try.
You should write 'quality' => '100%' instead of only '100'
Default:
$config['image_library'] = 'gd2';
Alternative:
$config['image_library'] = 'gd';
$config['quality'] = 100;
Best Quality:
$config['image_library'] = 'imagemagick';
$config['library_path'] = "/usr/bin";
$config['quality'] = 100;
Ref: https://forum.codeigniter.com/thread-17374.html
It's a small mistake or a pitfall.
Do:
'quality' => '100%',
instead of
'quality' => '100',
Should work, according to docs.
EDIT:
There seems to be another typo!
'maintain_ration'
should be
'maintain_ratio'
Also, how can new_image end with a /? you are doing something wrong there.

error resizeing images in a loop in codeigniter

i have a problem while uploading and resizing images in a loop.
can anyone please provide me the working sample of code of codeigniter for uploading and resizing at the same time in a loop.
I want to upload and resize images uploaded from the form. There will be more than 1 images so i have to upload them in loop.
My code first uploads the image then resizes it. 1ts images is uploaded and resized correctly but during 2nd loop the image is uploaded but not resized. It throws this error:
Your server does not support the GD
function required to process this type
of image.
I have tried the clear function too
$this->image_lib->clear();
can anyone please help
Dont load image_lib multiple times. Add image_lib in autoload libs and change
$this->load->library('image_lib', $config);
to
$this->image_lib->initialize($config);
I had the same problem but this seemed to work:
// Do upload
if (! $this->upload->do_upload($image_name)) {
// return errors
return array('errors' => $this->upload->display_errors());
}
$data = $this->upload->data();
$config_manip = array(
'image_library' => 'gd2',
'source_image' => "./assets/uploads/{$data['file_name']}",
'new_image' => "./assets/uploads/thumbs/{$data['file_name']}",
'create_thumb' => true,
'thumb_marker' => '',
'maintain_ratio' => true,
'width' => 140,
'height' => 140
);
// Create thumbnail
$this->load->library('image_lib');
$this->image_lib->resize();
$this->image_lib->clear();
$this->image_lib->initialize($config_manip);
if ( ! $this->image_lib->resize()){
return array('errors' => $this->image_lib->display_errors());
}
Notice the Create Thumbnail goes:
Load Library,
Resize Image,
CLEAR,
Initialize Config
I was putting the clear after the initialize which was causing the same error you're getting.
Here is a working code from my image gallery controller. This function uploads a batch of images, resizes them and saves them to database.
public function create_photo_batch() {
$this->load->library('image_lib');
$this->load->library('upload');
// Get albums list for dropdown
$this->data['albums'] = $this->gallery_m->get_albums_list();
if(isset($_FILES['images']) && $_FILES['images']['size'] > 0) {
$album_id = $this->input->post('albumid');
$images = array();
$ret = array();
// Upload
$files = $_FILES['images'];
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$upload_config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => realpath(APPPATH . "../uploads/gallery"),
'max_size' => 5000,
'remove_spaces' => TRUE,
'file_name' => md5(time())
);
$this->upload->initialize($upload_config);
if ($this->upload->do_upload('images[]')) {
$image_data = $this->upload->data();
$images[] = $image_data['file_name'];
} else {
$this->session->set_flashdata('error', $this->upload->display_errors() );
redirect('admin/gallery/create_photo_batch');
}
}
// Resize
foreach ($images as $image) {
$resize_config = array(
'source_image' => realpath(APPPATH . '../uploads/gallery') .'/'. $image,
'new_image' => realpath(APPPATH . '../uploads/gallery/thumbs'),
'maintain_ratio' => TRUE,
'width' => 500,
'height' => 500
);
$this->image_lib->initialize($resize_config);
if ( ! $this->image_lib->resize() ) {
echo $this->image_lib->display_errors();
die;
}
$this->image_lib->clear();
}
// Save to db
foreach ($images as $image) {
$ret[] = array(
'AlbumID' => (int) $album_id,
'Url' => $image,
'IsActive' => 1
);
}
$this->gallery_m->save_images($ret);
redirect('admin/gallery/album/'.$album_id);
}
//Load view
$this->data['subview'] = 'admin/gallery/photo_upload_batch_view';
$this->load->view('admin/_layout_main', $this->data);
}
Your error message suggest that it's not the loop that is the problem, but rather that the 2nd file is of a different filetype than the 1st. And that the underlying server don't have the needed libraries (http://www.libgd.org/Main_Page) installed to handle that file type.
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload_images/A.jpg';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 1600;
$config['height'] = 900;
$config['new_image'] = './assets/upload_images/'.$randy;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
I had same problem but solved these by this code.

Resources