Get Media entity view type url in Drupal 9

  $fid = $variables['elements']['field_image_media'][0]['#item']->entity->id();
  $file = File::load($fid);
  // Get origin image URI.
  $image_uri = $file->getFileUri();
  // Load image style "thumbnail".
  $style = ImageStyle::load('uncropped');
  // Get URI.
  $uri = $style->buildUri($image_uri);
  // Get URL.
  $url = $style->buildUrl($image_uri);

  $image_factory = \Drupal::service('image.factory')->get($image_uri);
  $height = $image_factory->getToolkit()->getHeight();
  $width = $image_factory->getToolkit()->getWidth();

  $variables['field_image_media_extra'] = [
    'url' => $url,
    'height' => $height,
    'width' => $width,
  ];