How do I automatically generate image styles?
Image derivatives will be automatically generated once requested and not-yet existing.

But if you are asking how to programmatically create image derivatives checkout ImageStyle::createDerivative.

Here's a sample snippet were I did exactly that:

$field_name = 'field_my_image';
$image_style_name = 'my_image_style';
$image = [];

// Get the original image URI.
$original_image = $node-&gt;{$field_name}-&gt;entity-&gt;getFileUri();

// Load the image style.
$style = \Drupal::entityTypeManager()
  -&gt;getStorage('image_style')
  -&gt;load($image_style_name);

// Get the styled image derivative.
$destination = $style-&gt;buildUri($original_image);

// If the derivative doesn't exist yet (as the image style may have been
// added post launch), create it.
if (!file_exists($destination)) {
  $style-&gt;createDerivative($original_image, $destination);
}

// Do whatever else you need to do.
$image['url'] = file_url_transform_relative($style-&gt;buildUrl($original_image));
$properties = $node-&gt;{$field_name}-&gt;first()-&gt;getValue();
$image['alt'] = $properties['alt'];
$image['title'] = $properties['title'];