/** * {@inheritDocs} * * Validate callback for the focal point field. */ public static function validateFocalPoint($element, FormStateInterface $form_state) { $field_name = array_pop($element['#parents']); $focal_point_value = $form_state->getValue($field_name); if (!is_null($focal_point_value) && !FocalPoint::validate($focal_point_value)) { \Drupal::formBuilder()->setError($element, $form_state, t('The !title field should be in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75.', array('!title' => $element['#title']))); } }
/** * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { // Next, attempt to crop the image. $focal_point = FocalPoint::getFromURI($image->getSource()); $crop_data = self::calculateCropData($focal_point, $image->getWidth(), $image->getHeight(), $this->configuration['width'], $this->configuration['height']); if (!$image->crop($crop_data['x'], $crop_data['y'], $crop_data['width'], $crop_data['height'])) { $this->logger->error('Focal point scale and crop failed while scaling and cropping using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; } return TRUE; }
/** * Compile the necessary data for the image crop effect. * * @param string $focal_point * @param int $image_width * @param int $image_height * @param int $crop_width * @param int $crop_height * * @return array|bool * An array containing the following keys: * - width * - height * - x * - y */ public static function calculateCropData($focal_point, $image_width, $image_height, $crop_width, $crop_height) { $crop_data = array(); $parsed_focal_point = FocalPoint::parse($focal_point); // Get the pixel location of the focal point for the current image taking // the image boundaries into account. $crop_data['width'] = (int) $crop_width; $crop_data['height'] = (int) $crop_height; $crop_data['x'] = self::calculateAnchor($image_width, $crop_width, $parsed_focal_point['x-offset']); $crop_data['y'] = self::calculateAnchor($image_height, $crop_height, $parsed_focal_point['y-offset']); return $crop_data; }
/** * Tests the validate() method. * * @dataProvider providerValidateFocalPoint */ public function testFocalPointValidate($focal_point, $expected) { $this->assertSame($expected, FocalPoint::validate($focal_point)); }