Skip to content

Instantly share code, notes, and snippets.

@bpb54321
Created August 24, 2016 19:09
Show Gist options
  • Select an option

  • Save bpb54321/1d4210da258a3d0b45bd7cac40606cc8 to your computer and use it in GitHub Desktop.

Select an option

Save bpb54321/1d4210da258a3d0b45bd7cac40606cc8 to your computer and use it in GitHub Desktop.
Given an image ID and some other parameters, returns as a string a responsive image tag using the srcset and sizes attributes.
/**
* Returns a string of an <img> tag with src, srcset and sizes attributes set.
* @param $attachment_id int The WordPress id for the attachment.
* @param $alt_string String The alt string for the image tag, e.g. "My image"
* @param string $class_string A string of classes to apply to the <img>
* @param array $sizes_to_exclude An array of strings, where each string is a named WordPress intermediate image size that you want to exclude from the srcset attribute.
* @param string $sizes_string The string that will become the sizes attribute of the <img>
* @return string
*/
function create_responsive_img_tag( $attachment_id = -1, $alt_string = '', $class_string = '', $sizes_to_exclude = [], $sizes_string = '' ) {
//The src attribute (default image file) will be of the large size of the image
$attachment_default_src_array = wp_get_attachment_image_src($attachment_id, 'large', false);
$attachment_default_src = $attachment_default_src_array[0];
$widths_and_urls = get_image_widths_and_urls( $attachment_id, $sizes_to_exclude );
//Create srcset string
$srcset_array = [];
//$sizes_array = [];
foreach ( $widths_and_urls as $current_width_and_url ) {
$image_url = $current_width_and_url["url"];
$image_width = $current_width_and_url["width"];
$current_srcset_element = "{$image_url} {$image_width}w";
$srcset_array[] = $current_srcset_element;
}
$srcset_string = implode( ", ", $srcset_array );
$output = "<img class='{$class_string}' srcset='{$srcset_string}' sizes='{$sizes_string}' alt='{$alt_string}' src='{$attachment_default_src}'>";
return $output;
}
/**
* Helper function for getting the widths and url's of all the available crops of an image.
* @param Number $id: The id of the image that you need the info for
* @param Array $image_sizes_to_exclude An array of strings with the names of image sizes to exclude from the output
* @return Array An indexed array, where at each index there is an associative array with array["width"] containing an image width and array["url"] containing the corresponding image url.
* Example: Array(
* [0] => Array(
* [width] => 300
* [url] => http://placehold.it/50x50
* ),
* [1] , etc
*/
function get_image_widths_and_urls($id, $image_sizes_to_exclude = [] ) {
//Get all the possible image sizes
$image_size_array = get_intermediate_image_sizes();
//Filter out sizes we don't want to include
foreach ( $image_sizes_to_exclude as $image_size_to_exclude ) {
$size_found_key = array_search( $image_size_to_exclude, $image_size_array );
if ( $size_found_key !== false ) {
unset( $image_size_array[ $size_found_key ] );
}
}
$image_width_array = [];
$widths_and_urls = [];
$i = 0;
foreach ( $image_size_array as $image_size_name ) {
$image_info_array = wp_get_attachment_image_src($id, $image_size_name);
$is_intermediate_image_size = $image_info_array[3]; //False if it's the original image, not a scaled version
if ( $is_intermediate_image_size ) {
$image_url = $image_info_array[0];
$image_width = $image_info_array[1];
$image_width_array[] = $image_width;
$widths_and_urls[] = [
"width" => $image_width,
"url" => $image_url,
];
}
$i++;
}
//Get the full size image
$full_size_image_info_array = wp_get_attachment_image_src($id, "full");
$image_url = $full_size_image_info_array[0];
$image_width = $full_size_image_info_array[1];
$image_width_array[] = $image_width;
$widths_and_urls[] = [
"width" => $image_width,
"url" => $image_url,
];
array_multisort ($image_width_array, SORT_ASC, SORT_NUMERIC, $widths_and_urls); //Sorts $widths_and_urls by the "width" key
return $widths_and_urls;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment