Skip to content

Commit b720219

Browse files
Merge pull request #1198 from SixLabors/js/fix-1195
Ensure min dimension of 1px following rounding.
2 parents de74ad4 + 0fd2a4c commit b720219

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeHelper.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static (Size, Rectangle) CalculateTargetLocationAndBounds(Size sourceSize
7272

7373
// case ResizeMode.Stretch:
7474
default:
75-
return (new Size(width, height), new Rectangle(0, 0, width, height));
75+
return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(0, 0, Sanitize(width), Sanitize(height)));
7676
}
7777
}
7878

@@ -143,7 +143,7 @@ private static (Size, Rectangle) CalculateBoxPadRectangle(
143143
}
144144

145145
// Target image width and height can be different to the rectangle width and height.
146-
return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight));
146+
return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight)));
147147
}
148148

149149
// Switch to pad mode to downscale and calculate from there.
@@ -253,7 +253,7 @@ private static (Size, Rectangle) CalculateCropRectangle(
253253
}
254254

255255
// Target image width and height can be different to the rectangle width and height.
256-
return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight));
256+
return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight)));
257257
}
258258

259259
private static (Size, Rectangle) CalculateMaxRectangle(
@@ -282,7 +282,7 @@ private static (Size, Rectangle) CalculateMaxRectangle(
282282
}
283283

284284
// Replace the size to match the rectangle.
285-
return (new Size(targetWidth, targetHeight), new Rectangle(0, 0, targetWidth, targetHeight));
285+
return (new Size(Sanitize(targetWidth), Sanitize(targetHeight)), new Rectangle(0, 0, Sanitize(targetWidth), Sanitize(targetHeight)));
286286
}
287287

288288
private static (Size, Rectangle) CalculateMinRectangle(
@@ -330,7 +330,7 @@ private static (Size, Rectangle) CalculateMinRectangle(
330330
}
331331

332332
// Replace the size to match the rectangle.
333-
return (new Size(targetWidth, targetHeight), new Rectangle(0, 0, targetWidth, targetHeight));
333+
return (new Size(Sanitize(targetWidth), Sanitize(targetHeight)), new Rectangle(0, 0, Sanitize(targetWidth), Sanitize(targetHeight)));
334334
}
335335

336336
private static (Size, Rectangle) CalculatePadRectangle(
@@ -398,7 +398,7 @@ private static (Size, Rectangle) CalculatePadRectangle(
398398
}
399399

400400
// Target image width and height can be different to the rectangle width and height.
401-
return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight));
401+
return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight)));
402402
}
403403

404404
private static (Size, Rectangle) CalculateManualRectangle(
@@ -419,9 +419,11 @@ private static (Size, Rectangle) CalculateManualRectangle(
419419
int targetHeight = targetRectangle.Height > 0 ? targetRectangle.Height : height;
420420

421421
// Target image width and height can be different to the rectangle width and height.
422-
return (new Size(width, height), new Rectangle(targetX, targetY, targetWidth, targetHeight));
422+
return (new Size(Sanitize(width), Sanitize(height)), new Rectangle(targetX, targetY, Sanitize(targetWidth), Sanitize(targetHeight)));
423423
}
424424

425425
private static void ThrowInvalid(string message) => throw new InvalidOperationException(message);
426+
427+
private static int Sanitize(int input) => Math.Max(1, input);
426428
}
427429
}

tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,5 +605,21 @@ public void CanResizeExifIssueImages<TPixel>(TestImageProvider<TPixel> provider)
605605
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
606606
}
607607
}
608+
609+
[Fact]
610+
public void Issue1195()
611+
{
612+
using (var image = new Image<Rgba32>(2, 300))
613+
{
614+
var size = new Size(50, 50);
615+
image.Mutate(x => x
616+
.Resize(
617+
new ResizeOptions
618+
{
619+
Size = size,
620+
Mode = ResizeMode.Max
621+
}));
622+
}
623+
}
608624
}
609625
}

0 commit comments

Comments
 (0)