Add resize image + small fixs
This commit is contained in:
parent
3e6439433e
commit
85ca1368a2
@ -51,7 +51,7 @@ namespace ManagerService.Service.Controllers
|
||||
{
|
||||
#if DEBUG
|
||||
email = "test@email.be";
|
||||
password = "W/7aj4NB60i3YFKJq50pbw=="; // password = "kljqsdkljqsd";
|
||||
password = "kljqsdkljqsd"; // password = "kljqsdkljqsd"; // W/7aj4NB60i3YFKJq50pbw==
|
||||
#endif
|
||||
// Set user token ?
|
||||
var user = _UserDatabaseService.GetByEmail(email.ToLower());
|
||||
|
||||
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Manager.Helpers;
|
||||
using Manager.Interfaces.DTO;
|
||||
using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
|
||||
@ -11,6 +11,7 @@ using System.Threading.Tasks;
|
||||
using Manager.Interfaces.DTO;
|
||||
using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
using ManagerService.Helpers;
|
||||
using ManagerService.Service.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -31,6 +32,9 @@ namespace ManagerService.Controllers
|
||||
private ConfigurationDatabaseService _configurationService;
|
||||
private readonly ILogger<ResourceController> _logger;
|
||||
|
||||
private static int MaxWidth = 1024;
|
||||
private static int MaxHeight = 1024;
|
||||
|
||||
public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService, ResourceDataDatabaseService resourceDataService, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService)
|
||||
{
|
||||
_logger = logger;
|
||||
@ -77,7 +81,7 @@ namespace ManagerService.Controllers
|
||||
resourceDTOs.Add(resourceDTO);
|
||||
}
|
||||
|
||||
return new OkObjectResult(resourceDTOs);
|
||||
return new OkObjectResult(resourceDTOs.OrderByDescending(r => r.dateCreation));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -105,11 +109,35 @@ namespace ManagerService.Controllers
|
||||
|
||||
ResourceDTO resourceDTO = new ResourceDTO();
|
||||
resourceDTO = resource.ToDTO();
|
||||
if (resource.Type == ResourceType.ImageUrl)
|
||||
ResourceData resourceData = _resourceDataService.GetByResourceId(id);
|
||||
resourceDTO.data = resourceData.Data;
|
||||
/*if (resource.Type == ResourceType.ImageUrl)
|
||||
{
|
||||
var resourceData = _resourceDataService.GetByResourceId(resource.Id);
|
||||
resourceDTO.data = resourceData != null ? resourceData.Data : null;
|
||||
}*/
|
||||
|
||||
// RESIZE IMAGE
|
||||
|
||||
/*byte[] imageBytes = Convert.FromBase64String(resourceData.Data);
|
||||
|
||||
using (MemoryStream originalImageMemoryStream = new MemoryStream(imageBytes))
|
||||
{
|
||||
using (Image image = Image.FromStream(originalImageMemoryStream))
|
||||
{
|
||||
var width = image.Width;
|
||||
var height = image.Height;
|
||||
|
||||
if (image.Width > MaxWidth || image.Height > MaxHeight)
|
||||
{
|
||||
Size newSize = ImageResizer.ResizeKeepAspect(image.Size, MaxWidth, MaxHeight);
|
||||
byte[] resizedImage = ImageResizer.ResizeImage(image, newSize.Width, newSize.Height, image.Width, image.Height);
|
||||
|
||||
resourceData.Data = Convert.ToBase64String(resizedImage);
|
||||
ResourceData resourceModified = _resourceDataService.Update(resourceData.Id, resourceData);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return new OkObjectResult(resourceDTO);
|
||||
}
|
||||
@ -144,6 +172,26 @@ namespace ManagerService.Controllers
|
||||
|
||||
var file = Convert.FromBase64String(resourceData.Data);
|
||||
|
||||
// RESIZE IMAGE
|
||||
|
||||
/*using (MemoryStream originalImageMemoryStream = new MemoryStream(file))
|
||||
{
|
||||
using (Image image = Image.FromStream(originalImageMemoryStream))
|
||||
{
|
||||
var width = image.Width;
|
||||
var height = image.Height;
|
||||
|
||||
if(image.Width > MaxWidth || image.Height > MaxHeight)
|
||||
{
|
||||
Size newSize = ImageResizer.ResizeKeepAspect(image.Size, MaxWidth, MaxHeight);
|
||||
byte[] resizedImage = ImageResizer.ResizeImage(image, newSize.Width, newSize.Height, image.Width, image.Height);
|
||||
|
||||
resourceData.Data = Convert.ToBase64String(resizedImage);
|
||||
ResourceData resourceModified = _resourceDataService.Update(resourceData.Id, resourceData);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
if (resource.Type == ResourceType.Image)
|
||||
{
|
||||
return new FileContentResult(file, "image/png");
|
||||
@ -188,20 +236,20 @@ namespace ManagerService.Controllers
|
||||
{
|
||||
var stringResult = "";
|
||||
double fileSizeibMbs = (double) ((double)file.Length) / (1024*1024);
|
||||
if (fileSizeibMbs <= 2.01)
|
||||
if (fileSizeibMbs <= 4.01)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
file.CopyTo(ms);
|
||||
var fileBytes = ms.ToArray();
|
||||
if (resourceType == ResourceType.Image) {
|
||||
fileBytes = AddWatermark(fileBytes);
|
||||
fileBytes = ImageHelper.ResizeAndAddWatermark(fileBytes, MaxWidth, MaxHeight);
|
||||
}
|
||||
stringResult = Convert.ToBase64String(fileBytes);
|
||||
}
|
||||
} else
|
||||
{
|
||||
throw new FileLoadException(message: "Fichier inexistant ou trop volumineux (max 2Mb)");
|
||||
throw new FileLoadException(message: "Fichier inexistant ou trop volumineux (max 4Mb)");
|
||||
}
|
||||
// Todo add some verification ?
|
||||
Resource resource = new Resource();
|
||||
@ -239,37 +287,6 @@ namespace ManagerService.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] AddWatermark(Byte[] bytes)
|
||||
{
|
||||
byte[] convertedToBytes;
|
||||
|
||||
using (MemoryStream originalImageMemoryStream = new MemoryStream(bytes))
|
||||
{
|
||||
using (Image image = Image.FromStream(originalImageMemoryStream))
|
||||
{
|
||||
Font font = new Font("Arial", 25, FontStyle.Italic, GraphicsUnit.Pixel);
|
||||
Color color = Color.DarkBlue;
|
||||
Point point = new Point(image.Width /2, (int)Math.Round(image.Height - image.Height * 0.1));
|
||||
SolidBrush brush = new SolidBrush(color);
|
||||
using (Graphics graphics = Graphics.FromImage(image))
|
||||
{
|
||||
StringFormat stringFormat = new StringFormat();
|
||||
stringFormat.Alignment = StringAlignment.Center;
|
||||
stringFormat.LineAlignment = StringAlignment.Center;
|
||||
graphics.DrawString("fortsaintheribert.be", font, brush, point, stringFormat);
|
||||
}
|
||||
|
||||
using (MemoryStream updatedImageMemorySteam = new MemoryStream())
|
||||
{
|
||||
image.Save(updatedImageMemorySteam, System.Drawing.Imaging.ImageFormat.Jpeg);
|
||||
convertedToBytes = updatedImageMemorySteam.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return convertedToBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new resource
|
||||
/// </summary>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using Manager.Interfaces.DTO;
|
||||
using Manager.Helpers;
|
||||
using Manager.Interfaces.DTO;
|
||||
using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
122
ManagerService/Helpers/ImageHelper.cs
Normal file
122
ManagerService/Helpers/ImageHelper.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
|
||||
namespace ManagerService.Helpers
|
||||
{
|
||||
public static class ImageHelper
|
||||
{
|
||||
|
||||
public static Size ResizeKeepAspect(this Size src, int maxWidth, int maxHeight, bool enlarge = false)
|
||||
{
|
||||
maxWidth = enlarge ? maxWidth : Math.Min(maxWidth, src.Width);
|
||||
maxHeight = enlarge ? maxHeight : Math.Min(maxHeight, src.Height);
|
||||
|
||||
decimal rnd = Math.Min(maxWidth / (decimal)src.Width, maxHeight / (decimal)src.Height);
|
||||
return new Size((int)Math.Round(src.Width * rnd), (int)Math.Round(src.Height * rnd));
|
||||
}
|
||||
|
||||
// TO Byte[] or Image type
|
||||
public static dynamic ResizeImage(Image image,
|
||||
/* note changed names */
|
||||
int canvasWidth, int canvasHeight,
|
||||
/* new */
|
||||
int originalWidth, int originalHeight, bool isImage = false)
|
||||
{
|
||||
byte[] convertedToBytes;
|
||||
|
||||
System.Drawing.Image thumbnail =
|
||||
new Bitmap(canvasWidth, canvasHeight); // changed parm names
|
||||
System.Drawing.Graphics graphic =
|
||||
System.Drawing.Graphics.FromImage(thumbnail);
|
||||
|
||||
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphic.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphic.CompositingQuality = CompositingQuality.HighQuality;
|
||||
|
||||
/* ------------------ new code --------------- */
|
||||
|
||||
// Figure out the ratio
|
||||
double ratioX = (double)canvasWidth / (double)originalWidth;
|
||||
double ratioY = (double)canvasHeight / (double)originalHeight;
|
||||
// use whichever multiplier is smaller
|
||||
double ratio = ratioX < ratioY ? ratioX : ratioY;
|
||||
|
||||
// now we can get the new height and width
|
||||
int newHeight = Convert.ToInt32(originalHeight * ratio);
|
||||
int newWidth = Convert.ToInt32(originalWidth * ratio);
|
||||
|
||||
// Now calculate the X,Y position of the upper-left corner
|
||||
// (one of these will always be zero)
|
||||
int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
|
||||
int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);
|
||||
|
||||
graphic.Clear(Color.White); // white padding
|
||||
graphic.DrawImage(image, posX, posY, newWidth, newHeight);
|
||||
|
||||
/* ------------- end new code ---------------- */
|
||||
|
||||
System.Drawing.Imaging.ImageCodecInfo[] info =
|
||||
ImageCodecInfo.GetImageEncoders();
|
||||
EncoderParameters encoderParameters;
|
||||
encoderParameters = new EncoderParameters(1);
|
||||
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
|
||||
100L);
|
||||
|
||||
if (isImage)
|
||||
{
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
using (MemoryStream updatedImageMemorySteam = new MemoryStream())
|
||||
{
|
||||
thumbnail.Save(updatedImageMemorySteam, System.Drawing.Imaging.ImageFormat.Jpeg);
|
||||
convertedToBytes = updatedImageMemorySteam.ToArray();
|
||||
}
|
||||
return convertedToBytes;
|
||||
}
|
||||
|
||||
public static byte[] ResizeAndAddWatermark(Byte[] bytes, int maxWidth, int maxHeight)
|
||||
{
|
||||
byte[] convertedToBytes;
|
||||
|
||||
using (MemoryStream originalImageMemoryStream = new MemoryStream(bytes))
|
||||
{
|
||||
using (Image image = Image.FromStream(originalImageMemoryStream))
|
||||
{
|
||||
Image currentImage = image;
|
||||
|
||||
if (image.Width > maxWidth || image.Height > maxHeight)
|
||||
{
|
||||
Size newSize = ResizeKeepAspect(image.Size, maxWidth, maxHeight);
|
||||
Image resizedImage = ImageHelper.ResizeImage(image, newSize.Width, newSize.Height, image.Width, image.Height, true);
|
||||
currentImage = resizedImage;
|
||||
}
|
||||
|
||||
Font font = new Font("Arial", 25, FontStyle.Italic, GraphicsUnit.Pixel);
|
||||
Color color = Color.DarkBlue;
|
||||
Point point = new Point(currentImage.Width /2, (int)Math.Round(currentImage.Height - currentImage.Height * 0.1));
|
||||
SolidBrush brush = new SolidBrush(color);
|
||||
using (Graphics graphics = Graphics.FromImage(currentImage))
|
||||
{
|
||||
StringFormat stringFormat = new StringFormat();
|
||||
stringFormat.Alignment = StringAlignment.Center;
|
||||
stringFormat.LineAlignment = StringAlignment.Center;
|
||||
graphics.DrawString("fortsaintheribert.be", font, brush, point, stringFormat);
|
||||
}
|
||||
|
||||
using (MemoryStream updatedImageMemorySteam = new MemoryStream())
|
||||
{
|
||||
currentImage.Save(updatedImageMemorySteam, System.Drawing.Imaging.ImageFormat.Jpeg);
|
||||
convertedToBytes = updatedImageMemorySteam.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return convertedToBytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@ using Manager.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Manager.Services
|
||||
namespace Manager.Helpers
|
||||
{
|
||||
public class LanguageInit
|
||||
{
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using Manager.Framework.Business;
|
||||
using Manager.Framework.Models;
|
||||
using Manager.Helpers;
|
||||
using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
using ManagerService.Extensions;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user