New: Use ImageSharp for resizing

pull/2/head
ta264 5 years ago committed by Qstick
parent c85d3119f9
commit bc0cc2bfa9

@ -122,20 +122,6 @@ namespace NzbDrone.Common.Disk
}
}
public bool CanUseGDIPlus()
{
try
{
GdiPlusInterop.CheckGdiPlus();
return true;
}
catch (DllNotFoundException ex)
{
Logger.Trace(ex, "System does not have libgdiplus.");
return false;
}
}
public bool FolderWritable(string path)
{
Ensure.That(path, () => path).IsValidPath();

@ -1,44 +0,0 @@
using System;
using System.Drawing;
using NzbDrone.Common.EnvironmentInfo;
namespace NzbDrone.Common.Disk
{
public static class GdiPlusInterop
{
private static Exception _gdiPlusException;
static GdiPlusInterop()
{
TestLibrary();
}
private static void TestLibrary()
{
if (OsInfo.IsWindows)
{
return;
}
try
{
// We use StringFormat as test coz it gets properly cleaned up by the finalizer even if gdiplus is absent and is relatively non-invasive.
var strFormat = new StringFormat();
strFormat.Dispose();
}
catch (Exception ex)
{
_gdiPlusException = ex;
}
}
public static void CheckGdiPlus()
{
if (_gdiPlusException != null)
{
throw new DllNotFoundException("Couldn't load GDIPlus library", _gdiPlusException);
}
}
}
}

@ -19,7 +19,6 @@ namespace NzbDrone.Common.Disk
bool FolderExists(string path);
bool FileExists(string path);
bool FileExists(string path, StringComparison stringComparison);
bool CanUseGDIPlus();
bool FolderWritable(string path);
string[] GetDirectories(string path);
string[] GetFiles(string path, SearchOption searchOption);

@ -1,6 +1,8 @@
using ImageResizer;
using System;
using NzbDrone.Common.Disk;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.Memory;
namespace NzbDrone.Core.MediaCover
{
@ -16,28 +18,20 @@ namespace NzbDrone.Core.MediaCover
public ImageResizer(IDiskProvider diskProvider)
{
_diskProvider = diskProvider;
// More conservative memory allocation
SixLabors.ImageSharp.Configuration.Default.MemoryAllocator = new SimpleGcMemoryAllocator();
}
public void Resize(string source, string destination, int height)
{
try
{
if (!_diskProvider.CanUseGDIPlus())
{
throw new Exception("Can't resize without libgdiplus.");
}
using (var sourceStream = _diskProvider.OpenReadStream(source))
using (var image = Image.Load(source))
{
using (var outputStream = _diskProvider.OpenWriteStream(destination))
{
var settings = new Instructions();
settings.Height = height;
var job = new ImageJob(sourceStream, outputStream, settings);
ImageBuilder.Current.Build(job);
}
var width = (int)Math.Floor((double)image.Width * (double)height / (double)image.Height);
image.Mutate(x => x.Resize(width, height));
image.Save(destination);
}
}
catch

@ -7,7 +7,7 @@
<PackageReference Include="FluentMigrator.Runner" Version="4.0.0-alpha.268" />
<PackageReference Include="FluentMigrator.Runner.SQLite" Version="4.0.0-alpha.268" />
<PackageReference Include="FluentValidation" Version="8.4.0" />
<PackageReference Include="ImageResizer" Version="4.2.5" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="NLog" Version="4.6.7" />
<PackageReference Include="OAuth" Version="1.0.3" />

Loading…
Cancel
Save