Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter not maintaining aspect ratio

I would expect that $config['maintain_ratio'] = TRUE; would cause the width to be calculated if I only specify a height. I guess I have to provide any large number for the height to make it work?

    $config['image_library']    = 'gd2';
    $config['source_image']     = $image_filepath;
    $config['new_image']        = $thumbnail_filepath;
    $config['maintain_ratio']   = TRUE;
    $config['height']           = $this->thumbnail_height;

    $this->image_lib->initialize($config);
like image 665
Keyo Avatar asked Sep 13 '10 00:09

Keyo


People also ask

How do you fix an aspect ratio?

To create an exact aspect ratio, divide the height by the width. For example: 2:3 aspect ratio: 3 ÷ 2 = 1.5, so you'd drag the slider to 150.

What does the Maintain aspect ratio button do?

Maintaining aspect ratio is also known as "constraining proportions" in some graphic editing software (like Photoshop). It basically means that the width and height of the resized picture is enlarged/shrunk to proportion, so that the image does not look distorted after it has been resized.

Why you should maintain the aspect ratio while resizing an image?

When scaling your image, it's crucial to maintain the ratio of width to height, known as aspect ratio, so it doesn't end up stretched or warped. If you need a specific width and height, you may need a mixture of resizing and cropping to get the desired result.


2 Answers

Correct, you MUST specify both height and width. Since the maintain_ratio option is enabled, the resize will be as close to the target size as possible while preserving the original aspect ratio.

However, if you want to make sure it's always a certain height, set the master_dim config option to 'height', then you can set width to anything and it will always make the resize image that height while maintaining aspect ratio with the width. Similar idea works to maintain width.

like image 105
Mitchell McKenna Avatar answered Nov 06 '22 12:11

Mitchell McKenna


@Mitchell McKenna, Thanx. It helped me a lot.

In my project, the source image could have been of any size. and i wanted its width of '245' px, with maintaining ratio, So, I just did like this ....

$config['width'] = '245';
$config['height'] = '1';
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';

And it worked. Thanx

like image 40
TechCare99 Avatar answered Nov 06 '22 12:11

TechCare99