CodeIgniter Forums
Resize and maintain the golden ratio (3:5)? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Resize and maintain the golden ratio (3:5)? (/showthread.php?tid=16004)



Resize and maintain the golden ratio (3:5)? - El Forum - 02-21-2009

[eluser]markanderson993[/eluser]
Hello there CodeIgniter experts! I was wondering if there was a way to crop a picture to a 3:5 ratio?

I hope this question was simple!

Thanks in advance for any help! Smile

- Pianoman993


Resize and maintain the golden ratio (3:5)? - El Forum - 02-21-2009

[eluser]TheFuzzy0ne[/eluser]
Code:
function getWidth($height)
{
    return ($height / 3) * 5
}

Supply the height you want to the function and you'll get the right width back.


Resize and maintain the golden ratio (3:5)? - El Forum - 02-21-2009

[eluser]markanderson993[/eluser]
Thanks for the reply.

What if the picture was say 728px by 800px, would this still be able to crop the photo down to a 3:5 dimension? And if the photo was wider than tall it would appear 3 high and 5 long? (and vice-versa)


Resize and maintain the golden ratio (3:5)? - El Forum - 02-21-2009

[eluser]TheFuzzy0ne[/eluser]
The longest dimension should be used as your master dimension.

If the height is the largest dimension:
Code:
function getWidth($height)
{
    return ($height / 3) * 5
}

If the width is the largest dimension:

Code:
function getHeight($width)
{
    return ($width / 5) * 3
}

Just bear in mind that the further the image is away from this ratio, the more image you'll lose when it's cropped.


Resize and maintain the golden ratio (3:5)? - El Forum - 02-21-2009

[eluser]markanderson993[/eluser]
Makes sense to me. Thanks for your help Fuzzy!


Resize and maintain the golden ratio (3:5)? - El Forum - 02-21-2009

[eluser]TheFuzzy0ne[/eluser]
Sorry, I was wrong. You'd need to crop by the smallest dimension. It's 5:32AM and my brain is not working any more.

Code:
function getCropDimensions($width, $height)
{
    if ($height < width) // Height is master dim
    {
        $width = ($height / 3) * 5;
    }
    else // Otherwise width is master dim
    {
        $height = ($width / 5) * 3;
    }
    return array(
            'height' => $height,
            'width' => $width
        );
}
The above code is untested, but should work for you.