require 'RMagick' include Magick class ImageController < ApplicationController # # Simple example generating an image with each browser request # def generate fill = Magick::GradientFill.new(0, 0, 0, 0, '#d33', '#f99') canvas = Magick::Image.new(400, 300,fill) canvas.format = "PNG" text = Magick::Draw.new text.pointsize = 25 text.font_weight = Magick::BoldWeight text.gravity = Magick::CenterGravity text.fill='black' text.annotate(canvas, 0,0,2,2, "Ruby Meetup at #{Time.now.strftime('%I:%M:%S %p')}") send_data canvas.to_blob, :type => 'image/png' end # # Simple example of caching a generated image on the file system # def generate_and_cache directory='public/image_gen' path=File.join(directory, 'test.png') # make sure the directory exists FileUtils.mkdir_p directory if ! File.exists? directory # create the image if it doesn't exist if !File.exist?(path) canvas = Magick::Image.new(500, 200, Magick::HatchFill.new('white','lightcyan2')) text = Magick::Draw.new text.annotate(canvas, 0, 0, 0, 40, (Time.now.strftime("%m/%d %I:%M:%S %p"))) { self.gravity = Magick::SouthWestGravity self.pointsize = 25 self.fill = 'black' } canvas.write(path) end # send the image to the browser send_file path, :type => 'image/png', :disposition => 'inline' end # # Image Manipulation # def rotate # load an existing image image = Magick::ImageList.new("public/image_gen/iphone.gif") # manipulate it and send it to the browser image = image.rotate(params[:degrees].to_i) send_data image.to_blob, :type => 'image/gif' end end