Created
December 18, 2012 10:32
-
-
Save jimhj/4326959 to your computer and use it in GitHub Desktop.
极简验证码生成 ImageMagick & MiniMagick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module CustomAdmin | |
| module ApplicationHelper | |
| def show_mini_captcha | |
| raw "<img src='mini_captcha?v=#{Time.now.to_i}'/>" | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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