From 47472632588a1b21c6c6f8fcb3fb62745a6a8044 Mon Sep 17 00:00:00 2001 From: Marc Silverman Date: Sat, 26 Nov 2016 19:19:30 -0800 Subject: [PATCH] Update class-amp-img-sanitizer.php If only one image dimension is set, the img sanitizer would ignore it and use the actual image sizes instead. This proposed change checks if only one dimension was set, and if so, calculates the other. --- includes/sanitizers/class-amp-img-sanitizer.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/includes/sanitizers/class-amp-img-sanitizer.php b/includes/sanitizers/class-amp-img-sanitizer.php index 7b2025b788b..d37469db223 100644 --- a/includes/sanitizers/class-amp-img-sanitizer.php +++ b/includes/sanitizers/class-amp-img-sanitizer.php @@ -42,6 +42,14 @@ public function sanitize() { $new_attributes['width'] = $dimensions[0]; $new_attributes['height'] = $dimensions[1]; } + // Take care of the case when only one dimension is set, leaving the browser to calculate the other. + if ( ! $old_attributes['height'] ) { + $new_attributes['height'] = ( $old_attributes['width'] / $new_attributes['width'] ) * $new_attributes['height']; + $new_attributes['width'] = $old_attributes['width']; + } else if ( ! $old_attributes['width'] ) { + $new_attributes['width'] = ( $old_attributes['height'] / $new_attributes['height'] ) * $new_attributes['width']; + $new_attributes['height'] = $old_attributes['height']; + } } // Final fallback when we have no dimensions.