• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / wp_calculate_image_sizes() – Creates a ‘sizes’ attribute value for an image.

You appear to be a bot. Output may be restricted

Description

Creates a 'sizes' attribute value for an image.

Usage

$string|false = wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id );

Parameters

$size
( string|int[] ) required – Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
$image_src
( string ) optional – Optional. The URL to the image file. Default null.
$image_meta
( array ) optional – Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. Default null.
$attachment_id
( int ) optional – Optional. Image attachment ID. Either $image_meta or $attachment_id is needed when using the image size name as argument for `$size`. Default 0.

Returns

string|false A valid source size value for use in a 'sizes' attribute or false.

Source

File name: wordpress/wp-includes/media.php
Lines:

1 to 39 of 39
function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) {
  $width = 0;

  if ( is_array( $size ) ) {
    $width = absint( $size[0] );
  } elseif ( is_string( $size ) ) {
    if ( ! $image_meta && $attachment_id ) {
      $image_meta = wp_get_attachment_metadata( $attachment_id );
    }

    if ( is_array( $image_meta ) ) {
      $size_array = _wp_get_image_size_from_meta( $size, $image_meta );
      if ( $size_array ) {
        $width = absint( $size_array[0] );
      }
    }
  }

  if ( ! $width ) {
    return false;
  }

  // Setup the default 'sizes' attribute.
  $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width );

  
/**
 * Filters the output of 'wp_calculate_image_sizes()'.
 *
 * @since 4.4.0
 *
 * @param string       $sizes         A source size value for use in a 'sizes' attribute.
 * @param string|int[] $size          Requested image size. Can be any registered image size name, or
 *                                    an array of width and height values in pixels (in that order).
 * @param string|null  $image_src     The URL to the image file or null.
 * @param array|null   $image_meta    The image meta data as returned by wp_get_attachment_metadata() or null.
 * @param int          $attachment_id Image attachment ID of the original image or 0.
 */
  return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: wp_calculate_image_sizes
Plugin ref: WordPress
Version: 5.6.2
Sourcefile: wp-includes/media.php
File ref: wp-includes/media.php
Deprecated?: No
API Letters: C,I,S,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6.2
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites