You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/src/NzbDrone.Core/MediaCover/ImageResizer.cs

59 lines
1.6 KiB

using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;
using SixLabors.Memory;
namespace NzbDrone.Core.MediaCover
{
public interface IImageResizer
{
void Resize(string source, string destination, int height);
}
public class ImageResizer : IImageResizer
{
private readonly IDiskProvider _diskProvider;
private readonly bool _enabled;
public ImageResizer(IDiskProvider diskProvider, IPlatformInfo platformInfo)
{
_diskProvider = diskProvider;
_enabled = true;
// More conservative memory allocation
SixLabors.ImageSharp.Configuration.Default.MemoryAllocator = new SimpleGcMemoryAllocator();
// Thumbnails don't need super high quality
SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder
{
Quality = 92
});
}
public void Resize(string source, string destination, int height)
{
if (!_enabled) return;
try
{
using (var image = Image.Load(source))
{
image.Mutate(x => x.Resize(0, height));
image.Save(destination);
}
}
catch
{
if (_diskProvider.FileExists(destination))
{
_diskProvider.DeleteFile(destination);
}
throw;
}
}
}
}