2

There are plenty of examples on the internet on how to render a styled image; for instance this one from web omelette:

$render = [
    '#theme' => 'image_style',
    '#style_name' => 'thumbnail',
    '#uri' => 'public://my-image.png',
    // optional parameters
];

However I do not want to use an image style. I want to render the original untouched image. So I tried:

$render = [
    '#theme' => 'image_style',
    '#style_name' => NULL,
    '#uri' => 'public://my-image.png',
    // optional parameters
];

Which did not work. How do I render the original full-size image?

4

You can also use the #theme property image (which does not appear to be documented anywhere), but gives a valid image tag.

$render= [
  '#theme' => 'image',
  '#uri' => $file->getFileUri(),
];

Which becomes <img src="/sites/default/files/image.png" alt="" /> once rendered.

  • 1
    For reference: drupal_common_theme() – Clive Jan 16 '18 at 22:25
  • Ah great, I was already confused why it was not in image_theme(). Thanks :) – Neograph734 Jan 16 '18 at 22:51
  • You can also use Drupal function realpath for it. $render= [ '#theme' => 'image', '#uri' => \Drupal::service('file_system')->realpath('public://my-image.png'), ]; – Chirag Patel Jan 17 '18 at 6:16
  • Solution works, but any solution for the markup with height width and other attributes? – Vishal Kumar Sahu Jan 26 at 22:10
  • @VishalKumarSahu follow the link Clive posted above and find the supported variables for image. You can use #height.and more. – Neograph734 Jan 26 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.