Search code examples
phpwordpressimagewoocommercesize

How to get the image size details from size name in WooCommerce?


in WooCommerce or in WordPress in general we have size names like: thumbnail, woocommerce_thumbnail, woocommerce_gallery_thumbnail ...

How to get the size width and height just from the size name ?

I mean exactly the contrary of Get image's size name from it's dimension


Solution

  • You can use the WooCommerce dedicated function wc_get_image_size( $size_name ) like:

    $sizes = array('thumbnail', 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail');
    
    echo '<ul>';
    foreach( $sizes as $size_name ) {
        extract( wc_get_image_size( $size_name ) );
    
        echo '<li>Size "'. $size_name .'" ( width: '. $width .'px | height: '.$height.'px | crop: '.$crop.' )</li>';
    }
    echo '</ul>';
    

    This will display in a list for each size name, the width, height and crop values.

    You can use print_r() to visualize the array of values from a defined size name like:

    echo '<pre>' . print_r( wc_get_image_size( 'woocommerce_gallery_thumbnail' ), true ) . '</pre>';
    

    You can also see all sizes details in wc_get_image_size() function source code.