# Rails Graphics Tools # (c) 2006 Jeffrey Davis class ToolsController < ApplicationController # Require the RMagick library and include its namespace require 'RMagick' include Magick def roundedcorners # Rounded Corners similar to Google's implementation for Groups Beta # Don't render a layout. render :layout => false # Check the cache to see if the image is already stored fragment = read_fragment( params ) # If not, produce the image. if fragment.nil? # Retrieve the background color bgcolor = params[:bc] # If the background color wasn't specified, use a transparent color if bgcolor == nil bgcolor = 'transparent' end # If this is not a CSS2 color, then it must be a hex value. if bgcolor.match('[g-zG-Z]') else bgcolor = "#" + bgcolor end # Retrieve the color fgcolor = params[:c] # If the foreground color wasn't specified, use lightgray if fgcolor == nil fgcolor = 'lightgray' end # If this is not a CSS2 color, then it must be a hex value. if fgcolor.match('[g-zG-Z]') else fgcolor = "#" + fgcolor end # Retrive the size of the image width = params[:w] height = params[:h] # If size wasn't specified, set it to the default of 8 if width == nil width = 8 else width = width.to_i end if height == nil height = 8 else height = height.to_i end # Create a new image and set the background color img = Image.new(256,256) { self.background_color = bgcolor } # Create a new drawing object gc = Draw.new # Draw a circle in the upper left corner gc.fill(fgcolor) gc.stroke(fgcolor) gc.circle(0,0,0,255) # Write the drawing to the image gc.draw(img) # Check the orientation of the corner, and flip/flop accordingly case params[:a] when 'tl' img.flip! img.flop! when 'bl' img.flop! when 'br' when 'tr' img.flip! else img.flip! img.flop! end # Scale the image to the correct width and height img.scale!(width,height) # Write the image to a blob blob = img.to_blob() { self.format = 'PNG' self.depth = 8 } # Write the image to the cache write_fragment(params, blob) fragment = blob end # Send the cached blob to the client send_data fragment, :type => 'image/png', :disposition => 'inline' end def captcha_test if session[:captcha] else chars = ("B".."D").to_a + ("F".."H").to_a + ("J".."N").to_a + ("P".."T").to_a + ("V".."Z").to_a captcha = "" 1.upto(6) { |i| captcha << chars[rand(chars.size-1)] } session[:captcha] = captcha end end def captcha # Renders a 6 character captcha # Generate a new captcha with the following before rendering this view #if session[:captcha] # else # chars = ("B".."D").to_a + ("F".."H").to_a + ("J".."N").to_a + ("P".."T").to_a + ("V".."Z").to_a # captcha = "" # 1.upto(6) { |i| captcha << chars[rand(chars.size-1)] } # session[:captcha] = captcha # end # Don't render a layout render :layout => false # Retrieve the background color bgcolor = params[:bc] # If the background color wasn't specified, use black if bgcolor == nil bgcolor = 'black' end # If this is not a CSS2 color, then it must be a hex value. if bgcolor.match('[g-zG-Z]') else bgcolor = "#" + bgcolor end # Retrieve the color fgcolor = params[:c] # If the foreground color wasn't specified, use white if fgcolor == nil fgcolor = 'white' end # If this is not a CSS2 color, then it must be a hex value. if fgcolor.match('[g-zG-Z]') else fgcolor = "#" + fgcolor end # Create a new image img = Image.new(100,50) { self.background_color = bgcolor } # Create a new drawing object gc = Draw.new # Draw a bunch of random horizontal and vertical lines gc.fill(fgcolor) gc.fill_opacity(0.2) 5.times { x = rand(100) gc.rectangle(x,0,x,50) } 5.times { y = rand(50) gc.rectangle(0,y,100,y) } gc.fill_opacity(0.5) 5.times { x = rand(100) gc.rectangle(x,0,x,50) } 5.times { y = rand(50) gc.rectangle(0,y,100,y) } # Dump the drawing to the image gc.draw(img) # Apply a random wave effect img = img.wave(rand(5)+1,rand(5)+1) img = img.resize(100,50) # Create a new drawing object for the text text = Draw.new text.fill(fgcolor) text.font_family('times') # Adjust this to your implementation text.pointsize(18) text.font_style(NormalStyle) text.font_weight(BoldWeight) text.text_anchor(CenterAlign) text.text_antialias(true) # Write the text to the image text.text(50,30,session[:captcha]) text.draw(img) # Add noise to the image img = img.add_noise(ImpulseNoise) # Adjust the quality to your liking blob = img.to_blob() { self.format = 'JPEG' self.quality = 30 } # Send the image to the client send_data blob, :type => 'image/jpeg', :disposition => 'inline' end def spinner # Generate an animated spinner # Don't render a layout render :layout => false # Check the cache to see if the image is already stored fragment = read_fragment( params ) # If not, produce the image. if fragment.nil? # Retrieve the background color bgcolor = params[:bc] # If the background color wasn't specified, use white if bgcolor == nil bgcolor = 'white' end # If this is not a CSS2 color, then it must be a hex value. if bgcolor.match('[g-zG-Z]') else bgcolor = "#" + bgcolor end # Retrieve the color color1 = params[:c1] # If the color wasn't specified, use gray if color1 == nil color1 = 'gray' end # If this is not a CSS2 color, then it must be a hex value. if color1.match('[g-zG-Z]') else color1 = "#" + fgcolor end # Retrieve the color color2 = params[:c2] # If the color wasn't specified, use lightgray if color2 == nil color2 = 'lightgray' end # If this is not a CSS2 color, then it must be a hex value. if color2.match('[g-zG-Z]') else color2 = "#" + fgcolor end # Retrieve the size size = params[:size] # If size wasn't specified, set it to the default of 8 if size == nil size = 25 else size = size.to_i end # Retrieve the delay delay = params[:delay] # If delay wasn't specified, set it to the default of 10 if delay == nil delay = 10 else delay = delay.to_i end # Create a new image img = Image.new(size,size) { self.background_color = bgcolor self.format = 'BMP' } # Create a new image list. anim = ImageList.new() # Generate each frame of the animation 8.times do |j| gc = Draw.new gc.stroke(color1) gc.stroke_width(2) 8.times do |i| gc.arc(3,3,size-4,size-4,(i*2)*22.50,((i*2)*22.50)+22.50) end gc.stroke(color2) gc.arc(3,3,size-4,size-4,(j*2)*22.50,((j*2)*22.50)+22.50) gc.draw(img) anim.from_blob(img.to_blob) img = Image.new(size,size) { self.background_color = bgcolor self.format = 'BMP' } end # Set the delay anim.delay = delay # Write the image to a blob blob = anim.to_blob() { self.format = 'GIF' } # Write the image to the cache write_fragment(params, blob) fragment = blob end # Send the cached blob to the client send_data fragment, :type => 'image/gif', :disposition => 'inline' end end