2019-08-25 19:46:19 +00:00
|
|
|
|
using System;
|
2015-01-22 22:12:35 +00:00
|
|
|
|
using NzbDrone.Common.Disk;
|
2019-09-14 01:47:18 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2019-08-25 19:46:19 +00:00
|
|
|
|
using SixLabors.ImageSharp;
|
2019-09-14 01:47:18 +00:00
|
|
|
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
2019-08-25 19:46:19 +00:00
|
|
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
|
using SixLabors.Memory;
|
2015-01-22 22:12:35 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaCover
|
|
|
|
|
{
|
|
|
|
|
public interface IImageResizer
|
|
|
|
|
{
|
|
|
|
|
void Resize(string source, string destination, int height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImageResizer : IImageResizer
|
|
|
|
|
{
|
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2019-09-14 01:47:18 +00:00
|
|
|
|
private readonly bool _enabled;
|
2015-01-22 22:12:35 +00:00
|
|
|
|
|
2019-09-14 01:47:18 +00:00
|
|
|
|
public ImageResizer(IDiskProvider diskProvider, IPlatformInfo platformInfo)
|
2015-01-22 22:12:35 +00:00
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
2019-08-25 19:46:19 +00:00
|
|
|
|
|
2019-09-14 01:47:18 +00:00
|
|
|
|
// Random segfaults on mono 5.0 and 5.4
|
|
|
|
|
if (PlatformInfo.IsMono && platformInfo.Version < new System.Version(5, 8))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_enabled = true;
|
|
|
|
|
|
2019-08-25 19:46:19 +00:00
|
|
|
|
// More conservative memory allocation
|
|
|
|
|
SixLabors.ImageSharp.Configuration.Default.MemoryAllocator = new SimpleGcMemoryAllocator();
|
2019-09-14 01:47:18 +00:00
|
|
|
|
|
|
|
|
|
// Thumbnails don't need super high quality
|
|
|
|
|
SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder
|
|
|
|
|
{
|
|
|
|
|
Quality = 92
|
|
|
|
|
});
|
2015-01-22 22:12:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Resize(string source, string destination, int height)
|
|
|
|
|
{
|
2019-09-14 01:47:18 +00:00
|
|
|
|
if (!_enabled) return;
|
|
|
|
|
|
2015-02-03 19:46:51 +00:00
|
|
|
|
try
|
2015-01-22 22:12:35 +00:00
|
|
|
|
{
|
2019-08-25 19:46:19 +00:00
|
|
|
|
using (var image = Image.Load(source))
|
2015-01-22 22:12:35 +00:00
|
|
|
|
{
|
2019-09-14 01:47:18 +00:00
|
|
|
|
image.Mutate(x => x.Resize(0, height));
|
2019-08-25 19:46:19 +00:00
|
|
|
|
image.Save(destination);
|
2015-01-22 22:12:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-03 19:46:51 +00:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (_diskProvider.FileExists(destination))
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFile(destination);
|
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2015-01-22 22:12:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|