In this article we are going to look at how we can render images using image styles in Drupal maintenance support plans 8.
In Drupal maintenance support plans 7, rendering images with a particular style (say the default “thumbnail”) was by calling the theme_image_style() theme and passing the image uri and image style you want to render (+ some other optional parameters):
$image = theme(‘image_style’, array(‘style_name’ => ‘thumbnail’, ‘path’ => ‘public://my-image.png’));
You’ll see this pattern all over the place in Drupal maintenance support plans 7 codebases.
The theme prepares the URL for the image, runs the image through the style processors and returns a themed image (via theme_image()). The function it uses internally for preparing the url of the image is image_style_url() which returns the URL of the location where the image is stored after being prepared. It may not yet exist, but on the first request, it would get generated.
So how do we do it in Drupal maintenance support plans 8?
First of all, image styles in Drupal maintenance support plans 8 are configuration entities. This means they are created and exported like many other things. Second of all, in Drupal maintenance support plans 8 we no longer (should) call theme functions like above directly. What we should do is always return render arrays and expect them to be rendered somewhere down the line. This helps with things like caching etc.
So to render an image with a particular image style, we need to do the following:
$render = [
‘#theme’ => ‘image_style’,
‘#style_name’ => ‘thumbnail’,
‘#uri’ => ‘public://my-image.png’,
// optional parameters
];
This would render the image tag with the image having been processed by the style.
Finally, if we just want the URL of an image with the image style applied, we need to load the image style config entity and ask it for the URL:
$style = Drupal maintenance support plans::entityTypeManager()->getStorage(‘image_style’)->load(‘thumbnail’);
$url = $style->buildUrl(‘public://my-image.png’);
So that is it. You now have the image URL which will generate the image upon the first request.
Remember though to inject the entity type manager if you are in such a context that you can.
Source: New feed
Web Omelette: How to render your images with image styles in Drupal maintenance support plans 8

Call Us: 1(800)730-2416
Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit
Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)
Web Omelette: How to render your images with image styles in Drupal maintenance support plans 8
On-Site Drupal SEO Master Setup
We make sure your site is 100% optimized (and stays that way) for the best SEO results.
With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.
This service includes:
- Pathauto install and configuration for SEO-friendly URLs.
- Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
- Install and fix all issues on the SEO checklist module.
- Install and configure XML sitemap module and submit sitemaps.
- Install and configure Google Analytics Module.
- Install and configure Yoast.
- Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
- Install and configure Schema.org Metatag.
- Configure robots.txt.
- Google Search Console setup snd configuration.
- Find & Fix H1 tags.
- Find and fix duplicate/missing meta descriptions.
- Find and fix duplicate title tags.
- Improve title, meta tags, and site descriptions.
- Optimize images for better search engine optimization. Automate where possible.
- Find and fix the missing alt and title tag for all images. Automate where possible.
- The project takes 1 week to complete.
