From 16ff105bc7d269904a4f2e84aa3e98d210033bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sat, 18 Feb 2023 21:28:40 +0800 Subject: [PATCH] add global thumb_process_option setting and thumb version for index --- app/controllers/galleries_controller.rb | 46 +++++++++++++++++++--- app/models/album.rb | 15 +++++-- app/models/album_setting.rb | 10 ++++- app/uploaders/gallery_uploader.rb | 12 ++++-- app/views/admin/galleries/_form.html.erb | 4 +- app/views/admin/galleries/setting.html.erb | 6 +++ config/locales/en.yml | 23 ++++++----- config/locales/zh_tw.yml | 6 +++ gallery.gemspec | 4 ++ lib/gallery/engine.rb | 32 +++++++++++---- 10 files changed, 126 insertions(+), 32 deletions(-) diff --git a/app/controllers/galleries_controller.rb b/app/controllers/galleries_controller.rb index cb8efc6..04a61a6 100644 --- a/app/controllers/galleries_controller.rb +++ b/app/controllers/galleries_controller.rb @@ -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) } diff --git a/app/models/album.rb b/app/models/album.rb index 84b8b2c..d1df3c2 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -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 diff --git a/app/models/album_setting.rb b/app/models/album_setting.rb index 9d5c2a5..6215400 100644 --- a/app/models/album_setting.rb +++ b/app/models/album_setting.rb @@ -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 \ No newline at end of file diff --git a/app/uploaders/gallery_uploader.rb b/app/uploaders/gallery_uploader.rb index 82bf8b7..839cd9e 100644 --- a/app/uploaders/gallery_uploader.rb +++ b/app/uploaders/gallery_uploader.rb @@ -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 diff --git a/app/views/admin/galleries/_form.html.erb b/app/views/admin/galleries/_form.html.erb index 7283f6a..14a1315 100644 --- a/app/views/admin/galleries/_form.html.erb +++ b/app/views/admin/galleries/_form.html.erb @@ -122,7 +122,7 @@ input.minicolors-input{ <%= f.fields_for :album_colors do |album_color_form| %> -
+
<%= album_color_form.text_field :color , :class => 'input-block-level minicolors-input' %> @@ -130,7 +130,7 @@ input.minicolors-input{ <%= t('gallery.transparent') %>
-
+
diff --git a/app/views/admin/galleries/setting.html.erb b/app/views/admin/galleries/setting.html.erb index 1df7a8e..d90f21a 100644 --- a/app/views/admin/galleries/setting.html.erb +++ b/app/views/admin/galleries/setting.html.erb @@ -53,6 +53,12 @@ <%= f.text_field :limit,:placeholder => t('gallery.blank_for_nil') %>
+
+ +
+ <%= f.select :thumb_process_option, t('gallery.thumb_process_option').to_enum.with_index.collect{|v, i| [v, i]}[1..-1] %> +
+
<%= f.select :resize_gravity, [["---#{t("default")}: #{t("gallery.gravity.#{AlbumSetting::ResizeGravities[0]}")}---",nil]]+AlbumSetting::ResizeGravities.collect{|v| [t("gallery.gravity.#{v}"),v]} %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b64c6a6..8359e04 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 1b54c49..8649a7d 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -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: + - 預設 - 縮放並填充 - 僅縮放 - 縮放並裁切 diff --git a/gallery.gemspec b/gallery.gemspec index ce8659a..db21c54 100644 --- a/gallery.gemspec +++ b/gallery.gemspec @@ -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/**/*"] diff --git a/lib/gallery/engine.rb b/lib/gallery/engine.rb index 37a1217..c3f24a6 100644 --- a/lib/gallery/engine.rb +++ b/lib/gallery/engine.rb @@ -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