How to create Thumbnails from an unzipped image folder using for loop in Codeigniter?
nope
this would be better
config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
foreach ($images AS $file) {
$config['source_image'] = $file;
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
Load the directory helper:
$this->load->helper('directory');
Map the directory:
$images = directory_map('./directoryRelativeToIndexDotPhp/');
Now you an array of files in $images, set up a configure array for the image_lib class and loop through them, resizing the images:
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
foreach ($images AS $file) {
$config['source_image'] = $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
Not tested but this should give you a good start. You will probably want to check if the files are actually images before resizing.
Check out the documentation on the image manipulation library http://codeigniter.com/user_guide/libraries/image_lib.html
Related
This is the code which I am trying to run..
I have stored the image in images folder whose path is: C:\xampp\htdocs\ci\images, and is at the same level at that of the application folder
public function display(){
$config['image_library'] = 'gd2';
$config['source_image'] = '/images/mypic.jpg';
$config['new_image']='/images/re_mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
I am not getting any error or something
Add this and check your code errors
if ( ! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
for($i=0; $i< count($data['upload_data']); $i++){
//resize uploade image
$config['image_library'] = 'gd2';
$config['source_image'] = $data['upload_data'][$i]['full_path'];
$config['new_image'] = $data['upload_data'][$i]['full_path'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 700;
$config['height'] = 700;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
I want to resize the images in a loop, all the images are already in the database, but after running this script, only the first image is resized.
All the path are correct, anyone else encountering this problem?
After loading image_lib you might need to initialize it.
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
See: https://ellislab.com/codeigniter/user-guide/libraries/image_lib.html
You will NOT need to use the $this->image_lib->initialize function if
you save your preferences in a config file.
What I am basically trying to do is resize a image into two different size images. However I am only being able to resize the first featured_$filename I am not being able to resize thumb_$filename .
Initially I tried creating a single function and passed the configuration as an array and it didnt worked but same thing, only single image was resized.
$this->resizeImage($imagePath, $file['upload_data']['file_name']);
$this->resizeThumb($imagePath, $file['upload_data']['filename']);
public function resizeImage($imagePath, $filename){
$config['image_library'] = 'gd2';
$config['source_image'] = $imagePath;
$config['create_thumb'] = FALSE;
$config['new_image'] = 'featured_'.$filename;
$config['maintain_ratio'] = TRUE;
$config['width'] = 570;
$config['height'] = 372;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
public function resizeThumb($imagePath, $filename){
$config['image_library'] = 'gd2';
$config['source_image'] = $imagePath;
$config['create_thumb'] = FALSE;
$config['new_image'] = 'thumb_'.$filename;
$config['maintain_ratio'] = TRUE;
$config['width'] = 180;
$config['height'] = 135;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
You need to load the library only once but initialize the library twice with different configs. E.g. :
$this->load->library('image_lib');
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
Hope it helps.
hi load library first and use $this->image_lib->initialize($config) to pass config and also in new_image config pass path with new image name.
$this->load->library('image_lib');
$config['new_image'] = 'image_dir/thumb_'.$filename
//ALL your other configs
$this->image_lib->initialize($config)
I'm trying to resize and rotate a image.
At the moment it's only resizing the image, but not rotate it.
Here is the code, hope someone have a solution or something :-)
$config['image_library'] = 'gd2';
$config['source_image'] = $data['full_path'];
$config['new_image'] = $data['file_path'].'thumbs/'.$data['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 235;
$config['height'] = 235;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
$config['create_thumb'] = FALSE; //No thumbnail
$config['source_image'] = $data['file_path'].'thumbs/'.$data['file_name']; //full path for the source image
$config['rotation_angle'] = '180';//
$this->load->library('image_lib',$config);
//Rotate the image
$this->image_lib->rotate();
After
$this->image_lib->clear();
add:
$config = array() to re-initialise your config array.
After clearing the configuration, don't reload the library, reinitialize it:
$this->image_lib->clear();
$config=array();
$config['image_library'] = 'gd2';
$config['source_image'] = $data['file_path'].'thumbs/'.$data['file_name'];
$config['rotation_angle'] = '180';
$this->image_lib->initialize($config); // reinitialize it instead of reloading
$this->image_lib->rotate();
This is the only solution that finally worked for me. Just re-initialising $config did not work out in CodeIgniter 2.2.0.
Make sure you recreate the $config before sending it again.
Otherwise you might end up sending values you don't want to send.
At the moment rotate() gets a $config like this:
$config['image_library'] = 'gd2';
$config['new_image'] = $data['file_path'].'thumbs/'.$data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 235;
$config['height'] = 235;
$config['create_thumb'] = FALSE; //No thumbnail
$config['source_image'] = $data['file_path'].'thumbs/'.$data['file_name']; //full path for the source image
$config['rotation_angle'] = '180'; //
I need to make two images of a single loaded picture. This images must have fixed width, - 180 and 300 pixels.
At the bottom of my current results. This function can resize and create just one of two images. Everybody failed on second image, I trying whole day, but I'm can't find reason. Need help.
$this->_resize($data['upload_data']['file_name'], 300);
$this->_resize($data['upload_data']['file_name'], 180);
private function _resize($file_name, $size) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'img/upload/' . $file_name;
$config['dest_image'] = base_url() . 'img/';
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_' . $size;
$config['maintain_ratio'] = FALSE;
$config['width'] = $size;
$config['height'] = $size;
$this->load->library('image_lib', $config);
$result = $this->image_lib->resize();
$this->image_lib->clear();
return;
}
I'm use CodeIgniter 2.02
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);
This could help you, from user guide
A good practice is use the processing
function conditionally, showing an
error upon failure, like this:
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
Nill
Think that problem takes place because on the first run your script moves initial file to another folder. Try to use:
$config['new_image'] = base_url() . 'img/';
instead of
$config['dest_image'] = base_url() . 'img/';
I found myself this problem. In my case, I put the image source and new_image without base_url or REAL_PATH:
public function create_thumbnail($file_name='2012_02_23_15_06_00_1.jpg'){
$this->layout = false;
$image_url = PATH_TO_IMAGE_ARTICLE.DIRECTORY_SEPARATOR;
$config['image_library'] = 'gd2';
$config['source_image'] = 'assets/img/content/article/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 210;
$config['height'] = 160;
$config['new_image'] = 'assets/img/content/article/thumb/thumb_' . $file_name;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();exit;
}
return TRUE;
}
See?
you don't put
$config['new_image'] = base_url().'assets/img/content/article/thumb/thumb_' . $file_name;
but
$config['new_image'] = 'assets/img/content/article/thumb/thumb_' . $file_name;