Get the App
SLTechnology News&Howtos  ›  Development  › 

How to customize random CAPTCHA and fonts by LazyCaptcha

Shulou Source: shulou.com Published: 2022-06-01 04:41:33 10月03日 Update

This article will explain in detail how LazyCaptcha customizes random CAPTCHAs and fonts. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Introduction

LazyCaptcha is a graphic CAPTCHA module based on .net Standard 2.1imitating EasyCaptcha and SimpleCaptcha.

At present, Gitee 52star, if it is helpful to you, please don't be stingy with some stars.

one。 Custom random CAPTCHA (version 1.1.2 required)

Random here refers to CaptchaType random, movement random and so on. You can set any CaptchaOptions option value. Each time the verification code is refreshed, the effect is as follows:

I don't know if this need really exists.

1. Custom RandomCaptcha/// random verification code / public class RandomCaptcha: DefaultCaptcha {private static readonly Random random = new (); private static readonly CaptchaType [] captchaTypes = Enum.GetValues () Public RandomCaptcha (IOptionsSnapshot options, IStorage storage): base (options, storage) {} / update options / protected override void ChangeOptions (CaptchaOptions options) / / Random CAPTCHA type options.CaptchaType = captchaTypes [random.Next (0, captchaTypes.Length)] / / when doing arithmetic, CodeLength refers to the number of numbers if (options.CaptchaType.IsArithmetic ()) {options.CodeLength = 2;} else options.CodeLength = 4 / / if Chinese is included, use kaiti font, otherwise garbled if (options.CaptchaType.ContainsChinese ()) options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; options.ImageOption.FontSize = 24; options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; / / Random options.ImageOption.Animation = random.Next (2) = = 0 / / interference line random options.ImageOption.InterferenceLineCount = random.Next (1,4); / / Bubble random options.ImageOption.BubbleCount = random.Next (1,4); / / other options.} 2. Inject RandomCaptcha// memory storage, configure builder.Services.AddCaptcha (builder.Configuration) based on appsettings.json; / / enable random verification code builder.Services.Add (ServiceDescriptor.Scoped ()); II. Custom font

Use KG HAPPY font, as shown in the figure:

1. Looking for fonts

You can find your favorite fonts through fontspace.

two。 Put the font into the project and set it to embed the resource.

Of course, you can not as an embedded resource, it is also possible to put it in a specific directory, as long as you make some changes to the following ResourceFontFamilysFinder.

3. Define the help class for finding fonts. The example uses ResourceFontFamilysFinderpublic class ResourceFontFamilysFinder {private static Lazy _ fontFamilies = new Lazy (() = > {var fontFamilies = new List (); var assembly = Assembly.GetExecutingAssembly (); var names = assembly.GetManifestResourceNames (); if (names?.Length > 0 = = true) {var fontCollection = new FontCollection ()) Foreach (var name in names) {if (! name.EndsWith ("ttf")) continue; fontFamilies.Add (fontCollection.Add (assembly.GetManifestResourceStream (name));}} return fontFamilies;}); public static FontFamily Find (string name) return _ fontFamilies.Value.First (e = > e.Name = = name);}} 4. Set option// memory storage and configure builder.Services.AddCaptcha based on appsettings.json (builder.Configuration, options = > {/ / Custom font options.ImageOption.FontSize = 28; options.ImageOption.FontFamily = ResourceFontFamilysFinder.Find ("KG HAPPY"); / / the name of the font is displayed when you open the ttf file}) On "LazyCaptcha how to customize random CAPTCHA and font" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Tags: Fonts validation articles help memory movement effects more resources storage operations configuration not bad stingy practical inaction number garbled content can be passed through Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Shulou Information Shulou Tech Info Microsoft