Saxonite v1.2.6.0

This commit is contained in:
2022-05-16 15:02:05 +01:00
parent 5229032a87
commit 7965902b14
4 changed files with 91 additions and 8 deletions
+1
View File
@@ -145,6 +145,7 @@ namespace Sarand.Net.Standard.Base.Core
// Images.
Png,
Jpeg,
Webp,
Bmp,
Gif,
Tif,
@@ -139,7 +139,7 @@ namespace Sarand.Net.Standard.Base.Helpers
new ContentTypeData_Internal("wav", "Waveform Audio Format", "audio/wav"),
new ContentTypeData_Internal("webm", "WEBM video", "video/webm"),
new ContentTypeData_Internal("weba", "WEBM audio", "audio/webm"),
new ContentTypeData_Internal("webp", "WEBP image", "image/webp"),
new ContentTypeData_Internal("webp", "WEBP image", "image/webp", Sacs.MimeType.Webp),
new ContentTypeData_Internal("woff", "Web Open Font Format (WOFF)", "application/font-woff"),
new ContentTypeData_Internal("woff", "Web Open Font Format (WOFF)", "font/woff") { PreferredContentType = "application/font-woff" },
new ContentTypeData_Internal("woff2", "Web Open Font Format (WOFF)", "font/woff2"),
+88 -6
View File
@@ -1,8 +1,14 @@
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Helpers;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Sarand.Net.Standard.Base.Utils
@@ -19,10 +25,20 @@ namespace Sarand.Net.Standard.Base.Utils
public class ImageConfig
{
public byte[] ImageData { get; set; }
public string ImageFormatName;
public string DefaultMimeType;
public Sacs.MimeType MimeType;
public bool ValidImage { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
//-------------------------------------------------------------------
/// <summary>
/// Convert the data stream to an image and extract width and height.
/// </summary>
/// <param name="imageData"></param>
/// <returns>Flag to indicate whether interpretation of image data succeeded.</returns>
public static ImageConfig VerifyImageData(byte[] imageData)
{
String fn = className + ".VerifyImageData";
@@ -30,12 +46,17 @@ namespace Sarand.Net.Standard.Base.Utils
imageConfig.ImageData = imageData;
try
{
var image = Image.Load<Rgba32>(imageData);
if (image == null) return imageConfig;
imageConfig.Width = image.Width;
imageConfig.Height = image.Height;
IImageFormat imageFormat = null;
using (var image = LoadImage(imageData, out imageFormat))
{
if (image == null) return imageConfig;
imageConfig.ImageFormatName = imageFormat.Name;
imageConfig.DefaultMimeType = imageFormat.DefaultMimeType;
imageConfig.MimeType = EnumUtil.ParseEnum(imageConfig.ImageFormatName, Sacs.MimeType.Unknown);
imageConfig.Width = image.Width;
imageConfig.Height = image.Height;
}
imageConfig.ValidImage = true;
return imageConfig;
}
catch (Exception exc)
{
@@ -44,12 +65,73 @@ namespace Sarand.Net.Standard.Base.Utils
return imageConfig;
}
//-------------------------------------------------------------------
// Load from byte array.
public static Image LoadImage(byte[] imageData)
/// <summary>Convert the data stream to an image.</summary>
public static SixLabors.ImageSharp.Image LoadImage(byte[] imageData)
{
var image = Image.Load<Rgba32>(imageData);
return image;
}
/// <summary>
/// Convert the data stream to an image.
/// <para>Identify the image's format on load.</para>
/// </summary>
public static SixLabors.ImageSharp.Image LoadImage(byte[] imageData, out IImageFormat format)
{
var image = Image.Load<Rgba32>(imageData, out format);
return image;
}
//-------------------------------------------------------------------
// Convert to byte array.
/// <summary>Convert the data stream to an image.</summary>
public static Byte[] GetImageData(this SixLabors.ImageSharp.Image image, IImageFormat imageFormat)
{
using (MemoryStream ms = new MemoryStream())
{
var imageEncoder = image.GetConfiguration().ImageFormatsManager.FindEncoder(imageFormat);
image.Save(ms, imageEncoder);
return ms.ToArray();
}
}
//-------------------------------------------------------------------
/// <summary>
/// Resize the image with the specified limits, if required.
/// </summary>
/// <param name="config">Image data</param>
/// <param name="widthMax"></param>
/// <param name="heightMax"></param>
/// <returns>Flag to indicate whether operation was successful.</returns>
public static bool ConstrainImage(ImageConfig config, ushort widthMax, ushort heightMax)
{
String fn = className + ".ConstrainImage";
if (widthMax < 1 || heightMax < 1) return false;
if (config.Width <= widthMax && config.Height <= heightMax) return true;
config.ValidImage = false;
try
{
// Resize the image.
IImageFormat imageFormat = null;
using (var image = LoadImage(config.ImageData, out imageFormat))
{
double resizeProportion = Math.Min(widthMax / (double)image.Width, heightMax / (double)image.Height);
image.Mutate(img => img.Resize((int)(image.Width * resizeProportion), (int)(image.Height * resizeProportion)));
config.ImageData = image.GetImageData(imageFormat);
config.Width = image.Width;
config.Height = image.Height;
config.ValidImage = true;
}
}
catch (Exception exc)
{
SaLogger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc));
}
return config.ValidImage;
}
//-------------------------------------------------------------------
}
+1 -1
View File
@@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.1" />
</ItemGroup>
<ItemGroup>