How to resize image in php codeigniter

CodeIgniter provided image manipulation classes let helps to do image resizing, thumbnail creation , image cropping, image rotating , image watermarking. Here we see how image resizing works.

Image Resize Codeigniter Configuration

You need to do simple configuration to resize images, set the configurations propeties like image_library ,source_image,create_thumb etc width, height as 50 * 50 , For that we are going to use inbuilt image library gd2.

 // Configuration
 $config['image_library'] = 'gd2';
 $config['source_image'] = './img/images/uploaded/'.$imgName.".jpeg";
 $config['new_image'] = './img/images/uploaded/'.$imgName."_new.jpeg";
 $config['create_thumb'] = TRUE;
 $config['maintain_ratio'] = TRUE;
 $config['width'] = 50;
 $config['height'] = 50;

Load library After Configuration

// Load the Library
$this->load->library('image_lib', $config);

Call the resize function

 // resize image
 $this->image_lib->resize();
 // handle if there is any problem
 if ( ! $this->image_lib->resize()){
  echo $this->image_lib->display_errors();
 }

resize() function resize the image file and create new image file in specified folder. You will get errors "$this->image_lib->display_errors()", if there is any problem while image manipulation.

function imageResize50X50($imgName){
 $img_path =  realpath("img")."\\images\\uploaded\\".$imgName.".jpeg";
 
 // Configuration
 $config['image_library'] = 'gd2';
 $config['source_image'] = './img/images/uploaded/'.$imgName.".jpeg";
 $config['new_image'] = './img/images/uploaded/'.$imgName."_new.jpeg";
 $config['create_thumb'] = TRUE;
 $config['maintain_ratio'] = TRUE;
 $config['width'] = 50;
 $config['height'] = 50;

 // Load the Library
 $this->load->library('image_lib', $config);
 
 // resize image
 $this->image_lib->resize();
 // handle if there is any problem
 if ( ! $this->image_lib->resize()){
  echo $this->image_lib->display_errors();
 }
}