Class: Fractal::FractalsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/fractal/fractals_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject

Fields:

id - the random seed for this fractal. Required.
width - the width of this fractal. Default: 128
height - the height of this fractal. Default: 128
smoothness - the smoothness of this fractal. Lower values produce more jagged / turbulent
             results, higher values produce smoother results. Default: 2
high_color - the hex color code to use for high intensity values. Default: "ffffff"
low_color  - the hex color code to use for low intensity values. Default: "000000"
alpha      - if true, an alpha channel will be added. Lower intensity values will be more
             transparent. Default: false
island     - if true, an "island" fractal will be generated, such that its borders are
             guaranteed to have intensity values equal to 0. Default: false


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/fractal/fractals_controller.rb', line 21

def show
  cache_key = File.join(params[:id], params[:width].to_s, params[:height].to_s,
                        params[:smoothness].to_s, params[:alpha].to_s,
                        params[:high_color].to_s, params[:low_color].to_s,
                        params[:island].to_s)

  unless data = Rails.cache.read(cache_key)
    # The proc ensures that the image leaves scope prior to garbage collection,
    # thus ensuring that it will actually be collected. This is all to prevent
    # a memory leak, detailed here:
    # http://rubyforge.org/forum/forum.php?thread_id=1374&forum_id=1618
    proc {
      image = generate_image
      data = image.to_blob { self.format = 'PNG' }
      image.destroy!
    }.call
    GC.start
    
    Rails.cache.write cache_key, data
  end
  
  send_data data, :filename => "#{params[:id]}.png", :type => "image/png",
                  :disposition => 'inline'
end