Example #1
0
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = $this->container->get('entity_type.manager');
    $this->cropStorage = $entity_type_manager->getStorage('crop');
    $this->cropTypeStorage = $entity_type_manager->getStorage('crop_type');
    $this->imageStyleStorage = $entity_type_manager->getStorage('image_style');
    $this->fileStorage = $entity_type_manager->getStorage('file');

    // Create DB schemas.
    /** @var \Drupal\Core\Entity\EntityTypeListenerInterface $entity_type_listener */
    $entity_type_listener = $this->container->get('entity_type.listener');
    $entity_type_listener->onEntityTypeCreate($entity_type_manager->getDefinition('user'));
    $entity_type_listener->onEntityTypeCreate($entity_type_manager->getDefinition('image_style'));
    $entity_type_listener->onEntityTypeCreate($entity_type_manager->getDefinition('crop'));
    $entity_type_listener->onEntityTypeCreate($entity_type_manager->getDefinition('file'));

    // Create test image style.
    $uuid = $this->container->get('uuid')->generate();
    $this->testStyle = $this->imageStyleStorage->create([
      'name' => 'test',
      'label' => 'Test image style',
      'effects' => [
        $uuid => [
          'id' => 'crop_crop',
          'data' => ['crop_type' => 'test_type'],
          'weight' => 0,
          'uuid' => $uuid,
        ]
      ],
    ]);
    $this->testStyle->save();

    // Create test crop type.
    $this->cropType = $this->cropTypeStorage->create([
      'id' => 'test_type',
      'label' => 'Test crop type',
      'description' => 'Some nice desc.',
    ]);
    $this->cropType->save();
  }
Example #2
0
  /**
   * {@inheritdoc}
   */
  public function saveCropEntity($x, $y, $width, $height, CropInterface $crop) {
    $absolute = $this->relativeToAbsolute($x, $y, $width, $height);

    $anchor = $crop->anchor();
    if ($anchor['x'] != $absolute['x'] || $anchor['y'] != $absolute['y']) {
      $crop->setPosition($absolute['x'], $absolute['y']);
      $crop->save();
    }

    return $crop;
  }
  /**
   * Returns the focal point value (in pixels) relative to the original image.
   *
   * @param \Drupal\crop\CropInterface $crop
   *   The crop object used to define the crop.
   * @param \Drupal\focal_point\FocalPointManager $focal_point_manager
   *   The focal point manager.
   *
   * @return array
   *   An array with the keys 'x' and 'y'. Values are in pixels.
   */
  protected function getOriginalFocalPoint(CropInterface $crop, FocalPointManager $focal_point_manager) {
    $focal_point = $crop->position();

    // Check if we are generating a preview image. If so get the focal point
    // from the query parameter, otherwise use the crop position.
    $preview_value = $this->getPreviewValue();
    if (!is_null($preview_value)) {
      // @todo: should we check that preview_value is valid here? If it's invalid it gets converted to 0,0.
      $original_image_size = $this->getOriginalImageSize();
      list($x, $y) = explode('x', $preview_value);
      $focal_point = $focal_point_manager->relativeToAbsolute($x, $y, $original_image_size['width'], $original_image_size['height']);
    }

    return $focal_point;
  }