diff --git a/CommonImageActions.AspNetCore/CommonImageActionsMiddleware.cs b/CommonImageActions.AspNetCore/CommonImageActionsMiddleware.cs index e445040..cd79df0 100644 --- a/CommonImageActions.AspNetCore/CommonImageActionsMiddleware.cs +++ b/CommonImageActions.AspNetCore/CommonImageActionsMiddleware.cs @@ -352,6 +352,12 @@ public static ImageActions ConvertQueryStringToImageActions(string queryString, imageActions.Shape = shape; } + var isGrayscale = query["gray"] ?? query["g"]; + if (Boolean.TryParse(isGrayscale, out var asIsGrayscale)) + { + imageActions.IsGrayscale = asIsGrayscale; + } + //add unofficial support for other interpretations of mode based on feedback //example: I like zoom, but others feel it should be call crop, so just fall back to zoom // as a general catchall for any other interpretations diff --git a/CommonImageActions.Core/ImageActionQueryBuilder.cs b/CommonImageActions.Core/ImageActionQueryBuilder.cs index fef3c6a..ff8aa11 100644 --- a/CommonImageActions.Core/ImageActionQueryBuilder.cs +++ b/CommonImageActions.Core/ImageActionQueryBuilder.cs @@ -67,6 +67,12 @@ public ImageActionQueryBuilder Text(string text) return this; } + public ImageActionQueryBuilder IsGrayscale(bool isGrayscale) + { + _actions.IsGrayscale = isGrayscale; + return this; + } + public ImageActionQueryBuilder AsInitials(bool asInitials) { _actions.AsInitials = asInitials; diff --git a/CommonImageActions.Core/ImageActions.cs b/CommonImageActions.Core/ImageActions.cs index 99f8f09..02e7c87 100644 --- a/CommonImageActions.Core/ImageActions.cs +++ b/CommonImageActions.Core/ImageActions.cs @@ -51,6 +51,8 @@ public ImageActions(ImageActions defaults) public bool? ChooseImageColorFromTextValue { get; set; } + public bool? IsGrayscale { get; set; } + public SKEncodedImageFormat? Format { get; set; } public ImageMode? Mode { get; set; } diff --git a/CommonImageActions.Core/ImageProcessor.cs b/CommonImageActions.Core/ImageProcessor.cs index f9d9f51..ccefd14 100644 --- a/CommonImageActions.Core/ImageProcessor.cs +++ b/CommonImageActions.Core/ImageProcessor.cs @@ -237,7 +237,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions var a = new SKPath(); a.AddCircle(centerX, centerY, radius); - canvas.ClipPath(a, antialias:true); + canvas.ClipPath(a, antialias: true); } else if (imageActions.Shape == ImageShape.Ellipse) { @@ -246,7 +246,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions var centerY = imageActions.Height.Value / 2; var r = GetSKRectByWidthAndHeight(0, 0, imageActions.Width.Value, imageActions.Height.Value); a.AddOval(r); - canvas.ClipPath(a, antialias:true); + canvas.ClipPath(a, antialias: true); } else if (imageActions.Shape == ImageShape.RoundedRectangle) { @@ -261,7 +261,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions { a.AddRoundRect(r, CornerRadius, CornerRadius); } - canvas.ClipPath(a, antialias:true); + canvas.ClipPath(a, antialias: true); } } @@ -321,9 +321,20 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions var imagePaint = new SKPaint { - FilterQuality = SKFilterQuality.High + FilterQuality = SKFilterQuality.High, }; + if(imageActions.IsGrayscale.HasValue && imageActions.IsGrayscale == true) + { + imagePaint.ColorFilter = SKColorFilter.CreateColorMatrix(new float[] + { + 0.3f, 0.59f, 0.11f, 0, 0, + 0.3f, 0.59f, 0.11f, 0, 0, + 0.3f, 0.59f, 0.11f, 0, 0, + 0, 0, 0, 1, 0 + }); + } + //write to the canvas switch (imageActions.Mode) { @@ -335,7 +346,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions if (isOddRotation) { var drawRect = GetSKRectByWidthAndHeight(rotationOffsetX, rotationOffsetY, imageActions.Height.Value, imageActions.Width.Value); - canvas.DrawImage(newImage, drawRect, paint:imagePaint); + canvas.DrawImage(newImage, drawRect, paint: imagePaint); } else { @@ -357,7 +368,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions var fitOffsetY = (imageActions.Height.Value - fitScaledHeight) / 2f; var drawRect2 = GetSKRectByWidthAndHeight(fitOffsetX, fitOffsetY, fitScaledWidth, fitScaledHeight); - canvas.DrawImage(newImage, drawRect2); + canvas.DrawImage(newImage, drawRect2, paint: imagePaint); break; //zoom in and fill canvas while maintaing aspect ratio diff --git a/CommonImageActions.Pdf/PdfActionQueryBuilder.cs b/CommonImageActions.Pdf/PdfActionQueryBuilder.cs index 168a21f..3387f40 100644 --- a/CommonImageActions.Pdf/PdfActionQueryBuilder.cs +++ b/CommonImageActions.Pdf/PdfActionQueryBuilder.cs @@ -109,6 +109,12 @@ public PdfActionQueryBuilder Height(int height) return this; } + public PdfActionQueryBuilder IsGrayscale(bool isGrayscale) + { + _actions.IsGrayscale = isGrayscale; + return this; + } + public PdfActionQueryBuilder Page(int page) { _actions.Page = page; diff --git a/CommonImageActions.SampleAspnetCoreProject/wwwroot/index.html b/CommonImageActions.SampleAspnetCoreProject/wwwroot/index.html index 62434a5..6e390aa 100644 --- a/CommonImageActions.SampleAspnetCoreProject/wwwroot/index.html +++ b/CommonImageActions.SampleAspnetCoreProject/wwwroot/index.html @@ -210,6 +210,23 @@
Page 2
+
+

Grayscale

+
+ +
+
+

+ +

+
Grayscale
+ /test/thumbsUp.jpg?w=100&g=true +
+
+ +
+
+

JPEG EXIF

diff --git a/README.md b/README.md index bdc574b..f23f442 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ app.UseCommonImageActions( | ------------- | ------------- | ------------- | | width, w | Integer | Set the width of the image | | height, h | Integer | Set the height of the image | +| gray, g | Boolean | When true set image to grayscale | | 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. |