add global thumb_process_option setting and thumb version for index

This commit is contained in:
邱博亞 2023-02-18 21:28:40 +08:00
parent 46df77f0e0
commit 16ff105bc7
10 changed files with 126 additions and 32 deletions

View File

@ -36,26 +36,62 @@ class GalleriesController < ApplicationController
end
def index
page = OrbitHelper.page
@album_setting = AlbumSetting.first
page_no = OrbitHelper.page_number
data_count = OrbitHelper.page_data_count
albums = Album.filter_by_categories.filter_by_tags.asc(:order).page(page_no).per(data_count)
album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
@image_version = 'thumb'
ModuleApp.all.select{|tmp| tmp.key.to_s=='gallery'}.each do |modile_app|
@show_option_items = modile_app.show_option_items rescue nil
end
if !(@show_option_items.nil?)
I18n.with_locale(:en) do
page.select_option_items.each do |select_option_item|
case select_option_item.field_name
when @show_option_items.keys[1].to_s
value = YAML.load(select_option_item.value)
tmp = value[:en]
if tmp == t('gallery.small_size')
@image_version = 'thumb'
elsif tmp == t('gallery.medium_size')
@image_version = 'thumb_large'
elsif tmp == t('gallery.large_size')
@image_version = 'mobile'
end
end
end
end
end
galleries = albums.collect do |a|
doc = Nokogiri::HTML(a.description.to_s)
alt_text = doc.text.empty? ? 'gallery image' : doc.text
colors = album_color_map[a.id] || []
thumb_src = a.cover_path || "/assets/gallery/default.jpg"
thumb_src = a.cover_path(@image_version)
if thumb_src
thumb_srcs = thumb_src.split('/')
thumb_srcs[-1] = thumb_srcs[-1].sub(/^\w+_/,"")
thumb_path = thumb_srcs[0..-2].join('/')
thumb_path_name = thumb_srcs[-1]
src = "#{thumb_path}/#{thumb_path_name}"
thumb_large_src = "#{thumb_path}/thumb_large_#{thumb_path_name}"
mobile_src = "#{thumb_path}/mobile_#{thumb_path_name}"
theater_src = "#{thumb_path}/theater_#{thumb_path_name}"
else
src = thumb_src = thumb_large_src = mobile_src = theater_src = "/assets/gallery/default.jpg"
end
{
"album-name" => a.name,
"album-description" => a.description,
"alt_title" => alt_text,
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
"thumb-src" => thumb_src,
"src" => thumb_src.sub('thumb_','') ,
"thumb-large-src" => thumb_src.sub("thumb_","thumb_large_"),
"mobile-src" => thumb_src.sub("thumb_","mobile_"),
"theater-src" => thumb_src.sub("thumb_","theater_"),
"src" => src,
"thumb-large-src" => thumb_large_src,
"mobile-src" => mobile_src,
"theater-src" => theater_src,
"album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'),
"album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color)
}

View File

@ -61,7 +61,7 @@ class Album
Position object at bottom-right of region
"""
tmp = self[:resize_gravity]
(tmp.blank? ? (AlbumSetting.first.resize_gravity.blank? ? "Center" : AlbumSetting.first.resize_gravity) : tmp) rescue 'Center'
tmp.blank? ? OrbitHelper::SharedHash['gallery'][:resize_gravity] : tmp
end
def self.find_by_param(input)
self.find_by(uid: input.to_s)
@ -98,10 +98,17 @@ class Album
end
tmp
end
def cover_path
def cover_path(thumb_version='thumb')
tmp = self['cover_path']
if tmp.nil?
tmp = (self.album_images.first.file.thumb.url rescue nil)
if tmp.nil? && self.album_images.first
case thumb_version
when 'thumb'
tmp = self.album_images.first.file.thumb.url
when 'thumb_large'
tmp = self.album_images.first.file.thumb_large.url
when 'mobile'
tmp = self.album_images.first.file.mobile.url
end
end
tmp
end

View File

@ -2,9 +2,17 @@ class AlbumSetting
include Mongoid::Document
include Mongoid::Timestamps
field :limit
field :resize_gravity, type: String, default: 'center'
field :resize_gravity, type: String, default: 'Center'
ResizeGravities = %w[Center NorthWest North NorthEast West East SouthWest South SouthEast]
field :album_card_background_color, default: ''
field :album_card_text_color, default: '#000000'
field :min_order, type: Integer, default: -1
field :thumb_process_option, type: Integer, default: 1
before_save do
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['gallery'][:thumb_process_option] = self.thumb_process_option
OrbitHelper::SharedHash['gallery'][:resize_gravity] = self.resize_gravity
end
end
end

View File

@ -177,12 +177,16 @@ class GalleryUploader < CarrierWave::Uploader::Base
end
def thumb_process(w,h)
now_album = model.album
case now_album.thumb_process_option
when 0
resize_and_pad(w, h, (transparent? ? :transparent : now_album.album_colors.first['color']), (now_album.resize_gravity rescue 'Center'))
thumb_process_option = now_album.thumb_process_option
if thumb_process_option == 0
thumb_process_option = OrbitHelper::SharedHash['gallery'][:thumb_process_option]
end
case thumb_process_option
when 1
limit_process(w, h)
resize_and_pad(w, h, (transparent? ? :transparent : now_album.album_colors.first['color']), (now_album.resize_gravity rescue 'Center'))
when 2
limit_process(w, h)
when 3
resize_to_fill(w, h)
end
end

View File

@ -122,7 +122,7 @@ input.minicolors-input{
<%= f.fields_for :album_colors do |album_color_form| %>
<!-- Frame Color -->
<div class="control-group thumb_process_option 0" style='display: flex;'>
<div class="control-group thumb_process_option 0 1" style='display: flex;'>
<label class="control-label muted"><%= t('gallery.frame_color') %></label>
<%= album_color_form.text_field :color , :class => 'input-block-level minicolors-input' %>
<span style='font-size: 20px;display: flex;align-items: center;'>
@ -130,7 +130,7 @@ input.minicolors-input{
<%= t('gallery.transparent') %>
</span>
</div>
<div class="control-group thumb_process_option 0">
<div class="control-group thumb_process_option 0 1">
<label class="control-label muted">
<%= t('gallery.thumb_resize_reference') %>
</label>

View File

@ -53,6 +53,12 @@
<label class="control-label muted"><%= t('gallery.album_limit_for_one_page') %></label>
<%= f.text_field :limit,:placeholder => t('gallery.blank_for_nil') %>
</div>
<div class="control-group" style='display: flex;'>
<label class="control-label muted"><%= t('gallery.thumb_process_option_trans') %></label>
<div class="controls">
<%= f.select :thumb_process_option, t('gallery.thumb_process_option').to_enum.with_index.collect{|v, i| [v, i]}[1..-1] %>
</div>
</div>
<div class="control-group" style='display: flex;'>
<label class="control-label muted"><%= t('gallery.thumb_resize_reference') %></label>
<%= f.select :resize_gravity, [["---#{t("default")}: #{t("gallery.gravity.#{AlbumSetting::ResizeGravities[0]}")}---",nil]]+AlbumSetting::ResizeGravities.collect{|v| [t("gallery.gravity.#{v}"),v]} %>

View File

@ -1,10 +1,15 @@
en:
gallery:
picture_thumb_size: Picture Showing Size
small_size: Small Size
medium_size: Medium Size
large_size: 16:9 Large Size
setting: Gallery Setting
advanced_setting: Advanced Setting
thumb_process_option_trans: Thumb Process Option
thumb_process_option:
- default
- resize and padding
- resize only
- resize and crop
@ -16,15 +21,15 @@ en:
album_card_text_color: Text Color for album card
thumb_resize_reference: Resize reference for thumb
gravity:
Center: center
NorthWest: top-left
North: top-center
NorthEast: top-right
West: left-center
East: right-center
SouthWest: left-bottom
South: bottom-center
SouthEast: bottom-right
Center: center
NorthWest: top-left
North: top-center
NorthEast: top-right
West: left-center
East: right-center
SouthWest: left-bottom
South: bottom-center
SouthEast: bottom-right
short-description: Short Description
album_limit_for_one_page: Amount of limit for album in one page
blank_for_nil: blank present no limit

View File

@ -1,10 +1,16 @@
zh_tw:
gallery:
picture_thumb_size: 圖片縮圖大小
small_size: 小張縮圖
medium_size: 中等縮圖
large_size: 16:9大縮圖
setting: 相簿設定
advanced_setting: 進階設定
thumb_process_option_trans: 縮圖處理選項
thumb_process_option:
- 預設
- 縮放並填充
- 僅縮放
- 縮放並裁切

View File

@ -90,6 +90,10 @@ Gem::Specification.new do |s|
s.summary = "Campus Gallery."
s.description = "Campus Galllery"
s.license = "MIT"
s.metadata = {
"_require" => "#{File.expand_path("../app/models/album_setting", __FILE__)}",
"global_hash" => "{resize_gravity: (AlbumSetting.first.resize_gravity rescue 'Center'), thumb_process_option: (AlbumSetting.first.thumb_process_option rescue 1)}"
}
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]

View File

@ -5,18 +5,36 @@ module Gallery
translate_data = Dir["#{Gallery::Engine.root}/config/locales/*.yml"] .map{|yaml_file| YAML.load(File.read(yaml_file))}
data = {}
key1 = {}
value1 = {}
value2 = {}
value3 = {}
key1_options = ['grid_style','card_style','slideshow_style']
key1_attrs = []
key1_options.each_with_index do |v, i|
key1_attrs[i] = {}
end
key2 = {}
key2_options = ['small_size','medium_size','large_size']
key2_attrs = []
key2_options.each_with_index do |v, i|
key2_attrs[i] = {}
end
translate_data.each do |t_data|
v = t_data.values
k = t_data.keys[0]
key1[k] = v[0]['gallery']['inner_page_layout']
value1[k] = v[0]['gallery']['grid_style']
value2[k] = v[0]['gallery']['card_style']
value3[k] = v[0]['gallery']['slideshow_style']
key1_options.each_with_index do |kk, i|
key1_attrs[i][k] = v[0]['gallery'][kk]
end
key2[k] = v[0]['gallery']['picture_thumb_size']
key2_options.each_with_index do |kk, i|
key2_attrs[i][k] = v[0]['gallery'][kk]
end
end
data[key1] = [value1,value2,value3]
data[key1] = key1_attrs
data[key2] = key2_attrs
rescue => e
puts ['error in gallery',e]
end