diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-06-01 21:13:43 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-06-01 21:13:43 +0200 |
| commit | 9383a2fb09ffb60cfe63683106945bd688affa59 (patch) | |
| tree | 65b3f4b48841583e355887db5de5a16e7005fc87 /src/Utilities/ImageFunctions.cs | |
| download | vinjesvingenhandel.no-9383a2fb09ffb60cfe63683106945bd688affa59.tar.xz vinjesvingenhandel.no-9383a2fb09ffb60cfe63683106945bd688affa59.zip | |
feat: Initial commit after clean slate
Diffstat (limited to 'src/Utilities/ImageFunctions.cs')
| -rw-r--r-- | src/Utilities/ImageFunctions.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/Utilities/ImageFunctions.cs b/src/Utilities/ImageFunctions.cs new file mode 100644 index 0000000..dd2e07c --- /dev/null +++ b/src/Utilities/ImageFunctions.cs @@ -0,0 +1,61 @@ +using System; +using System.Diagnostics; +using System.IO; +using ImageMagick; +using IOL.Helpers; +using VSH.Data.Static; + +namespace VSH.Utilities; + +public static class ImageFunctions +{ + private static MagickGeometry SmallGeometry => new(300); + private static MagickGeometry MiniGeometry => new(150); + private static MagickGeometry NormalGeometry => new(1280, 720); + + public static void CreateProductImageCollection(FileInfo path) { + try { + using var image = new MagickImage(path); + if (image.Width > NormalGeometry.Width) { + image.Resize(NormalGeometry); + image.Write(path); + } + + Debug.WriteLine(path.Name); + + if (image.Width > SmallGeometry.Width) { + var fileName = Path.Combine(path.DirectoryName ?? AppPaths.ProductImages.HostPath, + path.Name.ExtractFileName() + "-300" + path.Extension); + if (!File.Exists(fileName)) { + image.Resize(SmallGeometry); + image.Write(fileName); + } + } + + if (image.Width > MiniGeometry.Width) { + var fileName = Path.Combine(path.DirectoryName ?? AppPaths.ProductImages.HostPath, + path.Name.ExtractFileName() + "-150" + path.Extension); + if (!File.Exists(fileName)) { + image.Resize(MiniGeometry); + image.Write(fileName); + } + } + } catch (Exception e) { + Console.WriteLine(e); + throw; + } + } + + public static void EnsureNormalOrLessImageResolution(FileInfo path) { + try { + using var image = new MagickImage(path); + if (image.Width <= NormalGeometry.Width) + return; + image.Resize(NormalGeometry); + image.Write(path); + } catch (Exception e) { + Console.WriteLine(e); + throw; + } + } +}
\ No newline at end of file |
