You are here

public static function ImageCropWidget::getFileImageVariables in Image Widget Crop 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/ImageCropWidget.php \Drupal\image_widget_crop\Plugin\Field\FieldWidget\ImageCropWidget::getFileImageVariables()

Verify if the element have an image file.

Parameters

array $element: A form element array containing basic properties for the widget.

array $variables: An array with all existent variables for render.

Return value

array<string,array> The variables with width & height image informations.

File

src/Plugin/Field/FieldWidget/ImageCropWidget.php, line 147

Class

ImageCropWidget
Plugin implementation of the 'image_widget_crop' widget.

Namespace

Drupal\image_widget_crop\Plugin\Field\FieldWidget

Code

public static function getFileImageVariables(array $element, array &$variables) {

  // Determine image dimensions.
  if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
    $variables['width'] = $element['#value']['width'];
    $variables['height'] = $element['#value']['height'];
  }
  else {

    /** @var \Drupal\Core\Image\Image $image */
    $image = \Drupal::service('image.factory')
      ->get($variables['uri']);
    if ($image
      ->isValid()) {
      $variables['width'] = $image
        ->getWidth();
      $variables['height'] = $image
        ->getHeight();
    }
    else {
      $variables['width'] = $variables['height'] = NULL;
    }
  }
  return $variables;
}