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'; //
Related
Here is my code from controller
`
$type= explode('.', $_FILES['picture']['name']);
$type = $type[count($type)-1];
$url = "uploads/products/images/".uniqid(rand()).".".$type;
if(in_array($type, array('jpg','jpeg','png','JPG','JPGE','PNG') ) )
{
if(is_uploaded_file($_FILES['picture']['tmp_name']))
{
move_uploaded_file($_FILES['picture']['tmp_name'],$url);
}
}
'
Since you're using CodeIgniter, it has it's own Image Manipulation class:
https://www.codeigniter.com/userguide3/libraries/image_lib.html
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/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();
Set the width and height variables to what you need to resize the image to.
okay, so I have two separate configs for the same library in the same controller function. The first one runs just fine, but the second does not. How can I make it so codeigniter unsets the first one and uses the second config.
// Retrieve the data from the upload
$data = $this->upload->data();
//Re-size the large image and re-save it
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto';
$config['width'] = 600;
$config['height'] = 516;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
$this->image_lib->crop();
echo 'Image Resized!';
}
//Create a thumbnail for the image
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
echo 'Image Thumbnail Created!';
}
So it re-sizes the image and then is supposed to thumbnail an image.
You can use the initialize method provided by the library, so you call it instead of loading the library each time:
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto';
$config['width'] = 600;
$config['height'] = 516;
$this->image_lib->initialize($config);
....
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->image_lib->initialize($config);
I am using following code but unable to resize image even $this->image_lib->resize() is returning true, don't know what is going wrong:
if(file_exists($_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".str_replace('_','-',$image)))
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".str_replace('_','-',$image);
}
else if (!file_exists("./uploads/avatars/".$image) || $image=="")
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/photo.jpg';
}
else
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".$image;
}
$config['image_library'] = 'gd2';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['dynamic_output'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Try the new_image tag:
$config['new_image'] = '/path/to/new_image.jpg';
Better to use SimpleImage.php as helper in CodeIgniter
$source=base_url().'/uploads/avatars/photo.jpg';
$image = new SimpleImage();
$image->load($source);
$image->resize(600,400);
$image->save($name_and_location);
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 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.