Resize image without distortion of image in codeigniter

1.2k Views Asked by At

I want to resize image without image being distorted.For example if i have set the image height to be 100 and width as 200.The image resized must be of 100 height and 200 width.In order to achieve that i want to crop that image instead of getting it distorted.

My config options:-

            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = FALSE;
            $config['master_dim'] = 'auto';
            $config['width'] = 100;
            $config['height']= 100;

            $this->image_lib->initialize($config);
            $this->image_lib->resize();

Also is it possible without using any other library of image manipulation?

3

There are 3 best solutions below

1
On BEST ANSWER

I have solved my problem. I had to re-sized my image and then crop it. Its a bit of long process anyways if you have a better than this please suggest.

My code:

            //Resize Image
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/original/'.$image_name;
            $config['new_image'] = './assets/banner/'.$image_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['master_dim']= 'width';
            $config['quality']  = '100';
            $config['width'] = 1260;
            $config['height']= 645;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            // Crop Image
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/banner/'.$image_name;
            $config['new_image'] = './assets/banner/'.$image_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['quality']  = '100';
            $config['x_axis'] = 0;
            $config['y_axis'] = 0;
            $config['width'] = 1260;
            $config['height']= 645;
            $this->image_lib->initialize($config);
            $this->image_lib->crop();
0
On
 $this->image_lib->resize();

i think it must be

 $this->image_lib->crop();
0
On

You either resize or crop it like someone said here.

If CROP

$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['x_axis'] = '100';
$config['y_axis'] = '60';
$this->image_lib->initialize($config); 

if ( ! $this->image_lib->crop())
{
   echo $this->image_lib->display_errors();
}

If resize is exactly what you have.