Skip to content

C# library that makes common image operations like resize and shape into circle easier. Works with asp.net core url middleware via url parameters.

License

Notifications You must be signed in to change notification settings

dustout/CommonImageActions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Common Image Actions

GitHub Actions Workflow Status GitHub Actions Workflow Status NuGet Version NuGet Downloads GitHub License GitHub Repo stars

Summary

A tuned image conversion library to add common image actions like resize and mask to your .Net and Asp.net core projects. This library is highly performant both in execution time and in memory use since it extends Microsoft libraries. This library should continue to increase in performance as Microsoft further improves their libraries.

Animated gif that shows the functionality of common image actions

Features

âś… Resize images
âś… Convert images to be circle or rounded rectangle
âś… PDF support
âś… Fast and memory efficient
âś… Create user profile placeholders where background auto changes based on name Example of user profile placeholders
âś… Resize through url in asp.net core (.jpg?w=50&m=zoom)
âś… Works with any project that supports .net standard
âś… Fluent interface

Getting Started (Asp.Net Core)

Install the NuGet Package

    Install-Package CommonImageActions.AspNetCore

Add UseCommonImageActions Middleware

Add the middleware to your startup file. Make sure that it is above app.UseStaticFiles(). You can watch a specific directory by setting the PathToWatch property in the CommonImageActionSettings object.

app.UseCommonImageActions(
    new CommonImageActionSettings()
    {
        PathToWatch = "/test",
    }
);

Getting Started

Install the NuGet Package

    Install-Package CommonImageActions.Core

Pass image bytes to the ImageProcessor.Process

Get the image bytes[] and pass those bytes to the ImageProcessor.Process function

byte[] testJpg = File.ReadAllBytes("test.jpg");
var result = await ImageProcessor.Process(testJpg)
               .Width(100)
               .Height(100)
               .Mode(ImageMode.Zoom)
               .Shape(ImageShape.Circle)
               .ToImageAsync();

Code Examples (Asp.net core)

Watch all images in all directories

app.UseCommonImageActions()

Set all images to be 50x50 png images for the /logos directory

app.UseCommonImageActions(
    new CommonImageActionSettings()
    {
        PathToWatch = "/logos",
        DefaultImageActions = new CommonImageActions.Core.ImageActions()
        {
            Height = 50,
            Width = 50,
            Format = SkiaSharp.SKEncodedImageFormat.Png
        }
    }
);

Generate user placeholders and cache the response to the disk

//the ChooseImageColorFromTextValue ensures that the following will have a different background color
//https://localhost:44302/profilepicture/profile.png?t=DustinGa
//https://localhost:44302/profilepicture/profile.png?t=DustinG
app.UseCommonImageActions(
    new CommonImageActionSettings()
    {
        PathToWatch = "/profilepicture",
        IsVirtual = true,
        UseDiskCache = true,
        DefaultImageActions = new ImageActions()
        {
            Height = 50,
            Width = 50,
            Format = SkiaSharp.SKEncodedImageFormat.Png,
            Shape = ImageShape.Circle,
            AsInitials = true,
            ChooseImageColorFromTextValue = true
        }
    }
);

Add to Asp.net Core and route to an external blob storage

Like AzureBlob or Amazon S3 or any other remote server

app.UseCommonImageActions(
    new CommonImageActionSettings()
    {
        PathToWatch = "/remote",
        RemoteFileServerUrl = "https://dustingamester.com/img/"
    }
);

Code Examples

Process an image using the fluent API

byte[] testJpg = File.ReadAllBytes("test.jpg");
var result = await ImageProcessor.Process(testJpg)
               .Width(100)
               .Height(100)
               .Mode(ImageMode.Zoom)
               .Shape(ImageShape.Circle)
               .ToImageAsync();

Process a pdf using the fluent API

byte[] testPdf = File.ReadAllBytes("test.pdf");
var result = await PdfProcessor.Process(testPdf)
               .Width(100)
               .Height(100)
               .Mode(ImageMode.Zoom)
               .Shape(ImageShape.Circle)
               .ToImageAsync();

URL Parameters (Asp.net Core)

Parameter Possible Values Description
width, w Integer Set the width of the image
height, h Integer Set the height of the image
mode, m Max, Fit, Zoom, Stretch Max: If both width and height are present then the image will be resized to the max of whatever parameter, and the width will scale accordingly.
Fit: When both width and height are present fit the image into the container without adjusting the ratio.
Zoom: When both width and height are present zoom the image in to fill the space.
Stretch (default): When both width and height are present stretch the image to fit the container.
shape, s Circle, Ellipse, RoundedRectangle Mask out the image to be of a certain shape.
corner, cr Integer The corner radius when shape is RoundedRectangle. Default is 10.
text, t String The text to display on the image
initials, in Boolean When true will only display initials of text. For example DustinG is displayed as DG.
color, c String (ffccff or blue) Set a color for the image
textColor, tc String (ffccff or blue) Set the color of the text
colorFromText, ft Boolean When true a color will be generated based on a hash of the text. The list of colors can be updated in ImageProcessor.BackgroundColours.
format, f Bmp, Gif, Ico, Jpeg, Png, Wbmp, Webp, Pkm, Ktx, Astc, Dng, Heif, Avif What format to export the resulting image as. Default is png.
password, pw String (pdf only) password to open pdf
page, p Integer (pdf only) what page to render. First page is 1.

Modes Visualized

Visual that shows the different mode options

CommonImageActionSettings (Asp.net Core)

Setting Description
PathToWatch What path to watch. For example /test will watch for images in the test directory
IsVirtual Avoid looking up the image and construct the image virtually (useful for profile pictures)
RemoteFileServerUrl The URL of the remote resource to pull images from. Often a blob storage like Amazon S3 or Azure Blob
UseDiskCache When true the system will save and return cached images. This can dramatically improve performance.
DiskCacheLocation Where to store and retrieve the DiskCache images from. This directory needs to be writable. If it is not the system will print errors to the console, but will still continue to run.
DefaultImageActions Set a default image action to be used on all requests against a particular path. Useful when you want all images in a directory to be a specific dimension.

Benchmarking

Use BenchmarkDotNet to compare the performance between Common Image Actions, ImageSharp, and ImageFlowDotNet. The benchmark files can be found in the /Benchmark directory if you wish to run the same test.

Single Image

When converting a single image we can see the AllocatedBytes is half of ImageSharp, and the performance is faster than ImageFlow.
Benchmarking results of CommonImageActions against a single image

Multiple Images

When converting multiple images we can see where CommonImageActions really shines. What takes 547ms in CommonImageActions takes 4,400ms in ImageSharp and 11,430ms in ImageFlow. We can also see that the allocated memory is significantly less than ImageSharp.
Benchmarking results of CommonImageActions against multiple images

About

C# library that makes common image operations like resize and shape into circle easier. Works with asp.net core url middleware via url parameters.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages