<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vinsol - Leading Ruby on Rails Development and Consulting Firm in India &#187; image</title>
	<atom:link href="http://vinsol.com/blog/category/image/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinsol.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 26 Jul 2010 08:37:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Captcha in Ruby on Rails &#8211; Customize the use of captcha in the plugin validates_captcha</title>
		<link>http://vinsol.com/blog/2006/10/24/captcha-in-ruby-on-rails-customize-the-use-of-captcha-in-the-plugin-validates_captcha/</link>
		<comments>http://vinsol.com/blog/2006/10/24/captcha-in-ruby-on-rails-customize-the-use-of-captcha-in-the-plugin-validates_captcha/#comments</comments>
		<pubDate>Tue, 24 Oct 2006 18:59:40 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[captcha]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[validations]]></category>

		<guid isPermaLink="false">http://ajaxonrails.wordpress.com/2006/10/24/captcha-in-ruby-on-rails-customize-the-use-of-captcha-in-the-plugin-validates_captcha/</guid>
		<description><![CDATA[To implement captcha in RubyonRails, validates_captcha plugin can be a good option but a small customization i need with this plugin was to use it on some specific action and not to be validated the captcha field every time an instance of the model is saved or updated.
Here is a small work-around for its customization&#8230;
How [...]


Related posts:<ol><li><a href='http://vinsol.com/blog/2010/04/01/ssl-checklist-for-rails-applications/' rel='bookmark' title='Permanent Link: SSL checklist for Ruby on Rails Applications'>SSL checklist for Ruby on Rails Applications</a> <small>Cross posted from darthsid The purpose of SSL is to...</small></li>
<li><a href='http://vinsol.com/blog/2010/04/09/introduction-to-active-scaffold-part-i/' rel='bookmark' title='Permanent Link: Introduction to Active Scaffold  Part I'>Introduction to Active Scaffold  Part I</a> <small>I originally wrote this article for fifth issue of Rails...</small></li>
<li><a href='http://vinsol.com/blog/2009/09/04/how-to-integrate-tinymce-with-redbox-in-5-steps/' rel='bookmark' title='Permanent Link: How to Integrate TinyMce with Redbox in 5 steps ?'>How to Integrate TinyMce with Redbox in 5 steps ?</a> <small>TinyMCE is a platform independent web based Javascript HTML WYSIWYG...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'>
<p>Hello Everyone !!<br />
I have released a captcha plugin <a href="http://expressica.com/2007/02/06/simple-captcha-released-the-captcha-for-rails-applications/"><b>Simple Captcha</b></a>. It is really simple to implement, and provides a cool feature of multiple styles of images.</p>
<p>
<hr />
<b>Previous Post for validates_captcha</b></p>
<hr />
To implement captcha in RubyonRails, <a href="http://svn.2750flesk.com/validates_captcha"><strong>validates_captcha</strong></a> plugin can be a good option but a small customization i need with this plugin was to use it on some specific action and not to be validated the captcha field every time an instance of the model is saved or updated.<br />
Here is a small work-around for its customization&#8230;<br />
<strong>How to use customized captcha in RoR ?</strong><br />
Install the plugin validates_captcha in your rails application by running this command from the root of your application</p>
<pre>
ruby script/plugin install http://svn.2750flesk.com/validates_captcha</pre>
<p>Make sure that you can now see the directory <strong>vedor/plugins/validates_captcha</strong>.</p>
<p>Now run these commands from your application root to make the image and data directories</p>
<pre>
  ruby script/generate captcha store_directory
  ruby script/generate captcha image_directory</pre>
<p>Here is the complete <a href="http://dev.2750flesk.com/validates_captcha/"><strong>API</strong></a> for the usage of this plugin. I am describing the same idea as given in this API but in a bit more specific means.</p>
<p>Lets consider a model <strong>User</strong> in which we will implement the captcha.<br />
Add the following code in the file <strong>app/models/user.rb</strong></p>
<pre>
  class User &lt; ActiveRecord::Base

    validates_captcha :if =&gt; :request_captcha_validation?
    attr_accessor :request_captcha_validation

    def request_captcha_validation?
      (self.request_captcha_validation==true)? true : false
    end

  end</pre>
<h3>Handle View and Controller</h3>
<p><strong>Add the code in the view inside your existing form.</strong></p>
<pre>
  &lt;% c = prepare_captcha :type =&gt; :image -%&gt;
  &lt;%= captcha_hidden_field c, 'user' %&gt;
  &lt;%= captcha_image_tag c %&gt;
  &lt;%= captcha_label 'user', 'Type in the text from the image above' %&gt;
  &lt;%= captcha_text_field 'user' %&gt;</pre>
<p><strong>Your controller will look like</strong></p>
<pre>
  def save
    # the line in bold represents that you need captcha validation.
    # if captcha validation is not required then remove this line from your controller.
    @user = User.new(params[:user])
    <strong>@user.request_captcha_validation = true</strong>
    @user.save
  end</pre>
<p>However image is too noisy and it contains repeated strings.<br />
To improve the quality of images generated by the plugin validates_captcha visit <a href="http://ajaxonrails.wordpress.com/2006/10/17/how-to-improve-the-image-quality-and-generate-random-string-image-in-the-plugin-validates_captcha/"><strong>Here</strong></a>.</p>
</div>


<p>Related posts:<ol><li><a href='http://vinsol.com/blog/2010/04/01/ssl-checklist-for-rails-applications/' rel='bookmark' title='Permanent Link: SSL checklist for Ruby on Rails Applications'>SSL checklist for Ruby on Rails Applications</a> <small>Cross posted from darthsid The purpose of SSL is to...</small></li>
<li><a href='http://vinsol.com/blog/2010/04/09/introduction-to-active-scaffold-part-i/' rel='bookmark' title='Permanent Link: Introduction to Active Scaffold  Part I'>Introduction to Active Scaffold  Part I</a> <small>I originally wrote this article for fifth issue of Rails...</small></li>
<li><a href='http://vinsol.com/blog/2009/09/04/how-to-integrate-tinymce-with-redbox-in-5-steps/' rel='bookmark' title='Permanent Link: How to Integrate TinyMce with Redbox in 5 steps ?'>How to Integrate TinyMce with Redbox in 5 steps ?</a> <small>TinyMCE is a platform independent web based Javascript HTML WYSIWYG...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/10/24/captcha-in-ruby-on-rails-customize-the-use-of-captcha-in-the-plugin-validates_captcha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to improve the image quality and generate random string image in the plugin validates_captcha</title>
		<link>http://vinsol.com/blog/2006/10/17/how-to-improve-the-image-quality-and-generate-random-string-image-in-the-plugin-validates_captcha/</link>
		<comments>http://vinsol.com/blog/2006/10/17/how-to-improve-the-image-quality-and-generate-random-string-image-in-the-plugin-validates_captcha/#comments</comments>
		<pubDate>Tue, 17 Oct 2006 13:50:23 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[captcha]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://ajaxonrails.wordpress.com/2006/10/17/how-to-improve-the-image-quality-and-generate-random-string-image-in-the-plugin-validates_captcha/</guid>
		<description><![CDATA[Validates captchais a good pluging to implement captcha in your rails application.
However i found that there is repetition of the string of the image and the quality of image is not that good. To get a good quality image and random string replace the code of the file /vendor/plugins/validates_captcha/lib/captcha_challenge.rb with the following code&#8230;

require 'digest/sha1'
module AngryMidgetPluginsInc [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><a href="http://svn.2750flesk.com/validates_captcha" title="validates_captcha">Validates captcha</a>is a good pluging to implement captcha in your rails application.<br />
However i found that there is repetition of the string of the image and the quality of image is not that good. To get a good quality image and random string replace the code of the file <strong>/vendor/plugins/validates_captcha/lib/captcha_challenge.rb</strong> with the following code&#8230;</p>
<pre><code>
require 'digest/sha1'
module AngryMidgetPluginsInc #:nodoc:
# CaptchaChallenge
class CaptchaChallenge
include CaptchaConfig
extend CaptchaConfig
DEFAULT_TTL = 1200#Lifetime in seconds. Default is 20 minutes.

attr_reader :id, :created_at
attr_accessor :ttl
  def initialize(options = {}) #:nodoc:
    generate_id
    options = {
    :ttl =&gt; config['default_ttl'] || DEFAULT_TTL}.update(options)
    self.ttl = options[:ttl]
    @created_at = Time.now
    self.class.prune
  end

# Implement in subclasses.
  def correct? #:nodoc:
    raise NotImplementedError
  end

private

  def generate_id #:nodoc:
    self.id = Digest::SHA1.hexdigest(Time.now.to_s+rand.to_s)
  end

  def id=(i) #:nodoc:
    @id = i
  end

  def write_to_store #:nodoc:
    store.transaction{
    store[:captchas] = Array.new unless store.root?(:captchas)
    store[:captchas] &lt;&lt;&gt; c.created_at+c.ttl
    store[:captchas].delete_at(i)
  end
}
end
}
end#prune
end#class &lt;&lt; words =" 'gorilla" default_dir =" 'captcha'#public/images/captcha" write_dir =" File.join(RAILS_ROOT," default_filetype =" 'jpg'" options =" {})" options =" {" string =""&gt; config['words'] ? config['words'][rand(config['words'].size)] : WORDS[rand(WORDS.size)],
:dir =&gt; config['default_dir'] || DEFAULT_DIR,
:filetype =&gt; config['default_filetype'] || DEFAULT_FILETYPE
}.update(options)

#changed
self.string = Digest::SHA1.hexdigest(Time.now.to_s)[0..4] #options[:string]
self.dir = options[:dir]
self.filetype = options[:filetype]
self.filename = options[:filename] || generate_filename

write_to_store
end

# Generates the image.
def generate(options = {})
options = {
:fontsize =&gt; 50,
:padding =&gt; 20,
:color =&gt; '#000',
:background =&gt; '#fff',
:fontweight =&gt; 'bold',
:rotate =&gt; true
}.update(options)

options[:fontweight] = case options[:fontweight]
when 'bold' then 700
else 400
end

#added
text = Array.new
0.upto(4) do |i|
text[i] = Magick::Draw.new
text[i].pointsize = options[:fontsize]
text[i].font_weight = 300 #options[:fontweight]
text[i].fill = 'blue' #options[:color]
text[i].gravity = Magick::CenterGravity
text[i].rotation = (rand(2)==1 ? rand(30) : -rand(30)) if options[:rotate]
end

metric = text[2].get_type_metrics(self.string)
#add bg
canvas = Magick::ImageList.new
fill = Magick::HatchFill.new('white','lime')
x = metric.width+options[:padding]
y = metric.height+options[:padding]

#ADDING LINES

img1 = Magick::Image.new(x,y,fill)
gc = Magick::Draw.new
gc.stroke_linejoin('round')
gc.stroke('blue')
gc.stroke_width(2)
gc.line(rand(x),rand(y),rand(x),rand(y))
gc.line(rand(x),rand(y),rand(x),rand(y))
gc.line(rand(x),rand(y),rand(x),rand(y))
gc.draw(img1)
canvas &lt;&lt; background_color =" '#000F'" y_loc =" rand(y)" p =" Magick::Pixel.from_color(options[:background])" opacity =" Magick::MaxRGB" background_color =" p" image =" canvas.flatten_images.blur_image(1)" dir =" self.dir," filename =" self.filename)" downcase ="="&gt;public/images.
def file_path
File.join(dir,filename)
end

class &lt;&lt;&gt; c.created_at+c.ttl
if File.exists?(File.join(WRITE_DIR, c.file_path))
begin
File.unlink(File.join(WRITE_DIR, c.file_path))
rescue Exception
end
end
end
}
end
}
super
end#prune
end#class &lt;&lt; image="(i)" image =" i"&gt;</code></pre>
<p>visit <strong><a href="http://ajaxonrails.wordpress.com/2006/10/24/captcha-in-ruby-on-rails-customize-the-use-of-captcha-in-the-plugin-validates_captcha/" title="captcha in rails">Here</a></strong> to view the customized use of the plugin <strong>validates_captcha</strong> in RoR.
</p>
]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/10/17/how-to-improve-the-image-quality-and-generate-random-string-image-in-the-plugin-validates_captcha/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
