mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-11-25 20:16:05 -05:00
each challenge provider now respects the size parameter when construction image
This commit is contained in:
@@ -58,9 +58,9 @@ public class FontFunCaptcha implements ChallengeProvider {
|
||||
return null;
|
||||
}
|
||||
|
||||
private byte[] fontFun(String captchaText, String level, String path) {
|
||||
private byte[] fontFun(final int width, final int height, String captchaText, String level, String path) {
|
||||
String[] colors = {"#f68787", "#f8a978", "#f1eb9a", "#a4f6a5"};
|
||||
BufferedImage img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB);
|
||||
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics2D = img.createGraphics();
|
||||
for (int i = 0; i < captchaText.length(); i++) {
|
||||
Font font = loadCustomFont(level, path);
|
||||
@@ -83,8 +83,11 @@ public class FontFunCaptcha implements ChallengeProvider {
|
||||
|
||||
public Challenge returnChallenge(String level, String size) {
|
||||
String secret = HelperFunctions.randomString(7);
|
||||
final int[] size2D = HelperFunctions.parseSize2D(size);
|
||||
final int width = size2D[0];
|
||||
final int height = size2D[1];
|
||||
String path = "./lib/fonts/";
|
||||
return new Challenge(fontFun(secret, "medium", path), "image/png", secret.toLowerCase());
|
||||
return new Challenge(fontFun(width, height, secret, "medium", path), "image/png", secret.toLowerCase());
|
||||
}
|
||||
|
||||
public boolean checkAnswer(String secret, String answer) {
|
||||
|
||||
@@ -21,10 +21,8 @@ import lc.misc.GifSequenceWriter;
|
||||
|
||||
public class PoppingCharactersCaptcha implements ChallengeProvider {
|
||||
private final Font font = new Font("Arial", Font.ROMAN_BASELINE, 48);
|
||||
private final int width = 250;
|
||||
private final int height = 100;
|
||||
|
||||
private Integer[] computeOffsets(final String text) {
|
||||
private Integer[] computeOffsets(final int width, final int height, final String text) {
|
||||
final var img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
final var graphics2D = img.createGraphics();
|
||||
final var frc = graphics2D.getFontRenderContext();
|
||||
@@ -41,7 +39,7 @@ public class PoppingCharactersCaptcha implements ChallengeProvider {
|
||||
return advances.toArray(new Integer[]{});
|
||||
}
|
||||
|
||||
private BufferedImage makeImage(final Consumer<Graphics2D> f) {
|
||||
private BufferedImage makeImage(final int width, final int height, final Consumer<Graphics2D> f) {
|
||||
final var img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
final var graphics2D = img.createGraphics();
|
||||
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
@@ -56,16 +54,16 @@ public class PoppingCharactersCaptcha implements ChallengeProvider {
|
||||
return HelperFunctions.randomNumber(-2, +2);
|
||||
}
|
||||
|
||||
private byte[] gifCaptcha(final String text) {
|
||||
private byte[] gifCaptcha(final int width, final int height, final String text) {
|
||||
try {
|
||||
final var byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
final var output = new MemoryCacheImageOutputStream(byteArrayOutputStream);
|
||||
final var writer = new GifSequenceWriter(output, 1, 900, true);
|
||||
final var advances = computeOffsets(text);
|
||||
final var advances = computeOffsets(width, height, text);
|
||||
final var prevColor = Color.getHSBColor(0f, 0f, 0.1f);
|
||||
IntStream.range(0, text.length()).forEach(i -> {
|
||||
final var color = Color.getHSBColor(HelperFunctions.randomNumber(0, 100)/100.0f, 0.6f, 1.0f);
|
||||
final var nextImage = makeImage((g) -> {
|
||||
final var nextImage = makeImage(width, height, (g) -> {
|
||||
if (i > 0) {
|
||||
final var prevI = (i - 1) % text.length();
|
||||
g.setColor(prevColor);
|
||||
@@ -102,7 +100,10 @@ public class PoppingCharactersCaptcha implements ChallengeProvider {
|
||||
|
||||
public Challenge returnChallenge(String level, String size) {
|
||||
final var secret = HelperFunctions.randomString(6);
|
||||
return new Challenge(gifCaptcha(secret), "image/gif", secret.toLowerCase());
|
||||
final int[] size2D = HelperFunctions.parseSize2D(size);
|
||||
final int width = size2D[0];
|
||||
final int height = size2D[1];
|
||||
return new Challenge(gifCaptcha(width, height, secret), "image/gif", secret.toLowerCase());
|
||||
}
|
||||
|
||||
public boolean checkAnswer(String secret, String answer) {
|
||||
|
||||
@@ -38,8 +38,8 @@ public class ShadowTextCaptcha implements ChallengeProvider {
|
||||
return answer.toLowerCase().equals(secret);
|
||||
}
|
||||
|
||||
private byte[] shadowText(String text) {
|
||||
BufferedImage img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB);
|
||||
private byte[] shadowText(final int width, final int height, String text) {
|
||||
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Font font = new Font("Arial", Font.ROMAN_BASELINE, 48);
|
||||
Graphics2D graphics2D = img.createGraphics();
|
||||
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
@@ -76,6 +76,9 @@ public class ShadowTextCaptcha implements ChallengeProvider {
|
||||
|
||||
public Challenge returnChallenge(String level, String size) {
|
||||
String secret = HelperFunctions.randomString(6);
|
||||
return new Challenge(shadowText(secret), "image/png", secret.toLowerCase());
|
||||
final int[] size2D = HelperFunctions.parseSize2D(size);
|
||||
final int width = size2D[0];
|
||||
final int height = size2D[1];
|
||||
return new Challenge(shadowText(width, height, secret), "image/png", secret.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,12 @@ public class HelperFunctions {
|
||||
random.setSeed(seed);
|
||||
}
|
||||
|
||||
public static int[] parseSize2D(final String size) {
|
||||
final String[] fields = size.split("x");
|
||||
final int[] result = {Integer.parseInt(fields[0]), Integer.parseInt(fields[1])};
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void setRenderingHints(Graphics2D g2d) {
|
||||
g2d.setRenderingHint(
|
||||
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
|
||||
Reference in New Issue
Block a user