Create Image Styles and Effects programmatically

Parent Feed: 

In this short article we will learn how to create image styles and how to add image effects programmatically.

Creating image styles

Image styles are stored as configuration entities in Drupal 8. So to create a new image style we simply need to create a ImageStyle instance and save it.


use Drupal\image\Entity\ImageStyle;

$imageStyle = ImageStyle::create([
  'name' => 'machine_name',
  'label' => 'Label',

Add image effects

We will use the ImageStyle::addImageEffect method to do this.

  'id' => 'image_scale_and_crop',
  'weight' => 0,
  'data' => [
    'width' => 500,
    'height' => 500,

Each image effect is a plugin of the ImageEffect plugin type. You can find the core's image effect plugins under the Drupal\image\Plugin\ImageEffect namespace in the image module.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web