Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CommonImageActions.AspNetCore/CommonImageActionsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CommonImageActions.Core/ImageActionQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions CommonImageActions.Core/ImageActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
23 changes: 17 additions & 6 deletions CommonImageActions.Core/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CommonImageActions.Pdf/PdfActionQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions CommonImageActions.SampleAspnetCoreProject/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ <h5 class="card-title">Page 2</h5>
</div>
</div>

<div class="mb-5">
<h2>Grayscale</h2>
<div class="row">

<div class="card m-2" style="width: 18rem;">
<div class="card-body text-center">
<p>
<img src="/test/thumbsUp.jpg?w=100&g=true">
</p>
<h5 class="card-title">Grayscale</h5>
<code>/test/thumbsUp.jpg?w=100&g=true</code>
</div>
</div>

</div>
</div>

<div class="mb-5">
<h2>JPEG EXIF</h2>
<div class="row">
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br> **Fit**: When both width and height are present fit the image into the container without adjusting the ratio. <br> **Zoom**: When both width and height are present zoom the image in to fill the space. <br> **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. |
Expand Down
Loading