You are here

protected function AutoOrient::execute in Image Effects 8

Same name in this branch
  1. 8 src/Plugin/ImageToolkit/Operation/gd/AutoOrient.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\AutoOrient::execute()
  2. 8 src/Plugin/ImageToolkit/Operation/imagemagick/AutoOrient.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\AutoOrient::execute()
Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/AutoOrient.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\AutoOrient::execute()
  2. 8.2 src/Plugin/ImageToolkit/Operation/gd/AutoOrient.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\AutoOrient::execute()

Performs the actual manipulation on the image.

Image toolkit operation implementers must implement this method. This method is responsible for actually performing the operation on the image. When this method gets called, the implementer may assume all arguments, also the optional ones, to be available, validated and corrected.

Parameters

array $arguments: An associative array of arguments to be used by the toolkit operation.

Return value

bool TRUE if the operation was performed successfully, FALSE otherwise.

Overrides ImageToolkitOperationBase::execute

File

src/Plugin/ImageToolkit/Operation/gd/AutoOrient.php, line 31

Class

AutoOrient
Defines GD AutoOrient operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments) {

  // If image has been created in memory, this will not apply.
  if (!($source_path = $this
    ->getToolkit()
    ->getSource())) {
    return TRUE;
  }

  // Will not work without EXIF extension installed.
  if (!function_exists('exif_read_data')) {
    $this->logger
      ->notice('The image %file could not be auto-rotated because the exif_read_data() function is not available in this PHP installation. Check if the PHP EXIF extension is enabled.', [
      '%file' => $this
        ->getToolkit()
        ->getSource(),
    ]);
    return FALSE;
  }

  // Read EXIF data.
  $exif = @exif_read_data(\Drupal::service('file_system')
    ->realpath($source_path));
  if (isset($exif['Orientation'])) {

    // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html:
    // 1 = Horizontal (normal)                 [top-left].
    // 2 = Mirror horizontal                   [top-right].
    // 3 = Rotate 180                          [bottom-right].
    // 4 = Mirror vertical                     [bottom-left].
    // 5 = Mirror horizontal and rotate 270 CW [left-top].
    // 6 = Rotate 90 CW                        [right-top].
    // 7 = Mirror horizontal and rotate 90 CW  [right-bottom].
    // 8 = Rotate 270 CW                       [left-bottom].
    switch ($exif['Orientation']) {
      case 2:
        return $this
          ->getToolkit()
          ->apply('mirror', [
          'x_axis' => TRUE,
        ]);
      case 3:
        return $this
          ->getToolkit()
          ->apply('rotate', [
          'degrees' => 180,
        ]);
      case 4:
        return $this
          ->getToolkit()
          ->apply('mirror', [
          'y_axis' => TRUE,
        ]);
      case 5:
        $tmp = $this
          ->getToolkit()
          ->apply('mirror', [
          'x_axis' => TRUE,
        ]);
        if ($tmp) {
          $tmp = $this
            ->getToolkit()
            ->apply('rotate', [
            'degrees' => 270,
          ]);
        }
        return $tmp;
      case 6:
        return $this
          ->getToolkit()
          ->apply('rotate', [
          'degrees' => 90,
        ]);
      case 7:
        $tmp = $this
          ->getToolkit()
          ->apply('mirror', [
          'x_axis' => TRUE,
        ]);
        if ($tmp) {
          $tmp = $this
            ->getToolkit()
            ->apply('rotate', [
            'degrees' => 90,
          ]);
        }
        return $tmp;
      case 8:
        return $this
          ->getToolkit()
          ->apply('rotate', [
          'degrees' => 270,
        ]);
      default:
        return TRUE;
    }
  }
}