5

I want to be able to send an image included in a custom module to the image_style theme like:

  $render_image = [
    '#theme' => 'image_style',
    '#style_name' => $imagecache_template,
    '#uri' => '/modules/custom/MYMODULE/default.png',
    ...
  ];

But '#uri' is expecting something in the format public:// . Is it possible to marshall a relative path somehow?

  • It should be possible to create an image style derivative using an ImageStyle entity or the image.factory service, but I haven't tried it myself. – mradcliffe Jun 7 '16 at 13:11
1

I'll expand a bit on what acrosman answered, only because I needed this exact same thing so hopefully this will help the next person who comes across this.

I'm working from preprocess_page and using a view to grab my images.

Like his answer make sure you add the ImageStyle:

use Drupal\image\Entity\ImageStyle;

Once I grabbed the view entity I was able to narrow down my image field and then work my way to the styled image.

$image = $entity->get('field_images')->getValue();
$image_entity = File::load($image[0]['target_id']);
$image_uri = $image_entity->get('uri')->value;
$style = ImageStyle::load('my_image_style');
$image_url = $style->buildUrl($image_uri);

So overall my whole function to grab the image from a View and apply a custom image style looked like.

function _mytheme_preprocess_page_front_projects(array &$variables) {
  $projects = array();
  $view = Views::getView('projects');

  if (is_object($view)) {
    $view->setDisplay('page_1');
    $view->preExecute();
    $view->execute();

    foreach ($view->result as $k => $v) {
      $entity = $v->_entity;

      // Grab image entity.
      $image = $entity->get('field_images')->getValue();

      // Load the first value for preview image.
      $image_entity = File::load($image[0]['target_id']);

      // Grab the uri of that first image.
      $image_uri = $image_entity->get('uri')->value;

      // Using ImageStyle you then load your style.
      $style = ImageStyle::load('my_image_style');

      // Use buildUrl to create the path to your styled image.
      $image_url = $style->buildUrl($image_uri);

      // Then I return the url as an image variable to grab in page template.
      $projects[$k]['image'] = $image_url;
    }

    return $projects;
  }
}
  • this works well, don't forget to include use Drupal\file\Entity\File; before File::load – bdanin May 4 '18 at 23:14
0

I haven't tried it from a render array, but you can load the ImageStyle entity and have it build a URL from a path using buildUrl().

If you are doing this right in the .module file make sure to tell PHP you'll be using ImageStyle, in a plugin or controller you'll want to inject the class.

use Drupal\image\Entity\ImageStyle;

Then load you're style and build the URL (warning, I have not tested this):

$style = ImageStyle::load('img_style');
$image_path = $style->buildUrl('/modules/custom/MYMODULE/default.png');

From there you should be able to place that into whatever context you need to get the image rendered the way you'd like.

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.