get_intermediate_image_sizes Get the Size Names but How Do I Get the Sizes?

I’m setting up a widget that allows to add an image to a sidebar / widget area.

Basically I have a dropdown that allows the user to select one of the available sizes inside the theme’s functions.php file using add_image_size().

Apart from the obvious ( renaming all the add_image_size() ) is there a way to get the size so that I can display that as well as the name?

This is my current drop down code:

<?php       
    $sizes = get_intermediate_image_sizes();
    $available_size  = '<select name="' . $this->get_field_name('image') . '" >' . "rn";
    $available_size .= '<option value="0" selected="selected">Choose</option>' . "rn";
    foreach ( $sizes as $size ) {
        if ( $instance['size'] == $size ) :
            $available_size .= '<option value="' . $size . '" selected="selected">' . $size . '</option>' . "rn";
        else :
            $available_size .= '<option value="' . $size . '">' . $size . '</option>' . "rn";         
        endif;
    }

    $available_size .= '</select>' . "rn";

    echo $available_size;
    ?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

<?php
global $_wp_additional_image_sizes;
print '<pre>';
print_r( $_wp_additional_image_sizes );
print '</pre>';

Method 2

Get all possible registered image size with their names

$wp_additional_image_sizes = wp_get_additional_image_sizes();

$sizes = array();
$get_intermediate_image_sizes = get_intermediate_image_sizes();

// Create the full array with sizes and crop info
foreach ($get_intermediate_image_sizes as $_size) {
  if (in_array($_size, array('thumbnail', 'medium', 'large'))) {
    $sizes[$_size]['width'] = get_option($_size . '_size_w');
    $sizes[$_size]['height'] = get_option($_size . '_size_h');
    $sizes[$_size]['crop'] = (bool) get_option($_size . '_crop');
  } elseif (isset($wp_additional_image_sizes[$_size])) {
    $sizes[$_size] = array(
      'width' => $wp_additional_image_sizes[$_size]['width'],
      'height' => $wp_additional_image_sizes[$_size]['height'],
      'crop' =>  $wp_additional_image_sizes[$_size]['crop']
    );
  }
}
foreach ($sizes as $key => $image_size) {
  echo  "<li> ⚡ {$key} ☛ ({$image_size['width']} x {$image_size['height']}) {$image_size['crop']} </li>";
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x