'ImageObject', 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ), 'width' => $size, 'height' => $size, ); return $metadata; } function wpcom_amp_add_image_to_metadata( $metadata, $post ) { if ( ! class_exists( 'Jetpack_PostImages' ) ) { return wpcom_amp_add_fallback_image_to_metadata( $metadata ); } $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true, 'avatar_size' => 200, // AMP already attempts these 'from_thumbnail' => false, 'from_attachment' => false, ) ); if ( empty( $image ) ) { return wpcom_amp_add_fallback_image_to_metadata( $metadata ); } if ( ! isset( $image['src_width'] ) ) { $dimensions = wpcom_amp_extract_image_dimensions_from_getimagesize( array( $image['src'] => false, ) ); if ( false !== $dimensions[ $image['src'] ] ) { $image['src_width'] = $dimensions['width']; $image['src_height'] = $dimensions['height']; } } $metadata['image'] = array( '@type' => 'ImageObject', 'url' => $image['src'], 'width' => $image['src_width'], 'height' => $image['src_height'], ); return $metadata; } function wpcom_amp_add_fallback_image_to_metadata( $metadata ) { $metadata['image'] = array( '@type' => 'ImageObject', 'url' => staticize_subdomain( 'https://wordpress.com/i/blank.jpg' ), 'width' => 200, 'height' => 200, ); return $metadata; } add_action( 'amp_extract_image_dimensions_batch_callbacks_registered', 'wpcom_amp_extract_image_dimensions_batch_add_custom_callbacks' ); function wpcom_amp_extract_image_dimensions_batch_add_custom_callbacks() { // If images are being served from Photon or WP.com files, try extracting the size using querystring. add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_querystring', 9, 1 ); // Hook in before the default extractors. // Uses a special wpcom lib (wpcom_getimagesize) to extract dimensions as a last resort if we weren't able to figure them out. add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_getimagesize', 99, 1 ); // Our last resort, so run late // The wpcom override obviates this one, so take it out. remove_filter( 'amp_extract_image_dimensions_batch', array( 'AMP_Image_Dimension_Extractor', 'extract_by_downloading_images' ), 999, 1 ); } function wpcom_amp_extract_image_dimensions_from_querystring( $dimensions ) { foreach ( $dimensions as $url => $value ) { if ( is_array( $value ) ) { continue; } $host = parse_url( $url, PHP_URL_HOST ); if ( ! wp_endswith( $host, '.wp.com' ) || ! wp_endswith( $host, '.files.wordpress.com' ) ) { continue; } parse_str( parse_url( $url, PHP_URL_QUERY ), $query ); $w = isset( $query['w'] ) ? absint( $query['w'] ) : false; $h = isset( $query['h'] ) ? absint( $query['h'] ) : false; if ( false !== $w && false !== $h ) { $dimensions[ $url ] = array( 'width' => $w, 'height' => $h, ); } } return $dimensions; } function wpcom_amp_extract_image_dimensions_from_getimagesize( $dimensions ) { if ( ! function_exists( 'require_lib' ) ) { return $dimensions; } require_lib( 'wpcom/imagesize' ); foreach ( $dimensions as $url => $value ) { if ( is_array( $value ) ) { continue; } $result = wpcom_getimagesize( $url ); if ( is_array( $result ) ) { $dimensions[ $url ] = array( 'width' => $result[0], 'height' => $result[1], ); } } return $dimensions; }