diff --git a/src/Element/ImageCrop.php b/src/Element/ImageCrop.php index 9a3e346..8694626 100644 --- a/src/Element/ImageCrop.php +++ b/src/Element/ImageCrop.php @@ -207,14 +207,39 @@ class ImageCrop extends FormElement { $properties = []; $form_state_values = $form_state->getValue($element['#parents']); // Check if form state has values. - if (self::hasCropValues($element, $type, $form_state)) { - $form_state_properties = $form_state_values['crop_wrapper'][$type]['crop_container']['values']; + if ($form_state_values) { + $form_state_props = $form_state_values['crop_wrapper'][$type]['crop_container']['values']; // If crop is applied by the form state we keep it that way. - if ($form_state_properties['crop_applied'] == '1') { + if ($form_state_props['crop_applied'] == '1') { $element['crop_wrapper'][$type]['crop_container']['values']['crop_applied']['#default_value'] = 1; $edit = TRUE; + $properties = $form_state_props; + } + else { + list($width_ratio, $height_ratio) = explode(':', $crop_type->aspect_ratio); + + // Define properties according to dimensions and ratio. + $height = $image->getHeight(); + $width = $image->getWidth(); + + // If it's a wide crop then we use the image width. + if ($width / $width_ratio > $height / $height_ratio) { + $properties['height'] = $height; + $properties['width'] = $height * $width_ratio / $height_ratio; + $properties['x'] = ($width - $properties['width']) / 2; + $properties['y'] = 0; + } + else { + $properties['width'] = $width; + $properties['height'] = $width * $height_ratio / $width_ratio; + $properties['x'] = 0; + $properties['y'] = ($height - $properties['height']) / 2; + } + + $properties['crop_applied'] = 1; + $element['crop_wrapper'][$type]['crop_container']['values']['crop_applied']['#value'] = 1; + $edit = TRUE; } - $properties = $form_state_properties; } /** @var \Drupal\crop\CropInterface $crop */