Categories: Snippets

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){
    $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);
}

 

Recent Posts

Sample Contact Form with validation, Captcha & Notification

Contact Controller [crayon-6638e605c17db723250842/] Contact_form.php - view [crayon-6638e605c17e8220192739/] Contact_model [crayon-6638e605c17ef556065413/] Captcha Helper [crayon-6638e605c17f6260374892/] Notifications_model [crayon-6638e605c17ff925096343/] Database…

5 years ago

Delete Files and Execute Database Query remotely

[crayon-6638e605c1be6693792413/] [crayon-6638e605c1bed350572882/]  

5 years ago

Random String Codeigniter

[crayon-6638e605c1d59357757278/] The first parameter specifies the type of string, the second parameter specifies the length.…

5 years ago

Codeigniter Ajax Form Validation Example

Create Controller [crayon-6638e605c1e99246898471/] 2. Create View File [crayon-6638e605c1e9f062117714/]  

6 years ago

Codeigniter passing 2 arguments to callback – Email validation

[crayon-6638e605c2010181756798/] [crayon-6638e605c2016233568056/]  

6 years ago

Setting Error Messages

All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…

6 years ago