Skip to content

Instantly share code, notes, and snippets.

@dimerman
Created March 16, 2012 21:10
Show Gist options
  • Save dimerman/2052720 to your computer and use it in GitHub Desktop.
Save dimerman/2052720 to your computer and use it in GitHub Desktop.
Resizing an image to a given rectangle maintaining aspect ratio
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