Skip to content

Commit

Permalink
Reduce default image size to 220x70 and output JPEG to improve perfor…
Browse files Browse the repository at this point in the history
…mance (181.238 i/s -> 526.350 i/s).
  • Loading branch information
huacnlee committed Nov 5, 2022
1 parent 80b5e98 commit 9f5996f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ end

```
Warming up --------------------------------------
Generate image 18.000 i/100ms
Generate image 51.000 i/100ms
Calculating -------------------------------------
Generate image (2.6.x C Version) 181.238 (± 2.8%) i/s - 918.000 in 5.069689s
Generate image (3.x Rust version) 165.047 (±15.8%) i/s - 792.000 in 5.003023s
Generate image 526.350 (± 2.5%) i/s - 2.652k in 5.041681s
```
2 changes: 1 addition & 1 deletion app/controllers/ru_captcha/captcha_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
headers["Pragma"] = "no-cache"
data = generate_rucaptcha

opts = { disposition: "inline", type: "image/png" }
opts = { disposition: "inline", type: "image/jpeg" }
send_data data, opts
end
end
Expand Down
14 changes: 4 additions & 10 deletions ext/rucaptcha/src/captcha.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use image::{ImageBuffer, Rgb};
use imageproc::drawing::{draw_cubic_bezier_curve_mut, draw_text_mut};
use imageproc::noise::{gaussian_noise_mut, salt_and_pepper_noise_mut};
use imageproc::noise::gaussian_noise_mut;
use rand::{thread_rng, Rng};
use rusttype::{Font, Scale};
use std::io::Cursor;
Expand Down Expand Up @@ -195,8 +195,8 @@ impl CaptchaBuilder {
pub fn new() -> Self {
CaptchaBuilder {
length: 4,
width: 300,
height: 100,
width: 220,
height: 70,
complexity: 5,
}
}
Expand Down Expand Up @@ -247,15 +247,9 @@ impl CaptchaBuilder {
((5 * self.complexity) - 5) as u64,
);

salt_and_pepper_noise_mut(
&mut image,
((0.001 * self.complexity as f64) - 0.001) as f64,
(0.8 * self.complexity as f64) as u64,
);

let mut bytes: Vec<u8> = Vec::new();
image
.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png)
.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Jpeg)
.unwrap();

Captcha { text, image: bytes }
Expand Down

0 comments on commit 9f5996f

Please sign in to comment.