summaryrefslogtreecommitdiff
path: root/Services
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2023-06-05 17:11:36 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2023-06-05 17:11:36 +0300
commit480faae00b0d5ab545506e81d744be781059f8bb (patch)
tree7e6356a05a92e8d88a7c7a847091dd5834b8cc48 /Services
parent0f5cbb4d87388f6490915a53a2e83c9b93961c25 (diff)
Resize image on Upload
Diffstat (limited to 'Services')
-rw-r--r--Services/IImageResizer.cs2
-rw-r--r--Services/ImageResizer.cs29
2 files changed, 18 insertions, 13 deletions
diff --git a/Services/IImageResizer.cs b/Services/IImageResizer.cs
index de02893..be69e66 100644
--- a/Services/IImageResizer.cs
+++ b/Services/IImageResizer.cs
@@ -2,7 +2,7 @@ namespace MyDarling.Services
{
public interface IImageResizer
{
+ public void WriteResized(IFormFile formFile, string outputFilePath);
public void CreateThumbnail(string filePath);
- public void DownsizeImage(string filePath);
}
} \ No newline at end of file
diff --git a/Services/ImageResizer.cs b/Services/ImageResizer.cs
index 53c1eec..8e09424 100644
--- a/Services/ImageResizer.cs
+++ b/Services/ImageResizer.cs
@@ -2,15 +2,27 @@ using SkiaSharp;
namespace MyDarling.Services
{
- public class ImageResizer : IImageResizer
+ public class ImageResizer : IImageResizer
{
- const int size = 400;
- const int quality = 75;
public void CreateThumbnail(string inputPath)
{
using var input = File.OpenRead(inputPath);
using var inputStream = new SKManagedStream(input);
- using var original = SKBitmap.Decode(inputStream);
+ var outputPath = Path.GetDirectoryName(inputPath) + "\\" +
+ Path.GetFileNameWithoutExtension(inputPath) + "_thumb.jpg";
+ WriteResized(inputStream, 600, outputPath);
+ }
+ public void WriteResized(IFormFile formFile, string outputFilePath)
+ {
+ SKManagedStream stream = new(formFile.OpenReadStream());
+ WriteResized(stream, 2000, outputFilePath);
+ }
+
+ private void WriteResized(SKManagedStream stream, int size, string outputFilePath)
+ {
+ int quality = 95;
+ var skData = SKData.Create(stream);
+ using var original = SKBitmap.Decode(skData);
int width, height;
if (original.Width > original.Height)
@@ -28,16 +40,9 @@ namespace MyDarling.Services
if (resized == null) return;
using var image = SKImage.FromBitmap(resized);
- var outputPath = Path.GetDirectoryName(inputPath) + "\\" +
- Path.GetFileNameWithoutExtension(inputPath) + "_thumb.jpg";
- using var output = File.OpenWrite(outputPath);
+ using var output = File.OpenWrite(outputFilePath);
image.Encode(SKEncodedImageFormat.Jpeg, quality).SaveTo(output);
}
-
- public void DownsizeImage(string filePath)
- {
- throw new NotImplementedException();
- }
}
} \ No newline at end of file