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);
Related
it does not show any error, and do nothing, I can not understand whats wrong with it!
public function resize($path,$filename) {
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$config['new_image'] = './img/uploads/users/'.$filename . '.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->clear();
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
Problem is you are calling clear method before resize method, it clears all settings try this method, i have tested with same directory structure it works fine
public function resize($path, $filename)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = true;
$config['maintain_ratio'] = true;
$config['width'] = 75;
$config['height'] = 50;
$config['new_image'] = './img/uploads/users/' . $filename . '.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
// $this->image_lib->clear();
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
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);
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'; //