Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you support generating a captcha by specific text instead of random text? #20

Open
omega-pw opened this issue Jan 5, 2023 · 4 comments · May be fixed by #24
Open

Can you support generating a captcha by specific text instead of random text? #20

omega-pw opened this issue Jan 5, 2023 · 4 comments · May be fixed by #24

Comments

@omega-pw
Copy link

omega-pw commented Jan 5, 2023

I didn't find way to generate a captcha by specific text,can you add the support?

@sergorl
Copy link

sergorl commented May 24, 2023

Hi, guys, I support this question

@sergorl
Copy link

sergorl commented May 24, 2023

@omega4github , I found only this example of captcha generation from predefined symbols:

extern crate captcha;

use captcha::filters::{Cow, Noise, Wave};
use captcha::{Captcha, Geometry};

use std::path::Path;
fn main() {
    let mut c = Captcha::new();
    println!("{:?}", c.supported_chars());

    c.set_chars(&['0', '1', '2', '3', '4', '5', '6', '7', '8'])
        .add_chars(4)
        .apply_filter(Noise::new(0.2))
        .apply_filter(Wave::new(2.0, 20.0))
        .view(220, 120)
        .apply_filter(
            Cow::new()
                .min_radius(40)
                .max_radius(50)
                .circles(1)
                .area(Geometry::new(40, 150, 50, 70)),
        )
        .set_color([255, 128, 0]);
    c.save(Path::new("captcha.png")).expect("save failed");

    println!("{}", c.chars_as_string());
}

@Maklestiguan
Copy link

Maklestiguan commented Nov 23, 2023

Try implementing something like this:

/// sets text of captcha
    pub fn set_text(&mut self, vec: &[char]) -> &mut Self {
        for char in vec {
            if let Some((c, i)) = self.random_char_as_image(Some(char)) { 
                let x = self.text_area.right;
                let y = (self.text_area.bottom + self.text_area.top) / 2 - i.height() / 2;
                self.img.add_image(x, y, &i);

                self.text_area.top = min(self.text_area.top, y);
                self.text_area.right = x + i.width() - 1;
                self.text_area.bottom = max(self.text_area.bottom, y + i.height() - 1);
                self.chars.push(c);
            }
        }

        self
    }
    
    // accept optional char
    fn random_char_as_image(&mut self, c: Option<&char>) -> Option<(char, Image)> {
        match c {
            Some(val) => match self.font.png(*val) {
                Some(p) => Image::from_png(p).map(|i| (*val, i)),
                None => None,
            },
            None =>  match self.use_font_chars.choose(&mut self.rng) {
                None => None,
                Some(c) => match self.font.png(*c) {
                    None => None,
                    Some(p) => Image::from_png(p).map(|i| (*c, i)),
                },
            }
        }
    }
    

@a-nickol a-nickol linked a pull request Mar 22, 2024 that will close this issue
@Giwayume
Copy link

Giwayume commented Jan 1, 2025

@omega4github , I found only this example of captcha generation from predefined symbols:

c.set_chars(&['0', '1', '2', '3', '4', '5', '6', '7', '8'])
    .add_chars(4)

To be clear, this generates a captcha with 4 characters randomly selected from the set_chars list. There doesn't appear to be a built-in way to explicitly set the exact character sequence used for generation.

Unless you do something crazy like this?

c.set_chars(&['0'])
    .add_chars(1)
    .set_chars(&['2'])
    .add_chars(1)
    .set_chars(&['3'])
    .add_chars(1)
    .set_chars(&['4'])
    .add_chars(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants