How to Programmatically Load Image using Image Style in Drupal 8
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY akhila.vn
8 months ago
Drupal-8
0
comments comment

In Drupal 8 we can load an image using image style in a custom function using the file id of the image. For this first, we have to create an image style by configuration or create a config file for the same (Example below: image.style.sample_image_style.yml). Note that we can generate the below file after creating an image style and export the same from config synchronization in Drupal 8.


uuid: 4fcc87d8-09cd-40c7-96c7-af51690757ea
langcode: en
status: true
dependencies:
  module:
    - image_effects
name: sample_image_style
label: 'Sample Image Style'
effects:
  439fd8f0-4d9a-4450-a3e7-af51690757ea:
    uuid: 439fd8f0-4d9a-4450-a3e7-af51690757ea
    id: image_effects_set_canvas
    weight: 1
    data:
      canvas_size: relative
      exact:
        width: '160'
        height: '160'
        placement: center-center
        x_offset: 0
        y_offset: 0
      relative:
        left: 0
        right: 0
        top: 0
        bottom: 0
      canvas_color: '#646464FF'

Now we can use this image style to load the image using its file id by passing to a function.

Don't forget to use the controller of the image style along with your function


  use Drupal\image\Entity\ImageStyle;
  ...................................
  ....................................
  public function MyFunction($image_id) {
    global $base_url;
    $style = ImageStyle::load('sample_image_style');
    $images = array();
    if (!empty($image_id)) {
      $photo_file = file_load($image_id);
      $image_url = $style->buildUrl($photo_file->uri->value);
    }
    return $image_url;
  }

The variable $image_url will generate the url of the image from the image style folder. The image style should be loaded using the machine name of the image style. Hope this code snippet is useful. Happy coding!


RELATED ARTICLE

more_horiz
close

on 21st May 2013 / by Anoop John
Drupal 6 will be maintained till the release of Drupal 8. Drupal 8 release is expected to happen towards the end of 2013. The current stable branch Drupal 7 will be maintained till the release of Drupal 9 (probably 2015). If you are running a production site on Drupal 6 then you will probably have to start looking at upgrading the site by the end of this year. Drupal Drupal 6 Drupal Upgrade Drupal 7 Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 26th December 2014 / by abhai.sasidharan
YAML is a data serialization language which is easily readable to both humans and computers. It is for this very reason that YAML is being used in Drupal 8. YAML stands for YAML Ain't Markup Language. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 29th December 2014 / by abhai.sasidharan
Drupal, up until now a PAC (Presentation Abstraction Control) architecture, has taken a step closer to MVC (Model View Controller) architecture. Although nowhere have they mentioned about MVC, the new Drupal 8 is strictly object oriented. Another feature in Drupal 8, that is different from the existing versions of Drupal is that it is not entirely Drupal. Drupal 8 uses many components in symfony like it's ClassLoader, YAML file structure, twig and so on. For a detailed description please visit the Drupal 8 home page. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.