module Rai::Helpers

Public Instance Methods

get_resolution() click to toggle source
# File lib/rai/helpers.rb, line 3
def get_resolution
  settings.resolutions.sort!{|x,y| y <=> x }
  get_resolution_from_cookie || get_resolution_from_user_agent
end
get_resolution_from_user_agent() click to toggle source
# File lib/rai/helpers.rb, line 35
def get_resolution_from_user_agent
  is_mobile ? settings.resolutions.min : settings.resolutions.max
end
is_mobile() click to toggle source
# File lib/rai/helpers.rb, line 39
def is_mobile
  request.user_agent.downcase.match(/mobi|android|touch|mini/)
end
update_image() click to toggle source
# File lib/rai/helpers.rb, line 43
def update_image
  # Refresh the image if the cached version is too old
  if File.exists? @cached_image
    return unless settings.watch_cache
    return if File.mtime(@cached_image) >= File.mtime(@requested_image)
    File.delete @cached_image
  end

  begin
    image = MiniMagick::Image.open(@requested_image)
  rescue Exception => e
    halt(500, "Error loading image: #{e}")
  end

  # Do we need to downscale the image?
  if image[:width] <= @resolution
    @cached_image = @requested_image
    return
  end

  cache_dir = File.dirname(@cached_image)

  begin
    FileUtils.mkdir_p(cache_dir, :mode => 0755) unless File.directory?(cache_dir)
  rescue SystemCallError => e
    halt(500, "Unable to create caching directory. #{e}")
  rescue => e
    halt(500, "Unable to create caching directory. #{e}")
  end

  ratio      = image["%[fx:w/h]"].to_f
  new_width  = @resolution
  new_height = (new_width*ratio).ceil

  if params[:image_type] == 'jpg'
    image.combine_options do |c|
      c.interlace 'plane'
      c.quality settings.jpg_quality
    end
  end

  image.resize "#{new_width}x#{new_height}"

  if settings.sharpen
    radius    = 0.5
    sigma     = 0.5
    amount    = 1.0
    threshold = 0.02
    image.unsharp "#{radius}x#{sigma}+#{amount}+#{threshold}"
  end

  begin
    image.write @cached_image
  rescue Exception => e
    halt(500, "Error writing cache file: #{e}")
  end
end