Preprocess image: how to get parent entity?
Here is the the "real" code counterpart to the pseudecode from the question:

function THEMENAME_preprocess_responsive_image_formatter(array &amp;$variables) {
  // get the imagefield's parent entity
  $fieldParent = $variables['item']-&gt;getEntity();

  // this line is only needed if using media entities (= entity references)
  // must disable entity view caching to work correctly
  $fieldParent = $fieldParent-&gt;_referringItem-&gt;getEntity();

  // get the image width from the custom field
  $myImgSizes = $fieldParent-&gt;get('field_image_size')-&gt;getString();
  // can't change attribute here =&gt; store it temporarily
  $variables['responsive_image']['#attributes']['data-overridesizes'] = $myImgSizes;
}

function THEMENAME_preprocess_responsive_image(array &amp;$variables) {
  //override sizes attribute with my calculated value
  if (key_exists('data-overridesizes', $variables['img_element']['#attributes'])) {
    $variables['img_element']['#attributes']['sizes'] = $variables['img_element']['#attributes']['data-overridesizes'];
    unset($variables['img_element']['#attributes']['data-overridesizes']);
  }
}


If using media (=entity references) you also have to disable view cache for entites (see this question)

function MODULENAME_entity_view(&amp;$build, $entity, $display, $view_mode) {
  if ($entity-&gt;getEntityTypeId() == 'media' &amp;&amp; $entity-&gt;bundle() == 'image' &amp;&amp; $view_mode == 'my_uncached_viewmode'){
    unset($build['#cache']['keys']);
  }
}