PhotoSauce.MagicScaler
repository·master·Indexed 20 days ago
https://github.com/saucecontrol/photosauceA high-performance image processing pipeline for .NET designed for speed and efficiency. It features high-quality image resizing using linear light processing, sharpening, and advanced resampling. The library is cross-platform (Windows and Linux) and supports various image formats through plugins such as PhotoSauce.NativeCodecs for HEIC, AVIF, JPEG, PNG, GIF, and JPEG XL.
What's inside PhotoSauce.MagicScaler
- PhotoSauce.MagicScaler is a high-performance image processing pipeline for .NET designed for speed and efficiency. It focuses on high-quality image resizing using best-of-breed algorithms, linear light processing, and sharpening. It is suitable for complex imaging tasks where performance is a priority.
Configure Animated PNG (APNG) support
masterAPNG decoding is enabled by default. However, APNG encoding is disabled by default due to an intermittent memory corruption bug in APNG-patched
libpng.To enable APNG encoding for testing, set the
PhotoSauce.NativeCodecs.Libpng.EnableApngEncodeswitch at application startup usingAppContext.SetSwitchor via yourruntimeconfig.jsonfile.How MagicScaler achieves high image quality
masterMagicScaler uses several advanced techniques to ensure superior resizing quality compared to standard libraries:
- Linear Light Processing: It performs resampling in linear light, which preserves highlights and prevents color shifts during blending.
- High-Quality Resampling: It defaults to high-quality resampling algorithms to prevent blurriness.
- Post-Resizing Sharpening: It automatically applies a sharpening step to compensate for the natural blurring that occurs during the resizing process.
Use the Libheif codec for decoding
masterOnceUseLibheif()has been called during configuration, the codec is active. MagicScaler will automatically detect and decode compatible HEIC and AVIF images without any further manual intervention.Understand MagicScaler's color management
masterMagicScaler handles embedded color profiles (like Display P3) to maximize compatibility while preserving the original gamut. By default, it embeds the ICC profile in the output image (Option 2), which preserves color accuracy but may result in larger file sizes compared to converting to sRGB. This prevents the 'washed-out' color issues common in libraries that ignore color profiles.Register the Giflib codec
masterTo enable GIF support via Giflib, call the
UseGiflibextension method within yourCodecManager.Configureaction during application startup.Note: By default, this plugin will remove or replace the built-in Windows GIF codec if it is detected in the pipeline.
using PhotoSauce.MagicScaler; using PhotoSauce.NativeCodecs.Giflib; CodecManager.Configure(codecs => { codecs.UseGiflib(); });Requirements for PhotoSauce.WebRSize
masterTo run PhotoSauce.WebRSize, your hosting environment must meet the following criteria:
- IIS Version: IIS 7 or higher.
- ASP.NET Version: ASP.NET 4.6.1 or higher (compatible with WebForms or MVC).
- App Pool Mode: The host Application Pool must be running in Integrated Pipeline Mode.
Enable kernel caching for WebRSize processed images
masterBy default, WebRSize requests are ineligible for
http.syskernel caching because they use query strings and URL rewriting. To achieve performance parity with static image files, you must explicitly configure akernelCachePolicyin your IIS configuration.This allows processed images to be served directly from the kernel cache once they have been written to the disk cache.
To enable this for
.jpgfiles, add a profile to yourweb.configusing the following configuration:<system.webServer> <caching> <profiles> <add extension=".jpg" kernelCachePolicy="CacheUntilChange" location="Any" /> </profiles> </caching> </system.webServer>Register the WebRSizeModule for request interception
masterThe
WebRSizeModule(anIHttpModule) manages processing and caching. Because image file extensions are often mapped to the unmanaged IIS static file handler, the module might not automatically receive event notifications viaPreApplicationStartMethodAttribute.To ensure the module intercepts requests, use one of these two methods:
1. Explicit Module Registration (Recommended)
Register the module explicitly in the
system.webServersection and omit thepreConditionattribute. This ensures the module sees all requests, managed or not.<system.webServer> <modules> <add name="WebRSize" type="PhotoSauce.WebRSize.WebRSizeModule" /> </modules> </system.webServer>2. Explicit Handler Mapping
Map specific image extensions to a managed
IHttpHandler(likeSystem.Web.StaticFileHandler). This allows the self-registered module to intercept the requests.Install and configure PhotoSauce.NativeCodecs.Libpng
masterPhotoSauce.NativeCodecs.Libpng is a MagicScaler plugin that wraps the
libpngnative codec. It provides PNG support for non-Windows platforms or enhanced capabilities on Windows.Requirements
- A compatible native binary must be present. The NuGet package includes binaries for:
- Windows: x86, x64, and ARM64
- Linux: glibc x64 and ARM64
- If you need to build for an unsupported platform, you must use a custom build of
libpng. A specializedvcpkgport is available in the PhotoSauce GitHub repository underbuild/vcpkg/ports/pspng.
<!-- No installation command provided in source, but refers to NuGet package -->- A compatible native binary must be present. The NuGet package includes binaries for:
Requirements for PhotoSauce.NativeCodecs.Giflib
masterTo use this plugin, a compatible native binary must be present. The NuGet package provides native binaries for the following platforms:
- Windows: x86, x64, and ARM64
- Linux: glibc x64 and ARM64
This plugin is primarily used to provide GIF support on non-Windows platforms where the auto-discoverable Windows GIF codec is unavailable.
Configure WebRSize in ASP.NET (IIS Integrated Pipeline)
masterWebRSize requires explicit opt-in for image folders and a registered
ConfigSection. Note that WebRSize currently only works with ASP.NET hosted with IIS Integrated Pipeline Mode; ASP.NET Core support is pending.To enable WebRSize, first register the
webrsizesection in yourweb.config:<configSections> <section name="webrsize" type="PhotoSauce.WebRSize.WebRSizeSection" /> </configSections>Then, provide a minimal configuration specifying a
diskCachepath and at least oneimageFoldersentry. ThediskCachefolder must exist and the App Pool identity must have write access to it.<configSections> <section name="webrsize" type="PhotoSauce.WebRSize.WebRSizeSection" /> </configSections> <webrsize> <diskCache path="/webrsizecache" /> <imageFolders> <add name="images" path="/images/" /> </imageFolders> </webrsize>