Let's suppose that $article is a node with an image field named field_image.

First we need to get the image file uri.

$img_uri = $article->field_image->entity->getFileUri();

Next we need to get the style object we want to apply to the image.

Let's assume that the machine name of the style we want to apply is thumbnail.

Here is how you get the thumbnail style object.

$style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');

Finally, we get the url of the styled image like so.

$img_url = $style->buildUrl($img_uri);

How to get the render array of the styled image

Alternatively, you can get the render array of the styled image like this.

$render = [
    '#theme' => 'image_style',
    '#style_name' => 'thumbnail',
    '#uri' => $img_uri,
];
Tags