How to get height and width of image style? We have a custom module mymodule where we call mymodule_tokens so that we can assign custom token values from the current node. These custom tokens are used to insert values in the Article's MetaTag fields. 

Problem

When fetching the default value based on an Image Style (e.g. medium), I cannot get the Height and Width based on the Configuration for the Image Style. How can I get the height and width from the Image Style?

What I have tried, but cannot get the height and width:

function mymodule_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  $node = $data['node'];
  $node_image = $node-&gt;field_image;
  $field_image_style = 'medium';

  if($node_image-&gt;isEmpty()) {
    $field_info = FieldConfig::loadByName('node', 'article, 'field_image');
    $image_uuid = $field_info-&gt;getSetting('default_image')['uuid'];
    $image = Drupal::service('entity.repository')-&gt;loadEntityByUuid('file', $image_uuid);
    $field_image_url = ImageStyle::load($field_image_style)-&gt;buildUrl($image-&gt;getFileUri());
    $image_style = ImageStyle::load($field_image_style)-&gt;getEffects()-&gt;getConfiguration();

    // Both give me error messages.
    $image_style_height = $image_style-&gt;get('height');
    $image_style_width = $image_style-&gt;get('width');
  }
  else {
    $field_image_url = $node_field_image-&gt;entity-&gt;getFileUri();
    $image_style = ImageStyle::load($field_image_style)-&gt;getEffects()-&gt;getConfiguration();

    // Both give me error messages.
    $image_style_height = $image_style-&gt;get('height');
    $image_style_width = $image_style-&gt;get('width');
  }

  foreach($tokens as $name =&gt; $original) {
    switch ($name) {
      case 'custom_image_url':
        $replacements[$original] = $field_image_url;
        break;
      case 'custom_image_height':
        $replacements[$original] = $image_style_height;
        break;
      case 'custom_image_width':
        $replacements[$original] = $image_style_width;
       break;
    }
  }    

  return $replacements;
}


Error Message


  Error: Call to a member function get() on array in mymodule_tokens()


When printing ImageStyle::load($field_image_style) using Kint this is what we get.



When printing $image_style, I get the following.



Using core or contrib tokens rather than custom tokens

Using the following tokens only work when there is a value added to the field_image field, but when the field_image is not populated, the token is not able to return the default image that is populated on the field's settings page.

[node:field_image:medium:height]
[node:field_image:medium:url]
[node:field_image:medium:width]
No Sssweat's answer inspired this. 

I added this before if statement (if($node_image-&gt;isEmpty())) to get the height and width. 

$image_style_config = (ImageStyle::load('medium')-&gt;getEffects()-&gt;getConfiguration());

foreach($image_style_config as $config) { 
  if(isset($config['data'])) {
    if(isset($config['data']['height'])) {
      $image_style_height = $config['data']['height'];
    }
    if(isset($config['data']['width'])) {
      $image_style_width = $config['data']['width'];
    }
  }
}