milesj/cake-uploader — Issue #40: Resize and keep aspect
Author: Erwane
Created: 2012-02-04 18:18:45+00:00
State: open

Hi,

I've edit your Plugin to add another option. Here is the code an explanation.

Sometimes we need to resize or scale to a specific size but keep aspect.

The transforms options :

array('method' => 'resize', 'width' => 1000, 'height' => 800, 'dbColumn' => 'picture_full', 'aspect' => true),

This mean we want max width to 1000 OR max height to 800 and keep aspect ratio.

In resize method, change :

} else if (is_numeric($maxHeight) && is_numeric($maxWidth)) {
$newWidth = $maxWidth;
$newHeight = $maxHeight;

} else {

to

if ($options['aspect']) {
// Maintains the aspect ratio of the image and makes sure
// that it fits within the maxW and maxH
$widthScale = 2;
$heightScale = 2;

if ($maxWidth > $width)
$maxWidth = $width;
$widthScale = $maxWidth / $width;

if ($maxHeight > $height)
$maxHeight = $height;
$heightScale = $maxHeight / $height;

if ($widthScale < $heightScale) {
$newWidth = $maxWidth;
$newHeight = ($height * $newWidth) / $width;
} elseif ($widthScale > $heightScale) {
$newHeight = $maxHeight;
$newWidth = ($newHeight * $width) / $height;
} else {
$newWidth = $maxWidth;
$newHeight = $maxHeight;
}
} else {
$newWidth = $maxWidth;
$newHeight = $maxHeight;
}

This do the thing, if you want to include that code, you can ... or not :)