# Rmagick and captcha

**URL:** <https://www.ruby-forum.com/t/rmagick-and-captcha/79211>\
**Category:** Rails\
**Created:** [March 9, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211 "2006-03-09T21:51:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 9, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/1 "2006-03-09T21:51:44Z")

</div>

Has anyone implemented a captcha with ruby on rails and rmagick?  
I tried this example:  
[http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages](http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages)  
but I got an error:  
undefined method `size’ for #NoisyImage:0x34d8070

Thank you

* * *

Rodrigo D.

Iplan Networks Datos Personales  
removed\_email\_address@domain.invalid removed\_email\_address@domain.invalid  
www.iplan.com.ar www.rorra.com.ar  
5031-6303 15-5695-6027

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 9, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/2 "2006-03-09T22:25:18Z")

</div>

Hi Rodrigo,

I am the author of the article in your msg.  
I am not sure how you are getting that error. The NoisyImage class has  
no other methods defined other than the initialize code. Which gets  
called when you do session[:noisy\_image] = NoisyImage.new(6).

The image size is hard coded in the NoisyImage class and you can  
change it to whatever size you want on this line, in the NoisyImage  
class…

canvas.new\_image(32\*len, 50, Magick::TextureFill.new(granite))

If you’d like you can create some vars named height and width and use  
them instead.  
width = 32 (or whatever - this is a per character width which gets  
multiplied by the length of the code string, which you can also change  
in you call to NoisyImage.new(length) I have it hard coded at six  
chars.  
height = 50  
canvas.new\_image(width\*len, height, Magick::TextureFill.new(granite))

Hope it solves your problem.

bakki

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 9, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/3 "2006-03-09T23:32:41Z")

</div>

Hi bakki

I just copy and pastle your code and it’s working now, thank you, you  
did a great job, very easy to implement 😉

* * *

Rodrigo D.

Iplan Networks  
removed\_email\_address@domain.invalid  
www.iplan.com.ar  
5031-6303

Datos Personales  
removed\_email\_address@domain.invalid  
www.rorra.com.ar  
15-5695-6027

-----Mensaje original-----  
De: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] En nombre de Bakki K.  
Enviado el: Jueves, 09 de Marzo de 2006 06:25 p.m.  
Para: removed\_email\_address@domain.invalid  
Asunto: Re: [Rails] rmagick and captcha

Hi Rodrigo,

I am the author of the article in your msg.  
I am not sure how you are getting that error. The NoisyImage class has  
no other methods defined other than the initialize code. Which gets  
called when you do session[:noisy\_image] = NoisyImage.new(6).

The image size is hard coded in the NoisyImage class and you can  
change it to whatever size you want on this line, in the NoisyImage  
class…

canvas.new\_image(32\*len, 50, Magick::TextureFill.new(granite))

If you’d like you can create some vars named height and width and use  
them instead.  
width = 32 (or whatever - this is a per character width which gets  
multiplied by the length of the code string, which you can also change  
in you call to NoisyImage.new(length) I have it hard coded at six  
chars.  
height = 50  
canvas.new\_image(width\*len, height, Magick::TextureFill.new(granite))

Hope it solves your problem.

bakki

On 3/9/06, Rodrigo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Has anyone implemented a captcha with ruby on rails and rmagick?  
> I tried this example:

[http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages](http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages)

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 9, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/4 "2006-03-09T23:35:44Z")

</div>

There is something that I just can’t understand…

In the article, you wrote this:

def protect  
unless session[:noisy\_image]  
session[:noisy\_image] = NoisyImage.new(6)  
session[:code] = session[:noisy\_image].code  
end  
end

def code\_image  
image = session[:noisy\_image].code\_image  
send\_data image, :type =\> ‘image/jpeg’, :disposition =\> ‘inline’  
end

but if I write

def code\_image  
image = NoisyImage.new(6)  
send\_data image, :type =\> ‘image/jpeg’, :disposition =\> ‘inline’  
end

the image is not displaying anymore, and I don’t know why… (actually,  
that was the error that I was getting before)

* * *

Rodrigo D.

Iplan Networks  
removed\_email\_address@domain.invalid  
www.iplan.com.ar  
5031-6303

Datos Personales  
removed\_email\_address@domain.invalid  
www.rorra.com.ar  
15-5695-6027

-----Mensaje original-----  
De: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] En nombre de Bakki K.  
Enviado el: Jueves, 09 de Marzo de 2006 06:25 p.m.  
Para: removed\_email\_address@domain.invalid  
Asunto: Re: [Rails] rmagick and captcha

Hi Rodrigo,

I am the author of the article in your msg.  
I am not sure how you are getting that error. The NoisyImage class has  
no other methods defined other than the initialize code. Which gets  
called when you do session[:noisy\_image] = NoisyImage.new(6).

The image size is hard coded in the NoisyImage class and you can  
change it to whatever size you want on this line, in the NoisyImage  
class…

canvas.new\_image(32\*len, 50, Magick::TextureFill.new(granite))

If you’d like you can create some vars named height and width and use  
them instead.  
width = 32 (or whatever - this is a per character width which gets  
multiplied by the length of the code string, which you can also change  
in you call to NoisyImage.new(length) I have it hard coded at six  
chars.  
height = 50  
canvas.new\_image(width\*len, height, Magick::TextureFill.new(granite))

Hope it solves your problem.

bakki

On 3/9/06, Rodrigo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Has anyone implemented a captcha with ruby on rails and rmagick?  
> I tried this example:

[http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages](http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages)

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 10, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/5 "2006-03-10T14:23:54Z")

</div>

That was the error, thank you for your time

* * *

Rodrigo D.

Iplan Networks  
removed\_email\_address@domain.invalid  
www.iplan.com.ar  
5031-6303

Datos Personales  
removed\_email\_address@domain.invalid  
www.rorra.com.ar  
15-5695-6027

-----Mensaje original-----  
De: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] En nombre de Bakki K.  
Enviado el: Viernes, 10 de Marzo de 2006 12:44 a.m.  
Para: removed\_email\_address@domain.invalid  
Asunto: Re: [Rails] rmagick and captcha

On 3/9/06, Rodrigo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end

Here’s the problem. WHen you do …  
image = NoisyImage.new(6)  
you are creating a NoisyImage object. The actual image binary is  
contained in the @code\_image instance variable. If you see the code  
there are two accessor methods. First is :code (string) and the other  
:code\_image (binary image data). So you will have to send it in your  
example as…  
send\_data image.code\_image, :type =\> ‘image/jpeg’, :disposition =\>  
‘inline’

In my code I had done that earlier in the line…  
image = session[:noisy\_image].code\_image  
since I was saving the noisy\_image object in the session.

Hope that clarifies.

bakki

> the image is not displaying anymore, and I don’t know why…  
> (actually,  
> Datos Personales
> 
> Hope it solves your problem.
> 
> bakki
> 
> On 3/9/06, Rodrigo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Has anyone implemented a captcha with ruby on rails and rmagick?  
> > I tried this example:

[http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages](http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages)

> > removed\_email\_address@domain.invalid  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 10, 2006, 4:44am UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/6 "2006-03-10T04:44:16Z")

</div>

On 3/9/06, Rodrigo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end

Here’s the problem. WHen you do …  
image = NoisyImage.new(6)  
you are creating a NoisyImage object. The actual image binary is  
contained in the @code\_image instance variable. If you see the code  
there are two accessor methods. First is :code (string) and the other  
:code\_image (binary image data). So you will have to send it in your  
example as…  
send\_data image.code\_image, :type =\> ‘image/jpeg’, :disposition =\>  
‘inline’

In my code I had done that earlier in the line…  
image = session[:noisy\_image].code\_image  
since I was saving the noisy\_image object in the session.

Hope that clarifies.

bakki

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [March 10, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/rmagick-and-captcha/79211/7 "2006-03-10T20:16:02Z")

</div>

You are welcome. Glad to help.  
-bakki
