class SharePhoto attr_reader :image SRC_PATH = Rails.root.join("app/assets/images/private_share/") def self.update_all source_files = SRC_PATH.join("src/backgrounds") Dir.foreach(source_files).each do |image_name| next if image_name == "." or image_name == ".." or image_name == ".DS_Store" share_photo = SharePhoto.new(image_name) share_photo.render share_photo.image.format "png" FileUtils.mv share_photo.image.path, SRC_PATH.join("v3").join(File.basename(image_name, ".*") + ".png") end end def self.update_vk_all source_files = SRC_PATH.join("vk/src/backgrounds") Dir.foreach(source_files).each do |image_name| next if image_name == "." or image_name == ".." or image_name == ".DS_Store" share_photo = SharePhoto.new(image_name, "vk/") share_photo.render_vk # share_photo.image.format "png" FileUtils.mv share_photo.image.path, SRC_PATH.join("vk2").join(image_name) end end def initialize(source_background, project_prefix = "") @project_prefix = project_prefix @background_path = private_share_path("backgrounds/#{source_background}") end def render @image = background @image = add_text(@image, " Путешествие\n на двоих\n от Hotellook") do |t| t.pointsize '80' t.interline_spacing "20" end hl_logo = MiniMagick::Image.open private_share_path('hl_logo.png') @image = add_logo(@image, hl_logo) cta = MiniMagick::Image.open private_share_path('cta_win.png') @image = add_button(@image, cta) label = MiniMagick::Image.open private_share_path('label.png') @image = add_label(@image, label) # @image.format "png" @image rescue MiniMagick::Error => error Rails.logger.error "Error generate image" Rollbar.report_exception(error) nil end def render_vk @image = background @image = add_text(@image, " Путешествие\n на двоих\n от Hotellook", x: 14, y: 50) do |t| t.pointsize '34' t.interline_spacing "8" end # hl_logo = MiniMagick::Image.open private_share_path('hl_logo.png') # @image = add_logo(@image, hl_logo) cta = MiniMagick::Image.open private_share_path('cta_win.png') @image = add_button(@image, cta) @image.format "jpg" @image rescue MiniMagick::Error => error Rails.logger.error "Error generate image" Rollbar.report_exception(error) nil end def add_text(image, message, x: 110, y: 170) text_color = "#ffffffff" escape = ->(str) { str.gsub("'", "\\\\'") } image.combine_options do |c| # NorthWest, North, NorthEast, West, Center, East, SouthWest, South, or SouthEast #c.gravity 'Center' # for more info see(http://www.imagemagick.org/Usage/fonts/#advanced) c.font font_path("Intro") c.family "Intro" c.weight "600" c.style "Normal" c.quality "100" c.undercolor "none" yield(c) if block_given? c.fill text_color c.draw("text #{x},#{y} '#{escape[message]}'") end end def background MiniMagick::Image.open @background_path end def add_logo(background, logo) background.composite(logo) do |c| c.compose "src-over" end end def add_button(background, logo) background.composite(logo) do |c| c.compose "src-over" end end def add_label(background, label) background.composite(label) do |c| c.compose "src-over" end end private def private_share_path(path) SRC_PATH.join(@project_prefix).join("src").join(path) end def font_path(file_name) "#{Rails.root}/app/assets/fonts/#{file_name}.ttf".tap do |path| raise "Error font not found: #{path}" unless File.exist?(path) end end end