2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
module ImageSizer
|
|
|
|
|
|
|
|
# Resize an image to the aspect ratio we want
|
2018-12-03 23:19:49 +08:00
|
|
|
def self.resize(width, height, opts = {})
|
2013-06-15 18:29:20 +08:00
|
|
|
return if width.blank? || height.blank?
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2015-07-15 23:15:43 +08: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-26 06:24:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
w = width.to_f
|
|
|
|
h = height.to_f
|
|
|
|
|
2013-08-26 06:24:24 +08:00
|
|
|
return [w.floor, h.floor] if w <= max_width && h <= max_height
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-07-15 23:15:43 +08:00
|
|
|
ratio = [max_width / w, max_height / h].min
|
2013-08-26 06:24:24 +08:00
|
|
|
[(w * ratio).floor, (h * ratio).floor]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-05-23 22:18:30 +08:00
|
|
|
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-28 00:48:02 +08:00
|
|
|
|
|
|
|
[[max_width, w].min.floor, [max_height, (h * ratio)].min.floor]
|
2016-05-23 22:18:30 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|