1

I'm using Drupal 8. Is there a hook for generating image from image style or better one module?

For example. I upload an image. When image is displayed for the first time, Drupal generates image style. Can I hook to that event somehow, at best when uploading the image?

1
  • I upload the image when i create one content with upload function. No derivatives are being created by default, only the default image in full size is available on filesystem. As i have a decoupled installation i need to have the image derivates available when image is added to node. Nothing specially in my installation, i can reproduce this with latest drupal 8 version fresh installed. – John Oct 22 '19 at 14:06
1

Ah yes, you are right.

Image derivatives will be automatically generated once requested and not-yet existing.

That's a quote from my own answer to a similar question. I guess you could try to hook into the upload somehow like Image Style Warmer does it, or simply try that module out yourself.

Synopsis

The Image Style Warmer module provides options to create image styles during upload or via queue worker. So configured image derivates already exists when they are requested.

0

You don't need to write code to generate image styles in Drupal.

This functionality is provided by the Image module in Drupal core.

From the documentation:

To create or change image styles:

  1. Choose Administration > Configuration > Media > Image styles.
  2. Add or select a style to edit its settings.
  3. When configuring styles you can add effects: crop, scale, resize, rotate, and desaturate (other contributed modules provide additional effects). For example, by combining effects as crop, scale, and desaturate, you can create square, grayscale thumbnails.
  4. Save the settings.

To start using the defined styles they must be assigned to the display of a content type.

For each display (e.g. "default" or "teaser"), a different style can chosen.

To assign a style:

  1. Choose Administer > Structure > Content types > Manage display.
  2. Choose the display (e.g. "default" or "teaser") via the tab buttons.
  3. Click the gear wheel button to choose an image style; you are also able to link the image to the node.
  4. Save the settings.
  5. Check the content that contains the image style in question to see your new styles at work.

By design, properly configured image styles should be generated when the image is first uploaded, not when it is displayed for the first time. (NOTE: I was wrong about this. See @leymannx comments below and answer above!)

Additionally, whenever you change an image style configuration, the module automatically refreshes all created images by flushing the imagecache.

Also from the documentation:

The Drupal 6 imagecache module was introduced into the Drupal 7 core. Since then, the option to 'flush' the image presets has changed. In the Drupal 7 UI, this 'Flush' image styles preset option was removed, but now, just resaving your image style will flush the image cache.

If you need to flush all image styles (eg. after adjusting the JPEG quality), you can execute drush image-flush, use the Image style flush module, or run the following code (for instance using Devel):

// Flush all image styles in Drupal 7.
foreach (image_styles() as $style) {
  image_style_flush($style);
}
5
  • I want to have the image style being generated when i add the image, not when it get loaded first time (the node). The documentation applies only for this case, but not how to create image styles at content creation (uploading image). – John Oct 19 '19 at 9:34
  • 1
    I added a note describing how to regenerate image styles with code. Perhaps you can edit your question to explain what you are trying to accomplish. – hotwebmatter Oct 19 '19 at 11:29
  • @John I updated my answer again to indicate that the desired behavior (image styles generated on upload, not when the node is first viewed) is already the default in Drupal. If you are certain that your situation is different, please provide more details. Otherwise, be sure to accept my answer by clicking the green check mark, to prevent it from beiing forever marked as "unanswered" and cluttering up the forum. – hotwebmatter Oct 20 '19 at 3:34
  • This is exactly which i can´t confirm. With default new drupal 8 installation with latest version it don´t create image styles when node is created, but only when it is viewed. I use a decoupled installation where i never open open any nodes via HTTP. When image is uploaded to the node nothing is being generated on the filesystem except the initial image but not any image styles which i have created. – John Oct 22 '19 at 9:43
  • 1
    Sorry @hotwebmatter, I maybe have set a red herring in my original comment under the question. Image derivatives are really only being created when requested, not during upload. As this would result in a lot of unused files otherwise (imagine your site as 100 image styles, but this one image will only be displayed in one style, would be 99 unused files then). – leymannx Oct 22 '19 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.