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?