Initial commit
This commit is contained in:
7
Server/Core/Common/Globals.cs
Normal file
7
Server/Core/Common/Globals.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Bring2mind.InMemoriam.Core.Common
|
||||
{
|
||||
public static class Globals
|
||||
{
|
||||
public const string uploadPath = @"InMemoriam\Pictures";
|
||||
}
|
||||
}
|
||||
43
Server/Core/Common/Image.cs
Normal file
43
Server/Core/Common/Image.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Bring2mind.InMemoriam.Core.Common
|
||||
{
|
||||
public class Image
|
||||
{
|
||||
public string ResizedFile { get; set; }
|
||||
public bool ImageExists { get; set; }
|
||||
private string Location { get; set; }
|
||||
|
||||
public Image(string location, string id, int width, int height, string method)
|
||||
{
|
||||
Location = location;
|
||||
var filePath = Path.Combine(location, id + ".resources");
|
||||
ImageExists = File.Exists(filePath);
|
||||
if (ImageExists)
|
||||
{
|
||||
ResizedFile = $"{id}_{width}_{height}_{method}.jpg";
|
||||
var m = ImageResizer.ResizeMethod.Stretch;
|
||||
switch (method)
|
||||
{
|
||||
case "b":
|
||||
m = ImageResizer.ResizeMethod.Box;
|
||||
break;
|
||||
case "c":
|
||||
m = ImageResizer.ResizeMethod.Crop;
|
||||
break;
|
||||
}
|
||||
ImageResizer.ResizeImage(filePath, $"{location}\\{ResizedFile}", width, height, m);
|
||||
}
|
||||
}
|
||||
|
||||
public void CopyToStream(Stream s)
|
||||
{
|
||||
var imgFile = ImageExists ? string.Format("{0}\\{1}", Location, ResizedFile) : string.Format("{0}\\images\\no-content.png", DotNetNuke.Common.Globals.ApplicationMapPath);
|
||||
using (var fs = new FileStream(imgFile, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
fs.CopyTo(s);
|
||||
fs.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Server/Core/Common/ImageOrientation.cs
Normal file
15
Server/Core/Common/ImageOrientation.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Bring2mind.InMemoriam.Core.Common
|
||||
{
|
||||
public enum ImageOrientation
|
||||
{
|
||||
Unknown = 0,
|
||||
Normal = 1,
|
||||
FlipHorizontal = 2,
|
||||
Rotate180 = 3,
|
||||
FlipVertical = 4,
|
||||
Transpose = 5,
|
||||
Rotate270 = 6,
|
||||
Transverse = 7,
|
||||
Rotate90 = 8
|
||||
}
|
||||
}
|
||||
134
Server/Core/Common/ImageResizer.cs
Normal file
134
Server/Core/Common/ImageResizer.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
|
||||
namespace Bring2mind.InMemoriam.Core.Common
|
||||
{
|
||||
public class ImageResizer
|
||||
{
|
||||
public enum ResizeMethod
|
||||
{
|
||||
Box,
|
||||
Crop,
|
||||
Stretch
|
||||
}
|
||||
|
||||
public static void ResizeImage(string source, string destination, int desiredWidth, int desiredHeight, ResizeMethod method)
|
||||
{
|
||||
var format = ImageFormat.Jpeg;
|
||||
var target = Resize(source, desiredWidth, desiredHeight, method);
|
||||
target.Save(destination, format);
|
||||
}
|
||||
|
||||
private static System.Drawing.Image Resize(string sourceImageMapPath, int desiredWidth, int desiredHeight, ResizeMethod method)
|
||||
{
|
||||
//throw error if bouning box is to small
|
||||
if (desiredWidth < 4 || desiredHeight < 4)
|
||||
throw new InvalidOperationException("Bounding Box of Resize Photo must be larger than 4X4 pixels.");
|
||||
var original = Bitmap.FromFile(sourceImageMapPath);
|
||||
|
||||
try
|
||||
{
|
||||
var orientation = (ImageOrientation)original.GetPropertyItem(274).Value[0];
|
||||
switch (orientation)
|
||||
{
|
||||
case ImageOrientation.FlipHorizontal:
|
||||
original.RotateFlip(RotateFlipType.RotateNoneFlipX);
|
||||
break;
|
||||
case ImageOrientation.Rotate180:
|
||||
original.RotateFlip(RotateFlipType.Rotate180FlipNone);
|
||||
break;
|
||||
case ImageOrientation.FlipVertical:
|
||||
original.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
||||
break;
|
||||
case
|
||||
ImageOrientation.Transpose:
|
||||
original.RotateFlip(RotateFlipType.RotateNoneFlipXY);
|
||||
break;
|
||||
case ImageOrientation.Rotate270:
|
||||
original.RotateFlip(RotateFlipType.Rotate90FlipNone);
|
||||
break;
|
||||
case
|
||||
ImageOrientation.Transverse:
|
||||
original.RotateFlip(RotateFlipType.Rotate90FlipXY);
|
||||
break;
|
||||
case ImageOrientation.Rotate90:
|
||||
original.RotateFlip(RotateFlipType.Rotate270FlipNone);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
//store image widths in variable for easier use
|
||||
var oW = (decimal)original.Width;
|
||||
var oH = (decimal)original.Height;
|
||||
var dW = (decimal)desiredWidth;
|
||||
var dH = (decimal)desiredHeight;
|
||||
|
||||
var scaleWidth = dW / oW;
|
||||
var scaleHeight = dH / oH;
|
||||
|
||||
int tW = desiredWidth;
|
||||
int tH = desiredHeight;
|
||||
int tX = 0;
|
||||
int tY = 0;
|
||||
int tDX = desiredWidth;
|
||||
int tDY = desiredHeight;
|
||||
|
||||
//check if image already fits
|
||||
if (oW <= dW && oH <= dH)
|
||||
return original; //image fits in bounding box, keep size (center with css) If we made it bigger it would stretch the image resulting in loss of quality.
|
||||
|
||||
//check for double squares or plain stretch resizing
|
||||
if ((oW == oH && dW == dH) || method == ResizeMethod.Stretch)
|
||||
{
|
||||
// don't do anything
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (method)
|
||||
{
|
||||
case ResizeMethod.Crop:
|
||||
var scaleMax = Math.Max(scaleHeight, scaleWidth);
|
||||
if (scaleHeight > scaleWidth)
|
||||
{
|
||||
tX = -1 * Convert.ToInt32(((scaleMax * oW) - dW) / 2);
|
||||
tDX = Convert.ToInt32(scaleMax * oW);
|
||||
}
|
||||
else
|
||||
{
|
||||
tY = -1 * Convert.ToInt32(((scaleMax * oH) - dH) / 2);
|
||||
tDY = Convert.ToInt32(scaleMax * oH);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
var scaleMin = Math.Min(scaleHeight, scaleWidth);
|
||||
tW = Convert.ToInt32(oW * scaleMin);
|
||||
tH = Convert.ToInt32(oH * scaleMin);
|
||||
tDX = tW;
|
||||
tDY = tH;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var destImage = new Bitmap(tW, tH);
|
||||
using (var graphics = Graphics.FromImage(destImage))
|
||||
{
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.DrawImage(original, tX, tY, tDX, tDY);
|
||||
}
|
||||
|
||||
return destImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Server/Core/Common/Visibility.cs
Normal file
9
Server/Core/Common/Visibility.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Bring2mind.InMemoriam.Core.Common
|
||||
{
|
||||
public enum Visibility
|
||||
{
|
||||
Public = 0,
|
||||
Friends = 1,
|
||||
Private = 2
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user