diff --git a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxReplacedElementFactory.java b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxReplacedElementFactory.java
index 26a2fdcf5..30231d0f8 100644
--- a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxReplacedElementFactory.java
+++ b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxReplacedElementFactory.java
@@ -69,13 +69,15 @@ public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box,
int intrinsicWidth = fsImage.getWidth();
if (hasMaxWidth && hasMaxHeight) {
- double rw = (double) intrinsicWidth / (double) maxWidth;
- double rh = (double) intrinsicHeight / (double) maxHeight;
+ if (intrinsicWidth > maxWidth || intrinsicHeight > maxHeight) {
+ double rw = (double) intrinsicWidth / (double) maxWidth;
+ double rh = (double) intrinsicHeight / (double) maxHeight;
- if (rw > rh) {
- fsImage.scale((int) maxWidth, -1);
- } else {
- fsImage.scale(-1, (int) maxHeight);
+ if (rw > rh) {
+ fsImage.scale((int) maxWidth, -1);
+ } else {
+ fsImage.scale(-1, (int) maxHeight);
+ }
}
} else if (hasMaxWidth && intrinsicWidth > maxWidth) {
fsImage.scale((int) maxWidth, -1);