Tag: website


Time Sensitive Background

I just added a new feature to my website. If you notice, the top graphic is somewhat like a sky. Now it changes depending on the time of day you are visiting my website. However, I did not accomplish this by making multiple graphics, and changing them out. I actually wrote a new model for rails that uses the Imagemagick/MiniMagick image processing library to accomplish this. So every few minutes of the day when it is called it will actually tint the picture an appropriate shade. It also saves these images, so they only have to be generated once. Here is the code for the model:

require 'rubygems'
gem 'mini_magick'
require 'mini_magick'

class Tphoto
  attr_accessor :time, :file
  def initialize(time,file)
    @filename = file
    @noon = Time.parse("%Y-%m-%d " << "12:45:00")
    distance = time.to_i - @noon.to_i
    if(distance > 0) then
      time = time - (distance * 2)
    end
    @seconds = time.strftime("%H%M").to_i/10
    @viewurl = "/images/tphoto/#{@seconds}/#{@filename}"
    unless File.exist?("#{RAILS_ROOT}/public/images/tphoto/#{@seconds}/#{@filename}")
      generate_time(@seconds, @file)
    end
  end

  def generate_time(seconds, file)
    unless File.exist?("#{RAILS_ROOT}/public/images/tphoto/#{@seconds}")
       Dir.mkdir("#{RAILS_ROOT}/public/images/tphoto/#{@seconds}")
    end
    image = MiniMagick::Image.from_file("#{RAILS_ROOT}/public/images/tphoto/#{@filename}")
      image.combine_options do |c|
        c.fill("#FFFFFF")
        c.tint(seconds+10)
    end
    image.write("#{RAILS_ROOT}/public/images/tphoto/#{@seconds}/#{@filename}")
  end

  def viewurl
    return @viewurl
  end

  def filepath
    return "#{RAILS_ROOT}/public/images/tphoto/#{@seconds}/#{@filename}"
  end

end

and here is the code for the controller:

class TphotoController < ApplicationController
  def index
    @time = Time.now
    @tphoto = Tphoto.new(@time, "monday.jpg")
    @response.headers['Last-Modified'] = Time.now.httpdate
    @response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
    @response.headers['Pragma'] = 'no-cache'
    @response.headers['Expires'] = 'Thu, 19 Nov 1981 08:52:00 GMT'
    send_file @tphoto.filepath, :type => 'image/jpeg', :disposition => 'inline'
  end
end

Tag cloud

  1. 1 entries are tagged with 2007
  2. 2 entries are tagged with applevalley
  3. 1 entries are tagged with archlinux
  4. 1 entries are tagged with automation
  5. 1 entries are tagged with bigsur
  6. 5 entries are tagged with burningman
  7. 3 entries are tagged with code
  8. 1 entries are tagged with cplusplus
  9. 1 entries are tagged with desert
  10. 1 entries are tagged with drinks
  11. 1 entries are tagged with dspam
  12. 2 entries are tagged with dunes
  13. 1 entries are tagged with energy
  14. 1 entries are tagged with esplanade
  15. 5 entries are tagged with europe
  16. 1 entries are tagged with evdo
  17. 1 entries are tagged with flv
  18. 1 entries are tagged with gadgets
  19. 1 entries are tagged with government
  20. 2 entries are tagged with losangeles
  21. 1 entries are tagged with motivation
  22. 1 entries are tagged with moving
  23. 1 entries are tagged with newserver
  24. 1 entries are tagged with rails
  25. 2 entries are tagged with recipe
  26. 5 entries are tagged with ruby
  27. 1 entries are tagged with salmon
  28. 3 entries are tagged with site
  29. 1 entries are tagged with snow
  30. 6 entries are tagged with travel
  31. 1 entries are tagged with trip
  32. 1 entries are tagged with website
  33. 2 entries are tagged with work
  34. 1 entries are tagged with x4200
  35. 3 entries are tagged with yz250

Twitter Updates

Links to check out

Subscribe RSS