discourse/lib/image_sizer.rb

Failed to ignore revisions in .git-blame-ignore-revs.

38 lines
986 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-05 14:16:51 -05:00
module ImageSizer
# Resize an image to the aspect ratio we want
def self.resize(width, height, opts = {})
return if width.blank? || height.blank?
2013-02-25 11:42:20 -05:00
max_width = (opts[:max_width] || SiteSetting.max_image_width).to_f
max_height = (opts[:max_height] || SiteSetting.max_image_height).to_f
2013-08-25 18:24:24 -04:00
2013-02-05 14:16:51 -05:00
w = width.to_f
h = height.to_f
2013-08-25 18:24:24 -04:00
return [w.floor, h.floor] if w <= max_width && h <= max_height
2013-02-05 14:16:51 -05:00
ratio = [max_width / w, max_height / h].min
2013-08-25 18:24:24 -04:00
[(w * ratio).floor, (h * ratio).floor]
2013-02-05 14:16:51 -05:00
end
def self.crop(width, height, opts = {})
return if width.blank? || height.blank?
max_width = (opts[:max_width] || SiteSetting.max_image_width).to_f
max_height = (opts[:max_height] || SiteSetting.max_image_height).to_f
w = width.to_f
h = height.to_f
return [w.floor, h.floor] if w <= max_width && h <= max_height
ratio = max_width / w
2016-07-27 12:48:02 -04:00
[[max_width, w].min.floor, [max_height, (h * ratio)].min.floor]
end
2013-02-05 14:16:51 -05:00
end