How to find the url of a media library image field in a preprocess function?
I wrote a little module I found helpful to get the media entity image values.
custom_helper_services/custom_helper_services.info.yml:
name: 'Custom Helper Services'
type: module
description: 'Provides useful services for working with themes.'
core_version_requirement: ^8 || ^9
package: 'Custom'

custom_helper_services/custom_helper_services.module:
&lt;?php

/**
 * @file
 * Contains custom_helper_services.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function custom_helper_services_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the custom_helper_services module.
    case 'help.page.custom_helper_services':
      $output = '';
      $output .= '&lt;h3&gt;' . t('About') . '&lt;/h3&gt;';
      $output .= '&lt;p&gt;' . t('Provides helpful service functions.') . '&lt;/p&gt;';
      return $output;

    default:
  }
}

custom_helper_services/custom_helper_services.services.yml:
services:
  chs:
    class: Drupal\custom_helper_services\Services\CustomHelperServices
    arguments: ['@entity_type.manager','@current_route_match','@entity.repository']

custom_helper_services/src/Services/CustomHelperServices.php:
&lt;?php

namespace Drupal\custom_helper_services\Services;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;

/**
 * Class CustomHelperServices.
 *
 * Provides helper services to be used in modules and themes.
 */
class CustomHelperServices {

  /**
   * Entity Type Manager.
   *
   * @var Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Route Match Interface.
   *
   * @var Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Entity Repository Interface.
   *
   * @var Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * CustomHelperServices constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity Type Manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
   *   The Route Match Interface.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
   *   The Entity Repository Interface.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, RouteMatchInterface $routeMatch, EntityRepositoryInterface $entityRepository) {
    $this-&gt;entityTypeManager = $entityTypeManager;
    $this-&gt;routeMatch = $routeMatch;
    $this-&gt;entityRepository = $entityRepository;
  }

  /**
   * Build our media style path service.
   */
  public function getMediaFieldImage($mediaField = NULL, $imageStyle = NULL) {

    if ($mediaField &amp;&amp; $mediaField-&gt;entity) {

      // Get image.
      $mediaImage = $mediaField-&gt;entity
        -&gt;get('field_media_image');

      // Get image attributes.
      $alt = $mediaImage-&gt;alt;
      $width = $mediaImage-&gt;width;
      $height = $mediaImage-&gt;height;
      $title = $mediaImage-&gt;title;

      // Get image entity.
      $mediaImageEntity = $mediaImage-&gt;entity;

      // Get image mime type.
      $mediaImageMimeType = $mediaImageEntity-&gt;getMimeType();

      // Get image file URI.
      $mediaImageEntityUri = $mediaImageEntity-&gt;getFileUri();

      // Set image URL.
      $url = file_create_url($mediaImageEntityUri);

      // Set image style URL.
      if ($imageStyle &amp;&amp; $mediaImageMimeType !== &quot;image/svg+xml&quot;) {
        $url = $this-&gt;entityTypeManager
          -&gt;getStorage('image_style')
          -&gt;load($imageStyle)
          -&gt;buildUrl($mediaImageEntityUri);
      }

      return [
        'url' =&gt; $url,
        'alt' =&gt; $alt,
        'width' =&gt; $width,
        'height' =&gt; $height,
        'mimeType' =&gt; $mediaImageMimeType,
        'title' =&gt; $title,
      ];
    }

    return NULL;
  }

}

Then in preprocess node, for example, you can call:
$variables['myVariable'] = \Drupal::service('chs')-&gt;getMediaFieldImage($node-&gt;get(&quot;field_name&quot;), &quot;image_style&quot;);
// Just need to pass the media field name. The image style is optional.

Then in a Twig template you can access as:
{{ myVariable.url }}
{{ myVariable.alt }}
{{ myVariable.width }}
{{ myVariable.height }}
{{ myVariable.mimeType }}
{{ myVariable.title }}

I've stripped down this code to show just the media entity image get, so let me know if there are issues and I can update if needed.

A shorter version (using preprocess_node()):
use Drupal\image\Entity\ImageStyle;
function MYTHEME_preprocess_node(&amp;$variables) {
  $node = $variables['node'];
  if ($node-&gt;hasField('field_name') &amp;&amp; $node-&gt;get('field_name')-&gt;entity) {
    $imageURI = $node-&gt;field_name-&gt;entity-&gt;getFileUri();
    $variables['myVariable'] = ImageStyle::load('image_style')-&gt;buildUrl($imageURI);
  }
}