Andrea
Andrea

Reputation: 45

Codeigniter resizing image with fix width

I'm trying to use Codeigniter's Image Manipulation Class to resize some images. I want to set the width to 150px and let the height change according with the new width. I.e. if I have a 300x200 image, it will become 150x100; if I have a 300x500 image, it will become 150x250.

With this configuration, portrait images widht is less than 150px:

$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 150;

If I don't set $config['height'] the resize class doesn't work as I hope. Is there a way to set a fix width and change height accordingly?

Upvotes: 0

Views: 9779

Answers (4)

Nookeen
Nookeen

Reputation: 465

Just in case anyone else wonders. It's actually quite simple, once you understand the logic behind CI resizing. I have an image 600x1125 that I need to make 100x'whatever_the _right_ratio_is". If we keep the code like this:

 //$config['maintain_ratio'] = TRUE; // By default this value is TRUE, so unless it is set to FALSE else where, it's not needed
 $config['width'] = 100;
 $config['height'] = 100;

CI goes with the Height. So it will make it 54x100, because it sees 100 as a maximum limit. Since we have maintain_ratio = TRUE, this is how it adjusts it. But if I make it like this:

 $config['width'] = 100;
 $config['height'] = 400;

Now, CI will go with the Width, since max width is 100 and this is the only way to keep the ratio. So it makes it 100x188.

Upvotes: 3

Melkor
Melkor

Reputation: 532

You can use $config['master_dim'] = 'width' to determine which axis should be used as the hard value.

See https://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

And I wonder if there's another way, because in order to keep radio, CI would need to determine which axis is the limit. If I were the developer, I would calculate the ratio of real width to desired width and the ratio of real height and desired height, then the larger ratio indicates the limit. So the limited axis size is just the desired size, and divide the real size of the other axis by this ratio would be the result size of the other axis. In this case, if I want to have a fixed width while keeping ratio, I just set height to a very large number, so that the resizer will actually ignore it, since the ratio of real height to desired height is far smaller than the ratio of real width and desired width. Then the result would be width fixed.

But when I set the height to PHP_INT_MAX, it throws out an error. I looked into the source code and found out CI did the calculation like this:

$new_width = ceil($this->orig_width*$this->height/$this->orig_height);
$new_height = ceil($this->width*$this->orig_height/$this->orig_width);

$ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));

So if I set height to PHP_INT_MAX, it'll exceed integer limit when doing multiplication.

Upvotes: 1

jagruti
jagruti

Reputation: 390

I think this code will help you, Its working fine for me.

    $config['image_library'] = 'gd2';
    $config['source_image'] = 'imagepath';
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']     = 75;
    $config['height']   = 50;
    $config['new_image'] = 'path to save resize image';

    $this->load->library('image_lib', $config); 
     $this->image_lib->initialize($config);

    if(!$this->image_lib->resize())
    {
        echo $this->image_lib->display_errors(); 
    }else echo "resize successfully";

In this code

source_image = Image path which image you want to resize
create_thumb = It will generate new image with imagename_thumb
maintain_ratio = maintain the original aspect ratio when resizing or use hard values.
new_image = location to save resize image Height and Width to resize image

Upvotes: 0

Praveen Govind
Praveen Govind

Reputation: 5607

YOu need to enable maintain aspect ratio

Use this configuration and check

 config['image_library'] = 'gd2';
 $config['source_image'] = '/path/to/image/mypic.jpg';
 $config['create_thumb'] = TRUE;
 $config['maintain_ratio'] = TRUE;
 $config['width'] = 75;
 $config['height'] = 50;

 $this->load->library('image_lib', $config);

 $this->image_lib->resize(); 

The above code tells the image_resize function to look for an image called mypic.jpg located in the source_image folder, then create a thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the maintain_ratio option is enabled, the thumb will be as close to the target width and height as possible while preserving the original aspect ratio. The thumbnail will be called mypic_thumb.jpg

Upvotes: 0

Related Questions