1

I've done a drupal migration from drupal 6 to drupal 8. While all files are migrated well, image styles are not applying on migrated images.

Image styles are applied to the newly added images.

I think I should have force drupal to generate image styles for migrated images, but I don't know how.

any help one that is appreciated.

PS. I read some information about Flushing image styles in drupal 7 but I couldn't find a proper way to use it in drupal 8 as a custom module.

  • 1
    The drush command for this is drushcommands.com/drush-8x/core/image-flush – Kevin Dec 19 '17 at 20:47
  • @Kevin Thanks for help, actually my shared hosting only supports drush 8.1.4 which is not compatible with drupal 8.4.3 so I cant use drush commands to solve my problem – Alireza Tabatabaeian Dec 22 '17 at 10:03
  • and I don't think that flushing the images would have helped. All that does is delete the already generated images for that style so that new ones can be made, but no new ones were being made in the first place – Dan Mantyla Feb 20 at 20:05
1

To solve the problem I used the code below to force Drupal to create the image styles for my nodes:

if ($entity->hasField('field_image') && $entity->get('field_image')->getValue() != null) {
  $image = \Drupal::service('image.factory')->get($entity->get('field_image')->entity->getFileUri());
  if ($image->isValid()) {
    $styles = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();
    $image_uri = $entity->get('field_image')->entity->getFileUri();
    foreach ($styles as $style) {
      $destination = $style->buildUri($image_uri);
      $style->createDerivative($image_uri, $destination);
    }
  }
}

Here is the description, for each node ($entity variable) , first it checks if the node type has a field with machine name "field_image" and if this field has a value, then it checks if fileUri is a valid image, then if all conditions met, the code iterates on all image styles available and build that style for given image manually.

This code can be used as a cron job function, so on every cron job image styles of given nodes are created.

  • where did you put the code? In a custom module? In a stand alone php file that loads drupal first? – Dan Mantyla Feb 20 at 19:42
  • 1
    Actually I added a new custom module, there I used a batch process to repair all image styles of migrated nodes – Alireza Tabatabaeian Feb 23 at 9:48

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.