function ubik_imagery_sizes_metadata() { // Cache size data static $sizes = array(); if (!empty($sizes)) { return $sizes; } // Get all required data global $_wp_additional_image_sizes; $sizes_intermediate = get_intermediate_image_sizes(); $sizes_builtin = ubik_imagery_sizes_builtin(); // Loop through and build a cleaner array of size definitions foreach ($sizes_intermediate as $size) { // Reset all variables $width = $height = $orientation = $percent = $ratio = $crop = ''; // Make sure the size exists in the defaults or additional image size table if (in_array($size, $sizes_builtin) || isset($_wp_additional_image_sizes[$size])) { // Instructions specific to the built-in sizes if (in_array($size, $sizes_builtin)) { $width = get_option($size . '_size_w'); $height = get_option($size . '_size_h'); $crop = (bool) get_option($size . '_crop'); } else { $width = $_wp_additional_image_sizes[$size]['width']; $height = $_wp_additional_image_sizes[$size]['height']; $crop = (bool) $_wp_additional_image_sizes[$size]['crop']; } // Set ratio and orientation to false for images only bound in one dimension if (max($width, $height) >= 9999 || !is_int($width) || $width < 1 || !is_int($height) || $height < 0) { $orientation = false; $percent = false; $ratio = false; } else { $orientation = ubik_imagery_sizes_orientation($width, $height); $percent = ubik_imagery_sizes_percent($width, $height); $ratio = ubik_imagery_sizes_ratio($width, $height); } // Populate the sizes array $sizes[$size] = array('width' => $width, 'height' => $height, 'orientation' => $orientation, 'percent' => $percent, 'ratio' => $ratio, 'crop' => $crop); } } // Last chance to filter all defined image sizes $sizes = apply_filters('ubik_imagery_sizes_metadata', $sizes); // Don't forget to pass it back return $sizes; }
function ubik_imagery_img($id = '', $size = '', $alt = '', $context = '') { // Return a placeholder if we have no ID to work with; this is up to the theme to style if (empty($id)) { return array(apply_filters('ubik_imagery_placeholder', '<div class="no-image"></div>'), 0, 0); } // Initialize $html = $src = $srcset = $sizes = $width = $height = $dimensions = $attributes = ''; $class = array('ubik-img'); $size = apply_filters('ubik_imagery_size', $size); // Custom replacement for get_image_tag(); roll your own instead of using $html = get_image_tag( $id, $alt, $title, $align, $size ); list($src, $width, $height, $is_intermediate) = image_downsize($id, $size); // Add a styling hook for square images if (ubik_imagery_size_is_square($size)) { $class[] = 'square'; } // Another size was requested but WP returned the full image if (!$is_intermediate && $size !== 'full') { $size = 'full'; } // Provide an opportunity to filter the `src` attribute; here you can clear it, substitute a blank image, or do something else $src = esc_attr(apply_filters('ubik_imagery_src', $src, $id, $size)); if (!empty($src)) { $src = 'src="' . $src . '" '; } // `srcset` is easy; no filter here, just use the WordPress 4.4+ standard $srcset = wp_get_attachment_image_srcset($id, $size); if (!empty($srcset)) { $srcset = 'srcset="' . esc_attr($srcset) . '" '; } // `sizes` is slightly more complicated; these must be set at the theme level $sizes = ubik_imagery_sizes_attribute($size, $width, $context); if (!empty($sizes)) { $sizes = 'sizes="' . esc_attr($sizes) . '" '; } // Class $class = implode(' ', apply_filters('ubik_imagery_img_class', $class)); if (!empty($class)) { $class = 'class="' . esc_attr($class) . '"'; } // Aspect ratio $ratio = ubik_imagery_sizes_ratio($width, $height); if (!empty($ratio)) { $ratio = 'data-aspect-ratio="' . esc_attr($ratio) . '"'; } // Attributes $attributes = implode(' ', apply_filters('ubik_imagery_img_attributes', array('id' => 'id="wp-image-' . esc_attr($id) . '"', 'data-id' => 'data-id="' . esc_attr($id) . '"', 'class' => $class, 'ratio' => $ratio, 'schema' => 'itemprop="contentUrl"', 'alt' => 'alt="' . $alt . '"'))); // Return an array with the final width/height so these values can be passed up to the wrapper element for certain CSS styling tricks return array(apply_filters('ubik_imagery_img', '<img ' . apply_filters('ubik_imagery_src_html', $src) . apply_filters('ubik_imagery_srcset_html', $srcset) . apply_filters('ubik_imagery_sizes_html', $sizes) . apply_filters('ubik_imagery_dimensions', image_hwstring($width, $height), $width, $height) . $attributes . '>'), $width, $height); }