Skip to content

Commit 2aa2ef4

Browse files
committed
[layouts] Fix data defined page sizes sometimes lead to incorrect
created atlas image sizes Because the exporter prefers to use the image size when it's specified (as is done in QGIS app), we need to ignore this when the aspect ratio of that size doesn't match the aspect ratio of the page to render. Fixes #18534
1 parent 543c7a7 commit 2aa2ef4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/core/layout/qgslayoutexporter.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ QImage QgsLayoutExporter::renderPageToImage( int page, QSize imageSize, double d
169169
( void )restorer;
170170

171171
QRectF paperRect = QRectF( pageItem->pos().x(), pageItem->pos().y(), pageItem->rect().width(), pageItem->rect().height() );
172+
173+
if ( imageSize.isValid() && ( !qgsDoubleNear( static_cast< double >( imageSize.width() ) / imageSize.height(),
174+
paperRect.width() / paperRect.height(), 0.008 ) ) )
175+
{
176+
// specified image size is wrong aspect ratio for paper rect - so ignore it and just use dpi
177+
// this can happen e.g. as a result of data defined page sizes
178+
// see https://issues.qgis.org/issues/18534
179+
imageSize = QSize();
180+
}
181+
172182
return renderRegionToImage( paperRect, imageSize, dpi );
173183
}
174184

tests/src/python/test_qgslayoutexporter.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,22 @@ def testExportToImage(self):
378378

379379
# image size
380380
settings.imageSize = QSize(600, 851)
381-
382381
rendered_file_path = os.path.join(self.basetestpath, 'test_exporttoimagesize.png')
383382
self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success)
384383
self.assertFalse(os.path.exists(rendered_file_path))
385384
page2_path = os.path.join(self.basetestpath, 'test_exporttoimagesize_2.png')
386385
self.assertTrue(self.checkImage('exporttoimagesize_page2', 'exporttoimagesize_page2', page2_path))
387386

387+
# image size with incorrect aspect ratio
388+
# this can happen as a result of data defined page sizes
389+
settings.imageSize = QSize(851, 600)
390+
rendered_file_path = os.path.join(self.basetestpath, 'test_exporttoimagesizebadaspect.png')
391+
self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success)
392+
393+
page2_path = os.path.join(self.basetestpath, 'test_exporttoimagesizebadaspect_2.png')
394+
im = QImage(page2_path)
395+
self.assertTrue(self.checkImage('exporttoimagesize_badaspect', 'exporttoimagedpi_page2', page2_path), '{}x{}'.format(im.width(), im.height()))
396+
388397
def testExportToPdf(self):
389398
md = QgsProject.instance().metadata()
390399
md.setTitle('proj title')

0 commit comments

Comments
 (0)