# Validates\_captcha not generating a captcha

**URL:** <https://www.ruby-forum.com/t/validates-captcha-not-generating-a-captcha/181515>\
**Category:** Rails\
**Created:** [January 13, 2010, 1:04am UTC](https://www.ruby-forum.com/t/validates-captcha-not-generating-a-captcha/181515 "2010-01-13T01:04:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 13, 2010, 1:04am UTC](https://www.ruby-forum.com/t/validates-captcha-not-generating-a-captcha/181515/1 "2010-01-13T01:04:33Z")

</div>

Newbie here.

This is not a question but a report on a work-around for a problem I  
detected with the gem validates\_captcha.

My environment is  
Windows, Vista, Rails  
Rails 2.3.5  
Ruby 1.8.6  
gem 1.3.5  
validates\_captcha-0.9.6

The problem:  
validates\_captcha is generating a default gif with the word “captcha” in  
it rather than a captcha with the value of a a random string (the  
captcha) in it.

The work-around:  
In module .\lib\validates\_captcha\image\_generator\simple.rb there is the  
following module

* * *

```
 def generate(captcha_code)
    image_width = captcha_code.length * 20 + 10

    cmd = []
    cmd << "convert -size #{image_width}x40 xc:grey84 -background

```

grey84 -fill black "

```
    captcha_code.split(//).each_with_index do |char, i|
      cmd << " -pointsize #{rand(8) + 15} "
      cmd << " -weight #{rand(2) == 0 ? '4' : '8'}00 "
      cmd << " -draw 'text #{5 + 20 * i},#{rand(10) + 20}

```

“#{char}”’ "  
end

```
    cmd << " -rotate #{rand(2) == 0 ? '-' : ''}5 -fill grey40 "

    captcha_code.size.times do
      cmd << " -draw 'line #{rand(image_width)},0

```

#{rand(image\_width)},60’ "  
end

```
    cmd << " gif:-"

    image_magick_command = cmd.join

    `#{image_magick_command}`
 end

```

* * *

The problem appears to be in the line  
cmd \<\< " gif:-"

replace this with  
cmd \<\< " gif:c:\tmp\SomeTempFile.gif"

and then read (binary) c:\tmp\SomeTempFile.gif and push that string out  
instead of `#{image_magick_command}` pumping the output to stdout.

SomeTempFile.gif should probably not be a unique name since several  
users might want to generate captchas at the same time. I keyed my name  
off of the captcha code but there may be a better way to do this. If  
anone has a good solution to this, I’d love to hear it.

Oh, do remember to delete the uniquely named file or your tmp directory  
will fill up quickly.

It works for me. Your mileage may vary.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 13, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/validates-captcha-not-generating-a-captcha/181515/2 "2010-01-13T16:33:43Z")

</div>

Ralph S. wrote:

> Newbie here.
> 
> This is not a question but a report on a work-around for a problem I  
> detected with the gem validates\_captcha.

When you are writing things like this, there is no need to begin your  
posts with “Newbie here.”

Actually, that’s probably true in general. If your level of  
understanding is that if a newbie, it will show in what you write. If  
it doesn’t show in what you write (as here), then you’re not in newbie  
territory. In either case, there is no point to beginning your posts  
this way. 🙂

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
