You are here

public function ImageWidgetCropManager::getCropOriginalDimension in Image Widget Crop 8

Same name and namespace in other branches
  1. 8.2 src/ImageWidgetCropManager.php \Drupal\image_widget_crop\ImageWidgetCropManager::getCropOriginalDimension()

Get the size and position of the crop.

Parameters

array $field_values: The original values of image.

array $properties: The original height of image.

Return value

null|array The data dimensions (width & height) into this ImageStyle.

2 calls to ImageWidgetCropManager::getCropOriginalDimension()
ImageWidgetCropManager::applyCrop in src/ImageWidgetCropManager.php
Create new crop entity with user properties.
ImageWidgetCropManager::updateCrop in src/ImageWidgetCropManager.php
Update old crop with new properties choose in UI.

File

src/ImageWidgetCropManager.php, line 220

Class

ImageWidgetCropManager
ImageWidgetCropManager calculation class.

Namespace

Drupal\image_widget_crop

Code

public function getCropOriginalDimension(array $field_values, array $properties) {
  $crop_coordinates = [];

  /** @var \Drupal\Core\Image\Image $image */
  $image = \Drupal::service('image.factory')
    ->get($field_values['file-uri']);
  if (!$image
    ->isValid()) {
    drupal_set_message(t('The file "@file" is not valid, your crop is not applied.', [
      '@file' => $field_values['file-uri'],
    ]), 'error');
    return NULL;
  }

  // Get Center coordinate of crop zone on original image.
  $axis_coordinate = $this
    ->getAxisCoordinates([
    'x' => $properties['x'],
    'y' => $properties['y'],
  ], [
    'width' => $properties['width'],
    'height' => $properties['height'],
  ]);

  // Calculate coordinates (position & sizes) of crop zone on original image.
  $crop_coordinates['width'] = $properties['width'];
  $crop_coordinates['height'] = $properties['height'];
  $crop_coordinates['x'] = $axis_coordinate['x'];
  $crop_coordinates['y'] = $axis_coordinate['y'];
  return $crop_coordinates;
}