You are here

function template_preprocess_insert_image in Insert 8.2

Same name and namespace in other branches
  1. 8 insert.module \template_preprocess_insert_image()
  2. 6 includes/insert.inc \template_preprocess_insert_image()
  3. 7 includes/insert.inc \template_preprocess_insert_image()

Preprocess variables for the insert-image.html.twig file. The function is called for each image style configured to be used. The particular image is transformed according to the image style.

File

./insert.module, line 949

Code

function template_preprocess_insert_image(array &$vars) {

  /** @var \Drupal\Core\Image\ImageInterface $image */
  $image = \Drupal::service('image.factory')
    ->get($vars['file']
    ->getFileUri());
  if ($image
    ->isValid()) {
    if (!empty($vars['insert_settings']['width']) && $image
      ->getWidth() > $vars['insert_settings']['width']) {
      $vars['width'] = $vars['insert_settings']['width'];
      $vars['height'] = intval(round($vars['insert_settings']['width'] * $image
        ->getHeight() / $image
        ->getWidth()));
    }
    else {
      $vars['width'] = $image
        ->getWidth();
      $vars['height'] = $image
        ->getHeight();
    }
  }
  else {
    $vars['width'] = $variables['height'] = NULL;
  }
  if ($vars['style_name'] === 'image' && !isset($vars['insert__auto'])) {

    // Preprocessing template for inserting original image.
    return;
  }

  /** @var \Drupal\image\Entity\ImageStyle $style */
  $style = ImageStyle::load(isset($vars['insert__auto']) ? $vars['insert__auto'] : $vars['style_name']);
  if ($style === NULL) {
    return;
  }
  $style
    ->transformDimensions($vars, $vars['file']
    ->getFileUri());
}