FIX: proper support for pixel ratios up to 3

This commit is contained in:
Régis Hanol 2015-05-29 09:57:54 +02:00
parent 4215690f40
commit c3227b69fa
3 changed files with 14 additions and 12 deletions

View file

@ -59,7 +59,7 @@ class UserAvatarsController < ApplicationController
return render_dot if size < 8 || size > 500 return render_dot if size < 8 || size > 500
if !Discourse.avatar_sizes.include?(size) && Discourse.store.external? if !Discourse.avatar_sizes.include?(size) && Discourse.store.external?
closest = Discourse.avatar_sizes.to_a.min{|a,b| (size-a).abs <=> (size-b).abs} closest = Discourse.avatar_sizes.to_a.min { |a,b| (size-a).abs <=> (size-b).abs }
return redirect_to cdn_path("/user_avatar/#{params[:hostname]}/#{user.username_lower}/#{closest}/#{version}.png") return redirect_to cdn_path("/user_avatar/#{params[:hostname]}/#{user.username_lower}/#{closest}/#{version}.png")
end end

View file

@ -17,14 +17,9 @@ module Jobs
self.send("create_thumbnails_for_#{type}", upload) self.send("create_thumbnails_for_#{type}", upload)
end end
PIXEL_RATIOS ||= [1, 2, 3]
def create_thumbnails_for_avatar(upload) def create_thumbnails_for_avatar(upload)
PIXEL_RATIOS.each do |pixel_ratio|
Discourse.avatar_sizes.each do |size| Discourse.avatar_sizes.each do |size|
size *= pixel_ratio OptimizedImage.create_for(upload, size, size, allow_animation: SiteSetting.allow_animated_avatars)
OptimizedImage.create_for(upload, max, max, allow_animation: SiteSetting.allow_animated_avatars)
end
end end
end end

View file

@ -78,11 +78,18 @@ module Discourse
@anonymous_top_menu_items ||= Discourse.anonymous_filters + [:category, :categories, :top] @anonymous_top_menu_items ||= Discourse.anonymous_filters + [:category, :categories, :top]
end end
PIXEL_RATIOS ||= [1, 2, 3]
def self.avatar_sizes def self.avatar_sizes
# Don't cache until we can get a notification from site settings to expire cache # TODO: should cache these when we get a notification system for site settings
set = Set.new(SiteSetting.avatar_sizes.split("|").map(&:to_i)) set = Set.new
# add retinas which are 2x dpi
set.to_a.each { |size| set << size * 2 } SiteSetting.avatar_sizes.split("|").map(&:to_i).each do |size|
PIXEL_RATIOS.each do |pixel_ratio|
set << size * pixel_ratio
end
end
set set
end end