From 3775046d05cbd31f469d76f2081ecae3e7317ebd Mon Sep 17 00:00:00 2001 From: William Tam Date: Fri, 18 Nov 2022 21:22:15 +0000 Subject: [PATCH] check dimensions of enlargement and use npr resizer if appropriate --- classes/NPRAPIWordpress.php | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/classes/NPRAPIWordpress.php b/classes/NPRAPIWordpress.php index 8c7a706..8c2c6ec 100644 --- a/classes/NPRAPIWordpress.php +++ b/classes/NPRAPIWordpress.php @@ -336,28 +336,34 @@ function update_posts_from_stories( $publish = TRUE, $qnum = false ) { // check the and then the crops, in this order "enlargement", "standard" if they don't exist, just get the image->src if ( !empty( $image->enlargement ) ) { $image_url = $image->enlargement->src; - } else { - if ( !empty( $image->crop ) && is_array( $image->crop ) ) { + } + if ( !empty( $image->crop ) && is_array( $image->crop ) ) { + foreach ( $image->crop as $crop ) { + if ( empty( $crop->type ) ) { + continue; + } + if ( 'enlargement' === $crop->type ) { + $image_url = $crop->src; + // sometimes enlargements are much larger than needed + if ( ( 1500 < $crop->height ) || ( 2000 < $crop->width ) ){ + // if there's no querystring already, add s=6 which media.npr.org resizes to a usable but large size + if ( strpos( $image_url, "?" ) === false ) { + $image_url .= "?s=6"; + } + } + } + } + if ( empty( $image_url ) ) { foreach ( $image->crop as $crop ) { if ( empty( $crop->type ) ) { continue; } - if ( 'enlargement' === $crop->type ) { + if ( 'standard' === $crop->type ) { $image_url = $crop->src; } } - if ( empty( $image_url ) ) { - foreach ( $image->crop as $crop ) { - if ( empty( $crop->type ) ) { - continue; - } - if ( 'standard' === $crop->type ) { - $image_url = $crop->src; - } - } - } } - } + } if ( empty( $image_url ) && !empty( $image->src ) ) { $image_url = $image->src;