Skip to content

Instantly share code, notes, and snippets.

@nathancolgate
Created March 20, 2012 00:02
Show Gist options
  • Save nathancolgate/2128709 to your computer and use it in GitHub Desktop.
Save nathancolgate/2128709 to your computer and use it in GitHub Desktop.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module Slideshowbob
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# JavaScript files you want as :defaults (application.js is always included).
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Needed for paperclip: http://github.com/thoughtbot/paperclip/issues/issue/247
# The documentation states that custom processors that inherit from Paperclip::Processor will be loaded
# automatically if placed in lib/paperclip_processors. But in Rails 3, this isn't the case.
# This issue is caused by the Paperclip gem being loaded before the Rails object is properly configured.
# Needs to be fixed properly in Paperclip itself.
config.after_initialize do
# copied from paperclip.rb: due to bundler, this doesn't seem to happen automagically anymore!?!
Dir.glob(File.join(File.expand_path(Rails.root), "lib","paperclip_processors", "*.rb")).each do |processor|
require processor # PVDB don't rescue LoadError... let it rip!
end
end
end
end
class Photo < ActiveRecord::Base
belongs_to :show
validates :ken_burns_start_zoom, :presence => true, :numericality => true
validates :ken_burns_end_zoom, :presence => true, :numericality => true
validates :duration, :presence => true, :numericality => true
# Paperclip
has_attached_file :image,
:storage => :s3,
:processors => [:thumbnail,:watermark],
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:path => "photos/:id/image/:normalized_image_file_name_:style.:extension",
:url => ":s3_alias_url",
:s3_host_alias => "#{S3Config[:bucket]}.s3.amazonaws.com",
:styles => {
:square_25 => {
:geometry => '25x25#',
:watermark_path => "#{Rails.root}/public/images/watermarks/square_25.png",
:watermark_position => "Center"
},
:square_1x => {
:geometry => '100x100#',
:watermark_path => "#{Rails.root}/public/images/watermarks/square_1x.png",
:watermark_position => "Center"
},
:scaled_1x => {
:geometry => '100x67',
:watermark_path => "#{Rails.root}/public/images/watermarks/scaled_1x.png",
:watermark_position => "Center"
},
:scaled_2x => {
:geometry => '220x147',
:watermark_path => "#{Rails.root}/public/images/watermarks/scaled_2x.png",
:watermark_position => "Center"
},
:scaled_3x => {
:geometry => '340x227',
:watermark_path => "#{Rails.root}/public/images/watermarks/scaled_3x.png",
:watermark_position => "Center"
},
:scaled_4x => {
:geometry => '460x307',
:watermark_path => "#{Rails.root}/public/images/watermarks/scaled_4x.png",
:watermark_position => "Center"
},
:scaled_6x => {
:geometry => '700x467',
:watermark_path => "#{Rails.root}/public/images/watermarks/scaled_6x.png",
:watermark_position => "Center"
},
:scaled_8x => {
:geometry => '940x627',
:watermark_path => "#{Rails.root}/public/images/watermarks/scaled_8x.png",
:watermark_position => "Center"
}
},
:whiny => true
# http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
Paperclip.interpolates :normalized_image_file_name do |attachment, style|
attachment.instance.normalized_image_file_name
end
Photo::KenBurnsPositions = ['Center','North','NorthEast','East','SouthEast','South','SouthWest','West','NorthWest']
# http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
def normalized_image_file_name
# Regex strips the file extension
self.image_file_name[/.*(?=\..+$)/].gsub( /[^a-zA-Z0-9_]/, '_')
end
def image_file_extension
self.image_file_name[/\.([^\.]+)$/]
end
end
# /lib/paperclip_processors/watermark.rb
# from http://github.com/ng/paperclip-watermarking-app with modifications
module Paperclip
class Watermark < Processor
def initialize file, options = {}, attachment = nil
super
@file = file
@format = options[:format]
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@watermark_path = options[:watermark_path]
@watermark_position = options[:watermark_position].nil? ? "SouthEast" : options[:watermark_position]
end
# Performs the conversion of the +file+ into a watermark. Returns the Tempfile
# that contains the new image.
def make
src = @file
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
dst.binmode
if @watermark_path
parameters = []
parameters << "-gravity"
parameters << @watermark_position
parameters << ":watermark"
parameters << ":source"
parameters << ":dest"
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
begin
success = Paperclip.run(
'composite',
parameters,
:source => "#{File.expand_path(src.path)}[0]",
:dest => File.expand_path(dst.path),
:watermark => File.expand_path(@watermark_path)
)
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
end
end
dst
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment