Codeigniter image resize library not working properly - php

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.

Related

Error resizing image with codeigniter

i have a problem with resizing image in CI ( i'm new to CI by the way ).
so here is my code :
$config['image_library'] = 'gd2';
$config['source_image'] = base_url()."/uploads/test.png";
$config['maintain_ratio'] = TRUE;
$config['width'] = 800;
$config['height'] = 600;
$config['new_image'] = base_url().'/uploads/resized.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
echo $config['source_image'];
it give me this error :
The path to the image is not correct. Your server does not support the
GD function required to process this type of image.
i'm using MAMP as a server and i see GD enabled on phpinfo.
i also try to echo the image url , and yes its really there.
please help.
Thanks
You have to use relative path and you can try with this one
$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();
Please see this one

how can i provide image library path for imagemagick

I am using ImageMagick library in CodeIgniter for re-sizing and rotating image using image library. But its generating error. The error is -"The path to your image library is not correct. Please set the correct path in your image preferences."
$config = array();
$config['image_library'] = 'ImageMagick';
$config['source_image'] = $file;
$config['new_image'] = $file;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->image_lib->initialize($config);
if ( !$this->image_lib->resize())
{
echo "resize -".$this->image_lib->display_errors();
}
$this->image_lib->clear();
Below has given me expected result. Hope you will also get same.
$this->load->library('image_lib');
//For resizing of image in size of dilog
$config['image_library'] = 'ImageMagick';
$config['library_path'] = 'C:\\ImageMagick\\';
$config['source_image'] = $source_filepath;
$config['new_image'] = $new_filepath;
$config['width'] = 128;
$config['height'] = 128;
$config['quality'] = '100%';
$config['maintain_ratio'] = TRUE;
$this->image_lib->initialize($config);
if (! $this->image_lib->resize()) {
$error_msg = $this->image_lib->display_errors();
print_r($error_msg);
}
else {
echo "Done";
}
Here
$config['library_path'] = 'C:\\ImageMagick\\';
is the path for windows where your imageMagick application is installed.(Try to install in such a folder to which we can easily map it for library path).
Change the image library to :
$config['image_library'] = 'ImageMagick';
& other all configuration is remains same.
I think you are not giving correct path basically imagemagick is installed somewhere ideally in /user/bin. When I worked I use to give like this
imageMagickConvert = /usr/bin/convert
imageMagickComposite = /usr/bin/composite
$config['library_path'] = '/usr/bin';
add this line in your config.

Resize and rotate image - Codeigniter

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'; //

Problem resizing image using Code Ignitor1.7.3

Following is the code which resize the image, but here i am not able to resize the image
function processHome(){
$this->load->library('image_lib');
$img_path = base_url().'img/image/50X50/ori.jpeg';
$config['image_library'] = 'gd2';
$config['source_image'] = $img_path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
echo "No error";
exit;
$this->load->view('index', $data);
}
For a start, remove the
$this->load->library('image_lib');
at the beginning - you only need to load the library once, and only after you set the parameters.
Then, give the relative/server path to your image folder, instead of the url - as in, no base_url().
Finally, check the permissions for the folder the image is in - it must be readable/writable by all, and php should be allowed to create new files.
I guess that's all.

create thumbnails using for loop

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

Categories