Skip to content

Instantly share code, notes, and snippets.

@helloqidi
Forked from jimhj/controller.rb
Created December 19, 2012 08:46
Show Gist options
  • Save helloqidi/4335356 to your computer and use it in GitHub Desktop.
Save helloqidi/4335356 to your computer and use it in GitHub Desktop.
require 'mini_magick'
class CustomAdmin::MiniCaptchaController < CustomAdmin::ApplicationController
before_filter :clear_captcha_session, :only => :mini_captcha
def mini_captcha
send_data(
generate_mini_captcha_image,
:type => 'image/jpeg',
:disposition => 'inline',
:filename => 'mini_captcha.jpg')
end
private
# TODO: 在内存中生成验证码图片
def generate_mini_captcha_image(opts = {})
label = SecureRandom.hex(3)
session[:captcha] = label
image = MiniMagick::Image.new('mini_captcha.jpg')
image.run_command("convert -pointsize 16 -kerning 1 +noise Laplacian -undercolor lightgrey label:#{label} mini_captcha.jpg")
image.to_blob
end
end
module CustomAdmin
module ApplicationHelper
def show_mini_captcha
raw "<img src='mini_captcha?v=#{Time.now.to_i}'/>"
end
end
end
map.namespace :custom_admin do |custom_admin|
custom_admin.mini_captcha 'mini_captcha', :controller => 'mini_captcha', :action => 'mini_captcha'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment