Fixed Cover Gen on PDFs (#3028)

This commit is contained in:
Joe Milazzo 2024-06-30 11:31:34 -05:00 committed by GitHub
parent 7997cd6329
commit 99c2dfb467
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 760 additions and 11 deletions

View file

@ -237,18 +237,40 @@ public class ImageService : IImageService
using var sourceImage = Image.NewFromStream(stream);
if (stream.CanSeek) stream.Position = 0;
var scalingSize = GetSizeForDimensions(sourceImage, targetWidth, targetHeight);
var scalingCrop = GetCropForDimensions(sourceImage, targetWidth, targetHeight);
using var thumbnail = sourceImage.ThumbnailImage(targetWidth, targetHeight,
size: GetSizeForDimensions(sourceImage, targetWidth, targetHeight),
crop: GetCropForDimensions(sourceImage, targetWidth, targetHeight));
size: scalingSize,
crop: scalingCrop);
var filename = fileName + encodeFormat.GetExtension();
_directoryService.ExistOrCreate(outputDirectory);
try
{
_directoryService.FileSystem.File.Delete(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
} catch (Exception) {/* Swallow exception */}
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
return filename;
try
{
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
return filename;
}
catch (VipsException)
{
// NetVips Issue: https://github.com/kleisauke/net-vips/issues/234
// Saving pdf covers from a stream can fail, so revert to old code
if (stream.CanSeek) stream.Position = 0;
using var thumbnail2 = Image.ThumbnailStream(stream, targetWidth, height: targetHeight,
size: scalingSize,
crop: scalingCrop);
thumbnail2.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
return filename;
}
}
public string WriteCoverThumbnail(string sourceFile, string fileName, string outputDirectory, EncodeFormat encodeFormat, CoverImageSize size = CoverImageSize.Default)