Merge pull request #1687 from mailpoet/images-fix

Fix images without dimensions being given size of 0 [MAILPOET-1680]
This commit is contained in:
M. Shull
2018-12-12 12:34:06 -05:00
committed by GitHub
4 changed files with 56 additions and 7 deletions

View File

@@ -49,14 +49,16 @@ class StructureTransformer {
$image = $item; $image = $item;
} }
$width = $image->getAttribute('width');
$height = $image->getAttribute('height');
return array( return array(
'type' => 'image', 'type' => 'image',
'link' => $link, 'link' => $link,
'src' => $image->getAttribute('src'), 'src' => $image->getAttribute('src'),
'alt' => $image->getAttribute('alt'), 'alt' => $image->getAttribute('alt'),
'fullWidth' => $image_full_width, 'fullWidth' => $image_full_width,
'width' => $image->getAttribute('width'), 'width' => $width === null ? 'auto' : $width,
'height' => $image->getAttribute('height'), 'height' => $height === null ? 'auto' : $height,
'styles' => array( 'styles' => array(
'block' => array( 'block' => array(
'textAlign' => 'center', 'textAlign' => 'center',

View File

@@ -14,11 +14,17 @@ class Image {
$element['src'] = get_option('siteurl') . $element['src']; $element['src'] = get_option('siteurl') . $element['src'];
} }
$element['width'] = (int)$element['width']; $element['width'] = str_replace('px', '', $element['width']);
$element['height'] = (int)$element['height']; $element['height'] = str_replace('px', '', $element['height']);
$element = self::adjustImageDimensions($element, $column_base_width); if(is_numeric($element['width']) && is_numeric($element['height'])) {
$element['width'] = (int)$element['width'];
$element['height'] = (int)$element['height'];
$element = self::adjustImageDimensions($element, $column_base_width);
}
$max_width = is_numeric($element['width']) ? ($element['width'] . 'px') : '100%';
$image_template = ' $image_template = '
<img style="max-width:' . $element['width'] . 'px;" src="' . $element['src'] . '" <img style="max-width:' . $max_width . ';" src="' . $element['src'] . '"
width="' . $element['width'] . '" alt="' . $element['alt'] . '"/> width="' . $element['width'] . '" alt="' . $element['alt'] . '"/>
'; ';
if(!empty($element['link'])) { if(!empty($element['link'])) {

View File

@@ -28,7 +28,6 @@
} }
.mailpoet_image img { .mailpoet_image img {
height: auto; height: auto;
width: 100%;
-ms-interpolation-mode: bicubic; -ms-interpolation-mode: bicubic;
border: 0; border: 0;
display: block; display: block;

View File

@@ -334,6 +334,48 @@ class RendererTest extends \MailPoetTest {
expect($new_image_dimensions['width'])->equals(619); expect($new_image_dimensions['width'])->equals(619);
} }
function testItRendersImageWithAutoDimensions() {
$image = array(
'width' => 'auto',
'height' => 'auto',
'src' => 'https://example.com/image.jpg',
'link' => '',
'fullWidth' => false,
'alt' => 'some test alt text'
);
$rendered_image = Image::render($image, self::COLUMN_BASE_WIDTH);
expect($rendered_image)->contains('width="auto"');
expect($rendered_image)->contains('max-width:100%');
}
function testItAdjustImageDimensionsWithPx() {
$image = array(
'width' => '1000px',
'height' => '1000px',
'src' => 'https://example.com/image.jpg',
'link' => '',
'fullWidth' => false,
'alt' => 'some test alt text'
);
$rendered_image = Image::render($image, self::COLUMN_BASE_WIDTH);
expect($rendered_image)->contains('width="620"');
expect($rendered_image)->contains('max-width:620px');
}
function testItAdjustImageDimensionsWithoutPx() {
$image = array(
'width' => '1000',
'height' => '1000',
'src' => 'https://example.com/image.jpg',
'link' => '',
'fullWidth' => false,
'alt' => 'some test alt text'
);
$rendered_image = Image::render($image, self::COLUMN_BASE_WIDTH);
expect($rendered_image)->contains('width="620"');
expect($rendered_image)->contains('max-width:620px');
}
function testItRendersText() { function testItRendersText() {
$newsletter = $this->newsletter['body']; $newsletter = $this->newsletter['body'];
$template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][2]; $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][2];