[eluser]Unknown[/eluser]
Hi,
this has been discussed several times, and there is even a bug report regarding this problem, but for reasons beyond me, no one has taken 15 minutes to implement a simple fix for this. The problem is that the maintain_ratio setting for the image library behaves unexpectedly and illogically.
The whole point of maintaining an aspect ratio is that you should be able to provide either maximum allowed width OR height. If width is specified, then height should be calculated automatically and vice versa. However, in CI's image library, BOTH height AND width need to be specified for this to work, as seen from the following piece of code found at the beginning of image_reproportion() function
Code:
if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0) return;
For example, if I want to create an image with maximum width 500 on my page, and define the config in the following manner:
Code:
//Set resizing options
$config['image_library'] = 'gd2';
$config['source_image'] = $src;
$config['new_image'] = $dest;
$config['maintain_ratio'] = TRUE;
$config['width'] = 500;
It will resize the image to 500 in width, but keep the height equal to the one of the original image, essentially distorting the final image.
In my opinion this is a BUG, and a serious one. I know I can write my own helper that will calculate height in the appropriate aspect ratio, but I don't know why I should be forced to do this as the only logical place for that function would be inside the image library. It is very easy to fix. Please let me know if I am mistaken.