Programmatically Create & Delete Image Styles in Drupal

I recently needed to delete a image style that was no longer being used with a update handler. Being that Drupal’s documentation is less than informative, I did some digging and finally figured it out. Check out how to create, flush and delete image styles below.

Programmatically Create an Image Style

The code below will create an image style named, avatar that will scale images to 64×64:

<?php
$style = image_style_save(array('name' => 'avatar'));
$effect = array(
  'name' => 'image_scale',
  'data' => array(
    'width' => 64,
    'height' => 64,
    'upscale' => TRUE,
  ),
  'isid' => $style['isid'],
);
image_effect_save($effect);

Or, you can create image styles with a Drupal hook using hook_image_default_styles:

<?php
function hook_image_default_styles() {
  $styles = array();
  $styles['avatar'] = array(
    'effects' => array(
      array(
        'name' => 'image_scale',
        'data' => array(
          'width' => 200,
          'upscale' => 1,
        ),
        'weight' => 0,
      ),
      array(
        'name' => 'image_crop',
        'data' => array(
          'width' => 200,
          'height' => 200,
        ),
        'weight' => 1,
      ),
    ),
  );

  return $styles;
}

Programmatically Delete an Image Style

<?php
image_style_delete(image_style_load('my_image_style'));

Don’t forget to flush…

You may need to flush the image style before Drupal will let you delete it. You can flush image styles using two different methods:

Flush Image Styles with Drupal Console

# List image styles on the site
$ drupal image:styles:debug

# Execute flush function by image style or execute all flush images styles
$ drupal image:styles:flush

For a list of other useful Drupal 8 Console commands, see benmarshall.me/drupal-8-console-commands/.

Flush image styles with Drush

$ drush image-flush

This will create a list of options with all of your defined image styles. For more information run:

$ drush help image-flush

Programmatically flush image styles

<?php
image_style_flush('STYLE_NAME');

Or, if you want to flush all image styles:

<?php
foreach (image_styles() as $style) {
  image_style_flush($style);
}

More on Drupal Image Styles

Comments

  1. To flush all image_style in D8

    $imageStyle = \Drupal::entityTypeManager()->getStorage(‘image_style’);
    $styles = $imageStyle->loadMultiple();
    foreach ($styles as $style) {
    $imageStyle->load($style->get(‘name’))->flush();
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All comments are held for moderation and only published when on topic and not rude. You'll even get little stars if you do an extra good job.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.

icon-search icon-link