- 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);
|
$column_width = ColumnsHelper::columnWidth($column_count);
|
||||||
$padded_width = StylesHelper::$padding_width * 2;
|
$padded_width = StylesHelper::$padding_width * 2;
|
||||||
// scale image to fit column width
|
// scale image to fit column width
|
||||||
if($element['width'] > $column_width ||
|
if($element['width'] > $column_width) {
|
||||||
($element['width'] < $column_width && $element['fullWidth'] === true)
|
|
||||||
) {
|
|
||||||
$ratio = $element['width'] / $column_width;
|
$ratio = $element['width'] / $column_width;
|
||||||
$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
|
// resize image if the image is padded and wider than padded column width
|
||||||
if($element['fullWidth'] === false && $element['width'] >= $column_width) {
|
if($element['fullWidth'] === false &&
|
||||||
$ratio = $element['width'] / ($element['width'] - $padded_width);
|
$element['width'] > ($column_width - $padded_width)
|
||||||
$element['width'] = $element['width'] - $padded_width;
|
) {
|
||||||
$element['height'] = ceil($element['height'] / $ratio);
|
$ratio = $element['width'] / ($column_width - $padded_width);
|
||||||
|
$element['width'] = $column_width - $padded_width;
|
||||||
|
$element['height'] = (int) ceil($element['height'] / $ratio);
|
||||||
}
|
}
|
||||||
return $element;
|
return $element;
|
||||||
}
|
}
|
||||||
|
@@ -127,7 +127,7 @@ class NewsletterRendererCest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function itAdjustsImageDimensions() {
|
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(
|
$image = array(
|
||||||
'width' => 800,
|
'width' => 800,
|
||||||
'height' => 600,
|
'height' => 600,
|
||||||
@@ -136,31 +136,23 @@ class NewsletterRendererCest {
|
|||||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||||
expect($new_image_dimensions['width'])->equals(660);
|
expect($new_image_dimensions['width'])->equals(660);
|
||||||
expect($new_image_dimensions['height'])->equals(495);
|
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
|
// nothing happens when image width = column width
|
||||||
$image['width'] = 660;
|
$image['width'] = 661;
|
||||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||||
expect($new_image_dimensions['width'])->equals(660);
|
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;
|
$image['fullWidth'] = false;
|
||||||
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||||
expect($new_image_dimensions['width'])->equals(620);
|
expect($new_image_dimensions['width'])->equals(620);
|
||||||
}
|
// nothing happens when image with < padded column width
|
||||||
|
$image['width'] = 619;
|
||||||
function itAdjustImageSizeBasedOnColumnWidth() {
|
$new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1);
|
||||||
$newsletter = json_decode($this->newsletter['body'], true);
|
expect($new_image_dimensions['width'])->equals(619);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function itRendersText() {
|
function itRendersText() {
|
||||||
|
Reference in New Issue
Block a user