Probably the best place to do this is in a node preprocess function and then attach it for the render array. How it could be:
/** * Implements hook_preprocess_node(). */ function themename_preprocess_node(&$variables) { // Load the image field values from the node. $image_field = $variables['node']->get('field_image')->getValue(); // Get the first item of the field values. $image_field = reset($image_field); // Load file by the file id. $image = file_load($image_field['target_id']); // Create the actual HTML by creating a render array. $og_image = [ '#type' => 'html_tag', '#tag' => 'meta', '#attributes' => [ 'property' => 'og:image', 'content' => $image->toUrl(), ], ]; $variables['#attached']['html_head'][] = [$og_image, 'og:image']; }