From dc61eaad37898212829723a453129acdca9d6ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 5 Jun 2018 17:13:00 +0200 Subject: [PATCH] FEATURE: new 'min ratio to crop' site setting --- config/locales/server.en.yml | 2 ++ config/site_settings.yml | 5 +++++ lib/cooked_post_processor.rb | 9 ++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 4d10b61b92b..fd9451fa165 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1342,6 +1342,8 @@ en: strip_image_metadata: "Strip image metadata." + min_ratio_to_crop: "Ratio used to crop tall images. Enter the result of width / height." + enable_flash_video_onebox: "Enable embedding of swf and flv (Adobe Flash) links in oneboxes. WARNING: may introduce security risks." default_invitee_trust_level: "Default trust level (0-4) for invited users." diff --git a/config/site_settings.yml b/config/site_settings.yml index 6b4bd9d0a1d..52aeb51cc6b 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -941,6 +941,11 @@ files: default: true client: true strip_image_metadata: true + min_ratio_to_crop: + type: float + default: 0.45 # 90% of 18:9 + min: 0 + max: 1 trust: default_trust_level: diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 1dc0306dfb6..e1a44fa783c 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -267,10 +267,6 @@ class CookedPostProcessor rescue URI::InvalidURIError end - # only crop when the image is taller than 18:9 - # we only use 90% of that to allow for a small margin - MIN_RATIO_TO_CROP ||= (9.0 / 18.0) * 0.9 - def convert_to_link!(img) src = img["src"] return if src.blank? || is_a_hyperlink?(img) @@ -288,7 +284,10 @@ class CookedPostProcessor return if original_width <= width && original_height <= height return if original_width <= SiteSetting.max_image_width && original_height <= SiteSetting.max_image_height - if crop = (original_width.to_f / original_height.to_f < MIN_RATIO_TO_CROP) + crop = SiteSetting.min_ratio_to_crop > 0 + crop &&= original_width.to_f / original_height.to_f < SiteSetting.min_ratio_to_crop + + if crop width, height = ImageSizer.crop(original_width, original_height) img["width"] = width img["height"] = height