use actix_web::{HttpResponse, Responder};
use base64::{engine::general_purpose, Engine as _};
use captcha::filters::{Cow, Noise, Wave};
use captcha::{Captcha, Geometry};
pub async fn captcha() -> impl Responder {
let mut c = Captcha::new();
c.add_chars(5)
.apply_filter(Noise::new(0.2))
.apply_filter(Wave::new(2.0, 20.0))
.view(220, 80)
.apply_filter(
Cow::new()
.min_radius(40)
.max_radius(50)
.circles(1)
.area(Geometry::new(40, 150, 50, 70)),
);
log::debug!(
"CAPTCHA with text {} written to captcha.png",
c.chars_as_string()
);
let image_data = match c.as_png() {
Some(data) => data,
None => {
return HttpResponse::InternalServerError().body("CAPTCHA generation failed");
}
};
let b64 = general_purpose::STANDARD.encode(&image_data);
HttpResponse::Ok()
.content_type("text/plain")
.body(format!("data:image/png;base64,{}", b64))
}