Skip to content

Commit 1765392

Browse files
authored
Merge pull request #46 from dustout/feature/20250525-add-grayscale-support
Added grayscale support
2 parents ba5be38 + 68e2c81 commit 1765392

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

CommonImageActions.AspNetCore/CommonImageActionsMiddleware.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ public static ImageActions ConvertQueryStringToImageActions(string queryString,
352352
imageActions.Shape = shape;
353353
}
354354

355+
var isGrayscale = query["gray"] ?? query["g"];
356+
if (Boolean.TryParse(isGrayscale, out var asIsGrayscale))
357+
{
358+
imageActions.IsGrayscale = asIsGrayscale;
359+
}
360+
355361
//add unofficial support for other interpretations of mode based on feedback
356362
//example: I like zoom, but others feel it should be call crop, so just fall back to zoom
357363
// as a general catchall for any other interpretations

CommonImageActions.Core/ImageActionQueryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public ImageActionQueryBuilder Text(string text)
6767
return this;
6868
}
6969

70+
public ImageActionQueryBuilder IsGrayscale(bool isGrayscale)
71+
{
72+
_actions.IsGrayscale = isGrayscale;
73+
return this;
74+
}
75+
7076
public ImageActionQueryBuilder AsInitials(bool asInitials)
7177
{
7278
_actions.AsInitials = asInitials;

CommonImageActions.Core/ImageActions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public ImageActions(ImageActions defaults)
5151

5252
public bool? ChooseImageColorFromTextValue { get; set; }
5353

54+
public bool? IsGrayscale { get; set; }
55+
5456
public SKEncodedImageFormat? Format { get; set; }
5557

5658
public ImageMode? Mode { get; set; }

CommonImageActions.Core/ImageProcessor.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions
237237

238238
var a = new SKPath();
239239
a.AddCircle(centerX, centerY, radius);
240-
canvas.ClipPath(a, antialias:true);
240+
canvas.ClipPath(a, antialias: true);
241241
}
242242
else if (imageActions.Shape == ImageShape.Ellipse)
243243
{
@@ -246,7 +246,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions
246246
var centerY = imageActions.Height.Value / 2;
247247
var r = GetSKRectByWidthAndHeight(0, 0, imageActions.Width.Value, imageActions.Height.Value);
248248
a.AddOval(r);
249-
canvas.ClipPath(a, antialias:true);
249+
canvas.ClipPath(a, antialias: true);
250250
}
251251
else if (imageActions.Shape == ImageShape.RoundedRectangle)
252252
{
@@ -261,7 +261,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions
261261
{
262262
a.AddRoundRect(r, CornerRadius, CornerRadius);
263263
}
264-
canvas.ClipPath(a, antialias:true);
264+
canvas.ClipPath(a, antialias: true);
265265
}
266266
}
267267

@@ -321,9 +321,20 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions
321321

322322
var imagePaint = new SKPaint
323323
{
324-
FilterQuality = SKFilterQuality.High
324+
FilterQuality = SKFilterQuality.High,
325325
};
326326

327+
if(imageActions.IsGrayscale.HasValue && imageActions.IsGrayscale == true)
328+
{
329+
imagePaint.ColorFilter = SKColorFilter.CreateColorMatrix(new float[]
330+
{
331+
0.3f, 0.59f, 0.11f, 0, 0,
332+
0.3f, 0.59f, 0.11f, 0, 0,
333+
0.3f, 0.59f, 0.11f, 0, 0,
334+
0, 0, 0, 1, 0
335+
});
336+
}
337+
327338
//write to the canvas
328339
switch (imageActions.Mode)
329340
{
@@ -335,7 +346,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions
335346
if (isOddRotation)
336347
{
337348
var drawRect = GetSKRectByWidthAndHeight(rotationOffsetX, rotationOffsetY, imageActions.Height.Value, imageActions.Width.Value);
338-
canvas.DrawImage(newImage, drawRect, paint:imagePaint);
349+
canvas.DrawImage(newImage, drawRect, paint: imagePaint);
339350
}
340351
else
341352
{
@@ -357,7 +368,7 @@ public static SKData EncodeSkiaImage(SKImage newImage, ImageActions imageActions
357368
var fitOffsetY = (imageActions.Height.Value - fitScaledHeight) / 2f;
358369
var drawRect2 = GetSKRectByWidthAndHeight(fitOffsetX, fitOffsetY, fitScaledWidth, fitScaledHeight);
359370

360-
canvas.DrawImage(newImage, drawRect2);
371+
canvas.DrawImage(newImage, drawRect2, paint: imagePaint);
361372
break;
362373

363374
//zoom in and fill canvas while maintaing aspect ratio

CommonImageActions.Pdf/PdfActionQueryBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ public PdfActionQueryBuilder Height(int height)
109109
return this;
110110
}
111111

112+
public PdfActionQueryBuilder IsGrayscale(bool isGrayscale)
113+
{
114+
_actions.IsGrayscale = isGrayscale;
115+
return this;
116+
}
117+
112118
public PdfActionQueryBuilder Page(int page)
113119
{
114120
_actions.Page = page;

CommonImageActions.SampleAspnetCoreProject/wwwroot/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ <h5 class="card-title">Page 2</h5>
210210
</div>
211211
</div>
212212

213+
<div class="mb-5">
214+
<h2>Grayscale</h2>
215+
<div class="row">
216+
217+
<div class="card m-2" style="width: 18rem;">
218+
<div class="card-body text-center">
219+
<p>
220+
<img src="/test/thumbsUp.jpg?w=100&g=true">
221+
</p>
222+
<h5 class="card-title">Grayscale</h5>
223+
<code>/test/thumbsUp.jpg?w=100&g=true</code>
224+
</div>
225+
</div>
226+
227+
</div>
228+
</div>
229+
213230
<div class="mb-5">
214231
<h2>JPEG EXIF</h2>
215232
<div class="row">

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ app.UseCommonImageActions(
155155
| ------------- | ------------- | ------------- |
156156
| width, w | Integer | Set the width of the image |
157157
| height, h | Integer | Set the height of the image |
158+
| gray, g | Boolean | When true set image to grayscale |
158159
| 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. |
159160
| shape, s | Circle, Ellipse, RoundedRectangle | Mask out the image to be of a certain shape. |
160161
| corner, cr | Integer | The corner radius when shape is RoundedRectangle. Default is 10. |

0 commit comments

Comments
 (0)