You are here

protected function GDOperationTrait::getEntropyCropBySlicing in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::getEntropyCropBySlicing()
  2. 8 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::getEntropyCropBySlicing()

Computes the entropy crop of an image, using slices.

Parameters

resource $src: The source image resource.

string $width: The width of the crop.

string $height: The height of the crop.

Return value

\Drupal\image_effects\Component\PositionedRectangle The PositionedRectangle object marking the crop area.

2 calls to GDOperationTrait::getEntropyCropBySlicing()
SmartCrop::execute in src/Plugin/ImageToolkit/Operation/imagemagick/SmartCrop.php
SmartCrop::execute in src/Plugin/ImageToolkit/Operation/gd/SmartCrop.php
Performs the actual manipulation on the image.

File

src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php, line 410

Class

GDOperationTrait
Trait for GD image toolkit operations.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function getEntropyCropBySlicing($src, $width, $height) {
  $dx = imagesx($src) - min(imagesx($src), $width);
  $dy = imagesy($src) - min(imagesy($src), $height);
  $left = $top = 0;
  $left_entropy = $right_entropy = $top_entropy = $bottom_entropy = 0;
  $right = imagesx($src);
  $bottom = imagesy($src);

  // Slice from left and right edges until the correct width is reached.
  while ($dx) {
    $slice = min($dx, 10);

    // Calculate the entropy of the new slice.
    if (!$left_entropy) {
      $left_entropy = $this
        ->getAreaEntropy($src, $left, $top, $slice, imagesy($src));
    }
    if (!$right_entropy) {
      $right_entropy = $this
        ->getAreaEntropy($src, $right - $slice, $top, $slice, imagesy($src));
    }

    // Remove the lowest entropy slice.
    if ($left_entropy >= $right_entropy) {
      $right -= $slice;
      $right_entropy = 0;
    }
    else {
      $left += $slice;
      $left_entropy = 0;
    }
    $dx -= $slice;
  }

  // Slice from the top and bottom edges until the correct width is reached.
  while ($dy) {
    $slice = min($dy, 10);

    // Calculate the entropy of the new slice.
    if (!$top_entropy) {
      $top_entropy = $this
        ->getAreaEntropy($src, $left, $top, $width, $slice);
    }
    if (!$bottom_entropy) {
      $bottom_entropy = $this
        ->getAreaEntropy($src, $left, $bottom - $slice, $width, $slice);
    }

    // Remove the lowest entropy slice.
    if ($top_entropy >= $bottom_entropy) {
      $bottom -= $slice;
      $bottom_entropy = 0;
    }
    else {
      $top += $slice;
      $top_entropy = 0;
    }
    $dy -= $slice;
  }
  $rect = new PositionedRectangle($right - $left, $bottom - $top);
  $rect
    ->translate([
    $left,
    $top,
  ]);
  return $rect;
}