Score:-1

Programmatically create a new image style

us flag

I'm creating my own module and I need to programmatically create an image style to use with a new media type.

I found the core/profile/standard/config/optional/image.style.max_1300x1300.yml file whose content is the following one.

name: max_1300x1300
label: 'Max 1300x1300'
effects:
  04caae9a-fa3e-4ea6-ae09-9c26aec7d308:
    id: image_scale
    data:
      width: 1300
      height: 1300
      upscale: false
    weight: 1
    uuid: 04caae9a-fa3e-4ea6-ae09-9c26aec7d308
langcode: en
dependencies:
  module:
    - responsive_image
  enforced:
    module:
      - responsive_image

I take I need to create a file in config/install, named image.style.<image_style_id>.yml, with those attributes.

What UUID values should I use?

Kevin avatar
in flag
It's in the admin under configuration.
us flag
@Kevin thanks but not my question, I don't want to use the UI
Kevin avatar
in flag
Writing out the yaml by hand is pointless. Make them in the UI and export them.
Score:0
in flag

In answering this question, I assume that you have generated your configuration via UI, and then exported it, as Kevin mentioned in his comment. This is the best way to create the initial config for inclusion in your project.

Note that all examples are shown using procedural code for simplicity's sake; be sure to follow best practices and inject your services when building your project.


ImageStyle entity

Once that configuration file is installed, it becomes an ImageStyle configuration entity, and can be manipulated like any configuration entity.

// Get the storag ehandler for ImageStyle entities.
$image_style_storage = \Drupal::entityTypeManager()->getStorage("image_style");
// Get an instance of our ImageStyle entity.
$my_image_style = $image_style_storage->load("my_image_style");

The effects that are specified in the ImageStyle definition are plugins managed by the plugin.manager.image.effect plugin manager service.

As luck would have it, the ImageStyle entity offers the addImageEffect() public method, which accepts an array of effects info. This method actually takes care of generating and assigning the UUID, which is then returned to you.

public function addImageEffect(array $configuration) {
    $configuration['uuid'] = $this->uuidGenerator()->generate();
    $this->getEffects()->addInstanceId($configuration['uuid'], $configuration);
    return $configuration['uuid'];
  }

So, using your image.style.max_1300x1300 example, assigning the image effect would look like:

$effect_uuid = $my_image_style->addImageEffect([
  'id' => 'image_scale',
  'data' => [
      'width' => '1300',
      'height' => '1300',
      'upscale' => FALSE,
   ],
   'weight' => 1,
]);

You can find all of the image effects registered on your site by executing: \Drupal::service('plugin.manager.image.effect')->getDefinitions();

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.