Skip to content

Commit 0e5d561

Browse files
committed
adapt image width when maxImageWidth is set
Some mailers set up huge sizes that result in rendering problems, ie the image is wider than the available width.
1 parent a8b94a0 commit 0e5d561

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

lib/src/dom/image_transformers.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ class ImageTransformer implements DomTransformer {
2828
final data =
2929
toImageData(part, contentType.mediaType, configuration);
3030
imageElement.attributes['src'] = data;
31+
final maxImageWidth = configuration.maxImageWidth;
32+
if (maxImageWidth != null &&
33+
imageElement.attributes['width'] != null &&
34+
imageElement.attributes['height'] != null) {
35+
final width = int.tryParse(imageElement.attributes['width']!);
36+
final height = int.tryParse(imageElement.attributes['height']!);
37+
if (width != null && width > maxImageWidth && height != null) {
38+
final factor = maxImageWidth / width;
39+
imageElement.attributes['width'] = maxImageWidth.toString();
40+
imageElement.attributes['height'] =
41+
(height * factor).floor().toString();
42+
final styleAttribute = imageElement.attributes['style'];
43+
if (styleAttribute != null &&
44+
styleAttribute.contains('width')) {
45+
imageElement.attributes.remove('style');
46+
}
47+
}
48+
}
3149
if (imageElement.parent?.localName != 'a') {
3250
final linkCid =
3351
Uri.encodeComponent(cid.substring(1, cid.length - 1));

0 commit comments

Comments
 (0)