AMP requires images to have width and height attributes. When these attributes aren’t present in an image tag, AMP-WP will attempt to determine them for the image.
Extraction Methods #
Concurrent Dimension Extraction – PHP 5.3+ and cURL #
If you’re using PHP 5.3+ and have the cURL extension installed, AMP-WP will attempt to determine dimensions for all images that need them concurrently. Only the minimum number of bytes required to determine the dimensions for a given image type are retrieved. Dimensions are then cached via transients for subsequent requests. This is the fastest and therefore recommended method.
Sequential Dimension Extraction – PHP 5.2 or no cURL #
If you’re using PHP 5.2 or do not have the cURL extension installed, AMP-WP will attempt to determine image dimensions sequentially. Only the minimum number of bytes required to determine the dimensions for a given image type are retrieved, but the time it takes to retrieve each image’s dimensions sequentially can still add up. Dimensions are then cached via transients for subsequent requests.
Custom Dimension Extraction #
You can implement your own image dimension extraction method by adding a callback to the amp_extract_image_dimensions_batch filter.
amp_extract_image_dimensions_batch callback functions take a single argument, $dimensions by convention, which is a map/array of image urls to either an array containing the dimensions of the image at the url (if another callback for the filter was able to determine them), or false if the dimensions have yet to be determined, e.g.
array(
'http://i0.wp.com/placehold.it/350x150.png' => array(
'width' => 350,
'height' => 150,
),
'http://i0.wp.com/placehold.it/1024x768.png' => false,
);
Code language: PHP (php)
Your custom dimension extraction callback would iterate through the mappings contained in this single argument, determining dimensions via your custom method for all image url keys whose values are not arrays of dimensions, e.g.
function my_custom_dimension_extraction_callback( $dimensions ) {
foreach ( $dimensions as $url => $value ) {
// Skip if dimensions have already been determined for this image.
if ( is_array( $value ) ) {
continue;
}
$width = <YOUR CUSTOM CODE TO DETERMINE WIDTH>
$height = <YOUR CUSTOM CODE TO DETERMINE HEIGHT>
$dimensions[ $url ] = array(
'width' => $width,
'height' => $height,
);
}
return $dimensions;
Code language: PHP (php)
Your callback needs to return $dimensions so that the value either cascades to the next callback that was added to the amp_extract_image_dimensions_batch filter or is returned to the apply_filter() call (if there are no more unprocessed callbacks).
The default callback provided by WP-AMP described above, extract_by_downloading_images, will fire unless explicitly removed, so be sure to remove it from the callback chain if you don’t want it to, e.g.
remove_filter( 'amp_extract_image_dimensions_batch', array( 'AMP_Image_Dimension_Extractor', 'extract_by_downloading_images' ), 999, 1 );
Code language: PHP (php)
Note: if you previously added a custom dimension extraction callback to the amp_extract_image_dimensions filter, you need to update it to hook into the amp_extract_image_dimensions_batch filter instead and iterate over the key value pairs in the single argument as per the example above.