Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Thumbnail wasn't always generated on Linux based system. #1853

Merged
merged 1 commit into from Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 11 additions & 25 deletions backend/Origam.Server/Blob Control/BlobUploadHandler.cs
Expand Up @@ -56,6 +56,7 @@
using System.Text;
using Origam.Server;
using Origam.Server.Pages;
using ImageMagick;

namespace Origam.Server
{
Expand Down Expand Up @@ -232,15 +233,15 @@ public class BlobUploadHandler
// }
// }

public static byte[] FixedSizeBytes(Image img, int width, int height)
public static byte[] FixedSizeBytes(MagickImage img, int width, int height)
{
using (Image thumbnail = FixedSize(img, width, height))
using (MagickImage thumbnail = FixedSize(img, width, height))
{
MemoryStream ms = new MemoryStream();

try
{
thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
thumbnail.Write(ms);
return ms.GetBuffer();
}
finally
Expand Down Expand Up @@ -272,21 +273,17 @@ public static byte[] FixedSizeBytes(Image img, int width, int height)
// return true;
// }

public static Image FixedSize(Image imgPhoto, int Width, int Height)
public static MagickImage FixedSize(MagickImage imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
float nPercentW = ((float)Width / (float)sourceWidth);
float nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
Expand All @@ -303,23 +300,12 @@ public static Image FixedSize(Image imgPhoto, int Width, int Height)
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
bmPhoto.MakeTransparent(Color.Transparent);
var bmPhoto = new MagickImage(MagickColors.Black, Width, Height);
bmPhoto.Format = MagickFormat.Png;

System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Transparent);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
imgPhoto.Resize(destWidth, destHeight);

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
bmPhoto.Composite(imgPhoto, destX, destY);
return bmPhoto;
}
}
Expand Down
5 changes: 3 additions & 2 deletions backend/Origam.Server/Pages/UserApiProcessor.cs
Expand Up @@ -41,6 +41,7 @@
using System.Web;
using Origam.Extensions;
using Origam.Service.Core;
using ImageMagick;

namespace Origam.Server.Pages
{
Expand Down Expand Up @@ -633,11 +634,11 @@ private static byte[] GetFileBytes(PageParameterFileMapping fileMapping, PostedF
else
{
// get a thumbnail
Image img = null;
MagickImage img = null;

try
{
img = Image.FromStream(file.InputStream);
img = new MagickImage(file.InputStream);
}
catch
{
Expand Down