Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Rotate and flip: a long image turns its pages, not the whole strip
SeoJCalive6c48256e7095
diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.csindex b055c430..0dff43bc 100644--- a/src/Filee.Engines/Magick/ImageEncoder.cs+++ b/src/Filee.Engines/Magick/ImageEncoder.cs@@ -160,13 +160,9 @@ internal static class ImageEncoder_ => 1,};- /// <summary>Applies resize / colour / metadata options to one frame.</summary>- public static void ApplyOptions(IMagickImage<byte> image, ImageOptions options, string targetFormat)+ /// <summary>Turns and mirrors an image as <paramref name="options"/> say (part of <see cref="ApplyOptions"/>).</summary>+ public static void Turn(IMagickImage<byte> image, ImageOptions options){- var turned = options.TurnsOrMirrors || image.Orientation is not (OrientationType.TopLeft or OrientationType.Undefined);- image.AutoOrient();-- // After AutoOrient, so a phone photo turns the way it looks, not the way its pixels are stored.switch (options.Rotation){case ImageRotation.Right90:@@ -184,6 +180,16 @@ internal static class ImageEncoderimage.Flop();else if (options.Flip == ImageFlip.Vertical)image.Flip();+ }++ /// <summary>Applies resize / colour / metadata options to one frame.</summary>+ public static void ApplyOptions(IMagickImage<byte> image, ImageOptions options, string targetFormat)+ {+ var turned = options.TurnsOrMirrors || image.Orientation is not (OrientationType.TopLeft or OrientationType.Undefined);+ image.AutoOrient();++ // After AutoOrient, so a phone photo turns the way it looks, not the way its pixels are stored.+ Turn(image, options);// The small preview kept in the EXIF data still shows the picture as it was: Explorer and photo apps that// use it would show it unturned. As HeicWriter does, drop it once the pixels turned.if (turned && image.GetExifProfile() is { } exif && exif.ThumbnailLength > 0)diff --git a/src/Filee.Engines/Pdf/PdfiumConverter.cs b/src/Filee.Engines/Pdf/PdfiumConverter.csindex a571d0b6..d964c896 100644--- a/src/Filee.Engines/Pdf/PdfiumConverter.cs+++ b/src/Filee.Engines/Pdf/PdfiumConverter.cs@@ -62,6 +62,8 @@ public sealed class PdfiumConverter : IConverter, IImageTransformervar frame = ToMagick(bitmap, dpi);if (stack){+ // Each page turns, then they stack: a turned long image would lie on its side instead.+ ImageEncoder.Turn(frame, step.Preset.Image);if (step.Preset.Pdf.TrimPageMargins)TrimVertically(frame, dpi);collected.Add(frame); // stacked after the loop; the image options apply to the tall image@@ -91,11 +93,14 @@ public sealed class PdfiumConverter : IConverter, IImageTransformerif (stack){- var parts = step.Preset.Pdf.SplitTallImage ? Split(collected, step.To, step.Preset.Image) : [collected];+ var unturned = step.Preset.Image.Clone(); // the pages are turned already+ unturned.Rotation = ImageRotation.None;+ unturned.Flip = ImageFlip.None;+ var parts = step.Preset.Pdf.SplitTallImage ? Split(collected, step.To, unturned) : [collected];for (var part = 0; part < parts.Count; part++){using var tall = Stack(parts[part]);- ImageEncoder.ApplyOptions(tall, step.Preset.Image, step.To); // also scales it to fit the format+ ImageEncoder.ApplyOptions(tall, unturned, step.To); // also scales it to fit the formatif (step.Output.Allocate(extension, parts.Count > 1 ? $"_{part + 1}" : null) is { } path){tall.Write(path, format);diff --git a/tests/Filee.Engines.Tests/ImageToolTests.cs b/tests/Filee.Engines.Tests/ImageToolTests.csindex c2af3a12..14f4b2f6 100644--- a/tests/Filee.Engines.Tests/ImageToolTests.cs+++ b/tests/Filee.Engines.Tests/ImageToolTests.cs@@ -188,6 +188,32 @@ public class ImageToolTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.False(File.Exists(Path.Combine(dir, "doc_left.pdf")));}+ [Fact]+ public async Task A_long_image_of_turned_pages_still_runs_top_to_bottom()+ {+ var dir = fx.NewFolder();+ var source = Path.Combine(dir, "slides.pdf");+ using (var pdf = new PdfSharp.Pdf.PdfDocument())+ {+ for (var i = 0; i < 2; i++)+ {+ var page = pdf.AddPage();+ page.Width = PdfSharp.Drawing.XUnit.FromPoint(200); // landscape slides+ page.Height = PdfSharp.Drawing.XUnit.FromPoint(100);+ }+ pdf.Save(source);+ }+ var preset = Tool("pdf-long-image");+ preset.Image.Rotation = ImageRotation.Right90;++ var job = await fx.ConvertAsync([source], preset);++ Assert.Equal(JobState.Completed, job.State);+ using var result = new MagickImage(job.Outputs.Single());+ // Each page turned (100 × 200), then stacked: four times as tall as wide, not the whole strip on its side.+ Assert.InRange(result.Height / (double)result.Width, 3.5, 4.5);+ }+[Fact]public async Task An_image_into_a_PDF_is_turned_on_the_way(){