Created
March 16, 2012 21:10
-
-
Save dimerman/2052720 to your computer and use it in GitHub Desktop.
Resizing an image to a given rectangle maintaining aspect ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fit_dimensions( original, target ) | |
original_ratio = original[:height].to_f / original[:width].to_f | |
target_ratio = target[:height].to_f / target[:width].to_f | |
result = target.clone | |
case original_ratio <=> target_ratio | |
when -1 then result[:height] = target[:width].to_f * original_ratio | |
when 1 then result[:width] = target[:height].to_f / original_ratio | |
end | |
return result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment