add max_image_height site setting

This commit is contained in:
Régis Hanol 2013-08-26 00:24:24 +02:00
parent 7f185b6a6f
commit 32f717420d
7 changed files with 45 additions and 8 deletions

View file

@ -78,6 +78,7 @@ class SiteSetting < ActiveRecord::Base
setting(:queue_jobs, !Rails.env.test?) setting(:queue_jobs, !Rails.env.test?)
setting(:crawl_images, !Rails.env.test?) setting(:crawl_images, !Rails.env.test?)
setting(:max_image_width, 690) setting(:max_image_width, 690)
setting(:max_image_height, 500)
setting(:create_thumbnails, true) setting(:create_thumbnails, true)
client_setting(:category_featured_topics, 6) client_setting(:category_featured_topics, 6)
setting(:topics_per_page, 30) setting(:topics_per_page, 30)

View file

@ -470,6 +470,7 @@ en:
edit_history_visible_to_public: "Allow everyone to see previous versions of an edited post. When disabled, only staff members can view edit history." edit_history_visible_to_public: "Allow everyone to see previous versions of an edited post. When disabled, only staff members can view edit history."
delete_removed_posts_after: "Number of hours after which posts removed by the author will be deleted." delete_removed_posts_after: "Number of hours after which posts removed by the author will be deleted."
max_image_width: "Maximum allowed width of images in a post" max_image_width: "Maximum allowed width of images in a post"
max_image_height: "Maximum allowed height of images in a post"
category_featured_topics: "Number of topics displayed per category in the /categories page" category_featured_topics: "Number of topics displayed per category in the /categories page"
add_rel_nofollow_to_user_content: "Add rel nofollow to all submitted user content, except for internal links (including parent domains) changing this requires you update all your baked markdown with: \"rake posts:rebake\"" add_rel_nofollow_to_user_content: "Add rel nofollow to all submitted user content, except for internal links (including parent domains) changing this requires you update all your baked markdown with: \"rake posts:rebake\""
exclude_rel_nofollow_domains: "A comma delimited list of domains where nofollow is not added (tld.com will automatically allow sub.tld.com as well)" exclude_rel_nofollow_domains: "A comma delimited list of domains where nofollow is not added (tld.com will automatically allow sub.tld.com as well)"

View file

@ -446,6 +446,7 @@ fr:
crawl_images: "permettre la récupération des images provenant de sources tierces" crawl_images: "permettre la récupération des images provenant de sources tierces"
ninja_edit_window: "temps d'édition avant de sauvegarder une nouvelle version, en secondes." ninja_edit_window: "temps d'édition avant de sauvegarder une nouvelle version, en secondes."
max_image_width: "largeur maximale des images d'un message" max_image_width: "largeur maximale des images d'un message"
max_image_height: "hauteur maximale des images d'un message"
category_featured_topics: "nombre de discussions affichées dans la liste par catégories" category_featured_topics: "nombre de discussions affichées dans la liste par catégories"
add_rel_nofollow_to_user_content: "Ajouter rel nofollow à tous les contenus des utilisateurs, sauf les liens internes (incluant les domaines parents) Modifier ceci requiert une mise à jour de tout votre markdown (<code>rake posts:rebake</code>)" add_rel_nofollow_to_user_content: "Ajouter rel nofollow à tous les contenus des utilisateurs, sauf les liens internes (incluant les domaines parents) Modifier ceci requiert une mise à jour de tout votre markdown (<code>rake posts:rebake</code>)"
exclude_rel_nofollow_domains: "Une liste séparée par des virgules contenant les noms de domaines de premier niveau pour lesquels il faut ajouter un attribut nofollow (exemple.com va automatiquement fonctionner aussi avec sous.domaine.exemple.com)" exclude_rel_nofollow_domains: "Une liste séparée par des virgules contenant les noms de domaines de premier niveau pour lesquels il faut ajouter un attribut nofollow (exemple.com va automatiquement fonctionner aussi avec sous.domaine.exemple.com)"

View file

@ -117,7 +117,8 @@ class CookedPostProcessor
original_width, original_height = get_size(src) original_width, original_height = get_size(src)
return if original_width.to_i <= width && original_height.to_i <= height return if original_width.to_i <= width && original_height.to_i <= height
return if original_width.to_i <= SiteSetting.max_image_width return if original_width.to_i <= SiteSetting.max_image_width && original_height.to_i <= SiteSetting.max_image_height
return if is_a_hyperlink(img) return if is_a_hyperlink(img)
if upload if upload

View file

@ -2,16 +2,18 @@ module ImageSizer
# Resize an image to the aspect ratio we want # Resize an image to the aspect ratio we want
def self.resize(width, height) def self.resize(width, height)
max_width = SiteSetting.max_image_width.to_f
return if width.blank? || height.blank? return if width.blank? || height.blank?
max_width = SiteSetting.max_image_width.to_f
max_height = SiteSetting.max_image_height.to_f
w = width.to_f w = width.to_f
h = height.to_f h = height.to_f
return [w.floor, h.floor] if w < max_width return [w.floor, h.floor] if w <= max_width && h <= max_height
# Using the maximum width, resize the height retaining the aspect ratio ratio = [max_width / w, max_height / h].min;
[max_width.floor, (h * (max_width / w)).floor] [(w * ratio).floor, (h * ratio).floor]
end end
end end

View file

@ -123,6 +123,7 @@ describe CookedPostProcessor do
let(:cpp) { CookedPostProcessor.new(post) } let(:cpp) { CookedPostProcessor.new(post) }
before do before do
SiteSetting.stubs(:max_image_height).returns(2000)
SiteSetting.stubs(:create_thumbnails?).returns(true) SiteSetting.stubs(:create_thumbnails?).returns(true)
Upload.expects(:get_from_url).returns(upload) Upload.expects(:get_from_url).returns(upload)
cpp.stubs(:associate_to_post) cpp.stubs(:associate_to_post)

View file

@ -4,10 +4,11 @@ require 'image_sizer'
describe ImageSizer do describe ImageSizer do
before do before do
SiteSetting.expects(:max_image_width).returns(500) SiteSetting.stubs(:max_image_width).returns(500)
SiteSetting.stubs(:max_image_height).returns(500)
end end
it 'returns the same dimensions if the width is less than the maximum' do it 'returns the same dimensions when smaller than the maximums' do
ImageSizer.resize(400, 200).should == [400, 200] ImageSizer.resize(400, 200).should == [400, 200]
end end
@ -23,7 +24,7 @@ describe ImageSizer do
ImageSizer.resize('100', '101').should == [100, 101] ImageSizer.resize('100', '101').should == [100, 101]
end end
describe 'when larger than the maximum' do describe 'when larger than the maximum width' do
before do before do
@w, @h = ImageSizer.resize(600, 123) @w, @h = ImageSizer.resize(600, 123)
@ -39,4 +40,33 @@ describe ImageSizer do
end end
describe 'when larger than the maximum height' do
before do
@w, @h = ImageSizer.resize(123, 600)
end
it 'returns the maxmimum height if larger than the maximum' do
@h.should == 500
end
it 'resizes the width retaining the aspect ratio' do
@w.should == 102
end
end
describe 'when larger than the maximums' do
before do
@w, @h = ImageSizer.resize(533, 800)
end
it 'resizes both dimensions retaining the aspect ratio' do
@h.should == 500
@w.should == 333
end
end
end end