add transparency support on image resizing

This commit is contained in:
sbwalker 2023-08-04 16:07:37 -04:00
parent 92a4a1b210
commit f7bd03d051
4 changed files with 20 additions and 7 deletions

View File

@ -210,7 +210,7 @@
private string GetLogo(int fileid) private string GetLogo(int fileid)
{ {
var url = ImageUrl(fileid, 100, 100, "", "", "ffffff", 0, false); var url = ImageUrl(fileid, 100, 100);
url = (!string.IsNullOrEmpty(PageState.Alias.Path)) ? url.Substring(PageState.Alias.Path.Length + 1) : url; url = (!string.IsNullOrEmpty(PageState.Alias.Path)) ? url.Substring(PageState.Alias.Path.Length + 1) : url;
return Constants.PackageRegistryUrl + url; return Constants.PackageRegistryUrl + url;
} }

View File

@ -212,7 +212,7 @@
private string GetLogo(int fileid) private string GetLogo(int fileid)
{ {
var url = ImageUrl(fileid, 100, 100, "", "", "ffffff", 0, false); var url = ImageUrl(fileid, 100, 100);
url = (!string.IsNullOrEmpty(PageState.Alias.Path)) ? url.Substring(PageState.Alias.Path.Length + 1) : url; url = (!string.IsNullOrEmpty(PageState.Alias.Path)) ? url.Substring(PageState.Alias.Path.Length + 1) : url;
return Constants.PackageRegistryUrl + url; return Constants.PackageRegistryUrl + url;
} }

View File

@ -572,7 +572,7 @@ namespace Oqtane.Controllers
// validation // validation
if (!Enum.TryParse(mode, true, out ResizeMode _)) mode = "crop"; if (!Enum.TryParse(mode, true, out ResizeMode _)) mode = "crop";
if (!Enum.TryParse(position, true, out AnchorPositionMode _)) position = "center"; if (!Enum.TryParse(position, true, out AnchorPositionMode _)) position = "center";
if (!Color.TryParseHex("#" + background, out _)) background = "000000"; if (!Color.TryParseHex("#" + background, out _)) background = "transparent";
if (!int.TryParse(rotate, out _)) rotate = "0"; if (!int.TryParse(rotate, out _)) rotate = "0";
rotate = (int.Parse(rotate) < 0 || int.Parse(rotate) > 360) ? "0" : rotate; rotate = (int.Parse(rotate) < 0 || int.Parse(rotate) > 360) ? "0" : rotate;
if (!bool.TryParse(recreate, out _)) recreate = "false"; if (!bool.TryParse(recreate, out _)) recreate = "false";
@ -644,10 +644,23 @@ namespace Oqtane.Controllers
Mode = resizemode, Mode = resizemode,
Position = anchorpositionmode, Position = anchorpositionmode,
Size = new Size(width, height) Size = new Size(width, height)
}) }));
.BackgroundColor(Color.ParseHex("#" + background)));
image.Save(imagepath, new PngEncoder()); if (background != "transparent")
{
image.Mutate(x => x
.BackgroundColor(Color.ParseHex("#" + background)));
}
PngEncoder encoder = new PngEncoder
{
ColorType = PngColorType.RgbWithAlpha,
TransparentColorMode = PngTransparentColorMode.Preserve,
BitDepth = PngBitDepth.Bit8,
CompressionLevel = PngCompressionLevel.BestSpeed
};
image.Save(imagepath, encoder);
} }
} }
} }

View File

@ -133,7 +133,7 @@ namespace Oqtane.Shared
var url = (alias != null && !string.IsNullOrEmpty(alias.Path)) ? "/" + alias.Path : ""; var url = (alias != null && !string.IsNullOrEmpty(alias.Path)) ? "/" + alias.Path : "";
mode = string.IsNullOrEmpty(mode) ? "crop" : mode; mode = string.IsNullOrEmpty(mode) ? "crop" : mode;
position = string.IsNullOrEmpty(position) ? "center" : position; position = string.IsNullOrEmpty(position) ? "center" : position;
background = string.IsNullOrEmpty(background) ? "000000" : background; background = string.IsNullOrEmpty(background) ? "transparent" : background;
return $"{alias?.BaseUrl}{url}{Constants.ImageUrl}{fileId}/{width}/{height}/{mode}/{position}/{background}/{rotate}/{recreate}"; return $"{alias?.BaseUrl}{url}{Constants.ImageUrl}{fileId}/{width}/{height}/{mode}/{position}/{background}/{rotate}/{recreate}";
} }