0
votes

I wrote a custom template with a customtemplate_preprocess_image function.

In this function I have only $variables['uri'] at my disposal to load the same image but with a different image style. The uri is of this format: '/sites/default/files/styles/image_xs/public/2019-02/IMG_20181219_135712499_0.jpg?itok=1faUCioy'

If I now do this

$style = ImageStyle::load('image_lightbox');
$image = \Drupal::service('image.factory')->get($variables['uri']);
if ($image->isValid()) {
    print("Image url: ".$variables['uri'].'<br>');
}
else {
    print("Image invalid: ".$variables['uri'].'<br>');
}

It renders "Image invalid: /sites/default/files/styles/image_xs/public/2019-02/IMG_20181219_135712499_0.jpg?itok=1faUCioy

Why? And how can I solve this having only this uri?

1
Can you get the image target_id instead? And then with that, use the getSource so that you're starting with the base image and not a derived image. See api.drupal.org/api/drupal/…stacey.mosier

1 Answers

0
votes

You can use file uri in twig file on drupal 8.

{{ file_url(node.field_image.entity.fileuri) }}