- Updates logic behind image dimensions based on column width
This commit is contained in:
@@ -23,18 +23,18 @@ class Image {
|
||||
$column_width = ColumnsHelper::columnWidth($column_count);
|
||||
$padded_width = StylesHelper::$padding_width * 2;
|
||||
// scale image to fit column width
|
||||
if($element['width'] > $column_width ||
|
||||
($element['width'] < $column_width && $element['fullWidth'] === true)
|
||||
) {
|
||||
if($element['width'] > $column_width) {
|
||||
$ratio = $element['width'] / $column_width;
|
||||
$element['width'] = $column_width;
|
||||
$element['height'] = ceil($element['height'] / $ratio);
|
||||
$element['height'] = (int) ceil($element['height'] / $ratio);
|
||||
}
|
||||
// resize image if the image is padded and wider than column width
|
||||
if($element['fullWidth'] === false && $element['width'] >= $column_width) {
|
||||
$ratio = $element['width'] / ($element['width'] - $padded_width);
|
||||
$element['width'] = $element['width'] - $padded_width;
|
||||
$element['height'] = ceil($element['height'] / $ratio);
|
||||
// resize image if the image is padded and wider than padded column width
|
||||
if($element['fullWidth'] === false &&
|
||||
$element['width'] > ($column_width - $padded_width)
|
||||
) {
|
||||
$ratio = $element['width'] / ($column_width - $padded_width);
|
||||
$element['width'] = $column_width - $padded_width;
|
||||
$element['height'] = (int) ceil($element['height'] / $ratio);
|
||||
}
|
||||
return $element;
|
||||
}
|
||||
|
@@ -127,7 +127,7 @@ class NewsletterRendererCest {
|
||||
}
|
||||
|
||||
function itAdjustsImageDimensions() {
|
||||
// image gets scaled down when image width > column width & it's not padded
|
||||
// image gets scaled down when image width > column width
|
||||
$image = array(
|
||||
'width' => 800,
|
||||
'height' => 600,
|
||||
@@ -136,31 +136,23 @@ class NewsletterRendererCest {
|
||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||
expect($new_image_dimensions['width'])->equals(660);
|
||||
expect($new_image_dimensions['height'])->equals(495);
|
||||
// image gets scaled up when image width < column width & it's not padded
|
||||
$image['width'] = 100;
|
||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||
expect($new_image_dimensions['width'])->equals(660);
|
||||
// nothing happens when image width = column width
|
||||
$image['width'] = 660;
|
||||
$image['width'] = 661;
|
||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||
expect($new_image_dimensions['width'])->equals(660);
|
||||
// image is reduced by 40px when it's padded
|
||||
// nothing happens when image width < column width
|
||||
$image['width'] = 659;
|
||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||
expect($new_image_dimensions['width'])->equals(659);
|
||||
// image is reduced by 40px when it's width > padded column width
|
||||
$image['width'] = 621;
|
||||
$image['fullWidth'] = false;
|
||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||
expect($new_image_dimensions['width'])->equals(620);
|
||||
}
|
||||
|
||||
function itAdjustImageSizeBasedOnColumnWidth() {
|
||||
$newsletter = json_decode($this->newsletter['body'], true);
|
||||
$template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][1];
|
||||
$template['width'] = '800px';
|
||||
$DOM = $this->DOM_parser->parseStr(Image::render($template, $columnCount = 2));
|
||||
// 800px resized to 330px (2-column layout) and 40px padding applied
|
||||
expect($DOM('tr > td > img', 0)->attr('width'))->equals(290);
|
||||
$template['width'] = '280px';
|
||||
$DOM = $this->DOM_parser->parseStr(Image::render($template, $columnCount = 2));
|
||||
// 280px image should not be resized and padding should not be applied
|
||||
expect($DOM('tr > td > img', 0)->attr('width'))->equals(280);
|
||||
// nothing happens when image with < padded column width
|
||||
$image['width'] = 619;
|
||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||
expect($new_image_dimensions['width'])->equals(619);
|
||||
}
|
||||
|
||||
function itRendersText() {
|
||||
|
Reference in New Issue
Block a user