Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Resize Image Without Stretching in Codeigniter

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){
    $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 image_lib->initialize($config);
    $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;

    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)

    $image_data = $this->upload->data();

that’s all you have to do…


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

  • How To Get User’s Accurate Latitude and Longitude using Google’s APIs and PHP
  • Autocomplete Address Search Module Using Google API and PHP
  • Multi Level Nested Category System in Codeigniter and MySql

The post Resize Image Without Stretching in Codeigniter appeared first on My Programming Tutorials.

This post first appeared on My Programming Tutorials, please read the originial post: here

Share the post

Resize Image Without Stretching in Codeigniter


Subscribe to My Programming Tutorials

Get updates delivered right to your inbox!

Thank you for your subscription