My Programming Tutorials

My Programming Tutorials

Resize Image Without Stretching in Codeigniter

Last updated on by , 3 comments

By this article “resize an image without stretching in CodeIgniter” I’m going to show you the mathematics and logic of resizing an image to a different width and height without squeezing or stretching the original image.

If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the same ratio of destination image have.

How to resize image without stretching in CodeIgniter

I’m not going to explain about uploading an image as I’m focusing only resizing an image here. In this article the step or module I’m going to explain you have to apply this module after uploading an image, so let’s get started.

paste bellow function in your controller


public function resize_image($image_data){
    $this->load->library('image_lib');
    $w = $image_data['image_width']; // original image's width
    $h = $image_data['image_height']; // original images's height

    $n_w = 273; // destination image's width
    $n_h = 246; // destination image's height

    $source_ratio = $w / $h;
    $new_ratio = $n_w / $n_h;
    if($source_ratio != $new_ratio){

        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/uploaded_image.jpg';
        $config['maintain_ratio'] = FALSE;
        if($new_ratio > $source_ratio || (($new_ratio == 1) && ($source_ratio < 1))){
            $config['width'] = $w;
            $config['height'] = round($w/$new_ratio);
            $config['y_axis'] = round(($h - $config['height'])/2);
            $config['x_axis'] = 0;

        } else {

            $config['width'] = round($h * $new_ratio);
            $config['height'] = $h;
            $size_config['x_axis'] = round(($w - $config['width'])/2);
            $size_config['y_axis'] = 0;

        }

        $this->image_lib->initialize($config);
        $this->image_lib->crop();
        $this->image_lib->clear();
    }
    $config['image_library'] = 'gd2';
    $config['source_image'] = './uploads/uploaded_image.jpg';
    $config['new_image'] = './uploads/new/resized_image.jpg';
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $n_w;
    $config['height'] = $n_h;
    $this->image_lib->initialize($config);

    if (!$this->image_lib->resize()){

        echo $this->image_lib->display_errors();

    } else {

        echo "done";

    }
}

after uploading an image you’ve to call above function along with uploaded image data (array)


if($this->upload->do_upload()){
    $image_data = $this->upload->data();
    $this->resize_image($image_data);
}

that’s all you have to do…

Conclusion

This is the more efficient way of “resize image without stretching in CodeIgniter“. All you have to do is just call the resize_image function with the uploaded image data.

For more interesting web development tricks please subscribe us, because we’re going to post lots of web modules and tricks in future.

if you find this article helpful please share it with your friend.

You may also like

Author Info

Paritosh Pandey

He loves Technology

Advertisement

3 responses to “Resize Image Without Stretching in Codeigniter”

  1. Khussal Zamlahani says:

    i dont get why the condition (($new_ratio == 1) && ($source_ratio < 1)) is necessary

  2. Khussal Zamlahani says:

    and by the way were these meant to be like this or mistyped?

    $size_config[‘x_axis’] = round(($w – $config[‘width’])/2);
    $size_config[‘y_axis’] = 0;

  3. Khokon Chandra says:

    Very nice & outstanding and helpful post it is….
    I’m highly interested about to visit this site.
    One more about web development tricks.

Leave a Reply

Your email address will not be published. Required fields are marked *