You are here

public function ToolkitImagemagickTest::testManipulationsLegacy in ImageMagick 8.2

Legacy methods tests.

@dataProvider providerToolkitConfiguration

@group legacy

Parameters

string $toolkit_id: The id of the toolkit to set up.

string $toolkit_config: The config object of the toolkit to set up.

array $toolkit_settings: The settings of the toolkit to set up.

File

tests/src/Functional/ToolkitImagemagickTest.php, line 596

Class

ToolkitImagemagickTest
Tests that core image manipulations work properly through Imagemagick.

Namespace

Drupal\Tests\imagemagick\Functional

Code

public function testManipulationsLegacy($toolkit_id, $toolkit_config, array $toolkit_settings) {
  $this
    ->setUpToolkit($toolkit_id, $toolkit_config, $toolkit_settings);

  // Check package.
  $toolkit = \Drupal::service('image.toolkit.manager')
    ->createInstance('imagemagick');
  $this
    ->assertSame($toolkit_settings['binaries'], $toolkit
    ->getPackage());
  $this
    ->assertNotNull($toolkit
    ->getPackageLabel());
  $this
    ->assertSame([], $toolkit
    ->checkPath('')['errors']);

  // Typically the corner colors will be unchanged. These colors are in the
  // order of top-left, top-right, bottom-right, bottom-left.
  $default_corners = [
    $this->red,
    $this->green,
    $this->blue,
    $this->transparent,
  ];

  // A list of files that will be tested.
  $files = [
    'image-test.png',
    'image-test.gif',
    'image-test-no-transparency.gif',
    'image-test.jpg',
  ];

  // Setup a list of tests to perform on each type.
  $operations = [
    'resize' => [
      'function' => 'resize',
      'arguments' => [
        'width' => 20,
        'height' => 10,
      ],
      'width' => 20,
      'height' => 10,
      'corners' => $default_corners,
      'tolerance' => 0,
    ],
    'scale_x' => [
      'function' => 'scale',
      'arguments' => [
        'width' => 20,
      ],
      'width' => 20,
      'height' => 10,
      'corners' => $default_corners,
      'tolerance' => 0,
    ],
    'scale_y' => [
      'function' => 'scale',
      'arguments' => [
        'height' => 10,
      ],
      'width' => 20,
      'height' => 10,
      'corners' => $default_corners,
      'tolerance' => 0,
    ],
    'upscale_x' => [
      'function' => 'scale',
      'arguments' => [
        'width' => 80,
        'upscale' => TRUE,
      ],
      'width' => 80,
      'height' => 40,
      'corners' => $default_corners,
      'tolerance' => 0,
    ],
    'upscale_y' => [
      'function' => 'scale',
      'arguments' => [
        'height' => 40,
        'upscale' => TRUE,
      ],
      'width' => 80,
      'height' => 40,
      'corners' => $default_corners,
      'tolerance' => 0,
    ],
    'crop' => [
      'function' => 'crop',
      'arguments' => [
        'x' => 12,
        'y' => 4,
        'width' => 16,
        'height' => 12,
      ],
      'width' => 16,
      'height' => 12,
      'corners' => array_fill(0, 4, $this->white),
      'tolerance' => 0,
    ],
    'scale_and_crop' => [
      'function' => 'scale_and_crop',
      'arguments' => [
        'width' => 10,
        'height' => 8,
      ],
      'width' => 10,
      'height' => 8,
      'corners' => array_fill(0, 4, $this->black),
      'tolerance' => 100,
    ],
    'convert_jpg' => [
      'function' => 'convert',
      'width' => 40,
      'height' => 20,
      'arguments' => [
        'extension' => 'jpeg',
      ],
      'mimetype' => 'image/jpeg',
      'corners' => $default_corners,
      'tolerance' => 0,
    ],
    'convert_gif' => [
      'function' => 'convert',
      'width' => 40,
      'height' => 20,
      'arguments' => [
        'extension' => 'gif',
      ],
      'mimetype' => 'image/gif',
      'corners' => $default_corners,
      'tolerance' => 15,
    ],
    'convert_png' => [
      'function' => 'convert',
      'width' => 40,
      'height' => 20,
      'arguments' => [
        'extension' => 'png',
      ],
      'mimetype' => 'image/png',
      'corners' => $default_corners,
      'tolerance' => 5,
    ],
    'rotate_5' => [
      'function' => 'rotate',
      'arguments' => [
        'degrees' => 5,
        'background' => '#FF00FF',
        'resize_filter' => 'Box',
      ],
      'width' => 41,
      'height' => 23,
      'corners' => array_fill(0, 4, $this->fuchsia),
      'tolerance' => 5,
    ],
    'rotate_minus_10' => [
      'function' => 'rotate',
      'arguments' => [
        'degrees' => -10,
        'background' => '#FF00FF',
        'resize_filter' => 'Box',
      ],
      'width' => 41,
      'height' => 26,
      'corners' => array_fill(0, 4, $this->fuchsia),
      'tolerance' => 15,
    ],
    'rotate_90' => [
      'function' => 'rotate',
      'arguments' => [
        'degrees' => 90,
        'background' => '#FF00FF',
      ],
      'width' => 20,
      'height' => 40,
      'corners' => [
        $this->transparent,
        $this->red,
        $this->green,
        $this->blue,
      ],
      'tolerance' => 0,
    ],
    'rotate_transparent_5' => [
      'function' => 'rotate',
      'arguments' => [
        'degrees' => 5,
        'resize_filter' => 'Box',
      ],
      'width' => 41,
      'height' => 23,
      'corners' => array_fill(0, 4, $this->transparent),
      'tolerance' => 0,
    ],
    'rotate_transparent_90' => [
      'function' => 'rotate',
      'arguments' => [
        'degrees' => 90,
      ],
      'width' => 20,
      'height' => 40,
      'corners' => [
        $this->transparent,
        $this->red,
        $this->green,
        $this->blue,
      ],
      'tolerance' => 0,
    ],
    'desaturate' => [
      'function' => 'desaturate',
      'arguments' => [],
      'height' => 20,
      'width' => 40,
      // Grayscale corners are a bit funky. Each of the corners are a shade of
      // gray. The values of these were determined simply by looking at the
      // final image to see what desaturated colors end up being.
      'corners' => [
        array_fill(0, 3, 76) + [
          3 => 0,
        ],
        array_fill(0, 3, 149) + [
          3 => 0,
        ],
        array_fill(0, 3, 29) + [
          3 => 0,
        ],
        array_fill(0, 3, 225) + [
          3 => 127,
        ],
      ],
      // @todo tolerance here is too high. Check reasons.
      'tolerance' => 17000,
    ],
  ];

  // Prepare a copy of test files.
  $this
    ->getTestFiles('image');
  foreach ($files as $file) {
    $image_uri = 'public://' . $file;
    foreach ($operations as $op => $values) {

      // Load up a fresh image.
      $image = $this->imageFactory
        ->get($image_uri);
      if (!$image
        ->isValid()) {
        $this
          ->fail("Could not load image {$file}.");
        continue 2;
      }

      // Check that no multi-frame information is set.
      $this
        ->assertSame(1, $image
        ->getToolkit()
        ->getFrames());

      // Legacy source tests.
      $this
        ->assertSame($image_uri, $image
        ->getToolkit()
        ->getSource());
      $this
        ->assertSame($image
        ->getToolkit()
        ->arguments()
        ->getSourceLocalPath(), $image
        ->getToolkit()
        ->getSourceLocalPath());
      $this
        ->assertSame($image
        ->getToolkit()
        ->arguments()
        ->getSourceFormat(), $image
        ->getToolkit()
        ->getSourceFormat());

      // Perform our operation.
      $image
        ->apply($values['function'], $values['arguments']);

      // Save image.
      $file_path = $this->testDirectory . '/' . $op . substr($file, -4);
      $this
        ->assertTrue($image
        ->save($file_path));

      // Legacy destination tests.
      $this
        ->assertSame($file_path, $image
        ->getToolkit()
        ->getDestination());
      $this
        ->assertSame('', $image
        ->getToolkit()
        ->getDestinationLocalPath());
      $this
        ->assertNotNull($image
        ->getToolkit()
        ->arguments()
        ->getSourceFormat(), $image
        ->getToolkit()
        ->getDestinationFormat());

      // Reload image.
      $image = $this->imageFactory
        ->get($file_path);
      $this
        ->assertTrue($image
        ->isValid());

      // Legacy set methods.
      $image
        ->getToolkit()
        ->setSourceLocalPath('bar');
      $image
        ->getToolkit()
        ->setSourceFormat('PNG');
      $image
        ->getToolkit()
        ->setDestination('foo');
      $image
        ->getToolkit()
        ->setDestinationLocalPath('baz');
      $image
        ->getToolkit()
        ->setDestinationFormat('GIF');
      $this
        ->assertSame('bar', $image
        ->getToolkit()
        ->arguments()
        ->getSourceLocalPath());
      $this
        ->assertSame('PNG', $image
        ->getToolkit()
        ->arguments()
        ->getSourceFormat());
      $this
        ->assertSame('foo', $image
        ->getToolkit()
        ->arguments()
        ->getDestination());
      $this
        ->assertSame('baz', $image
        ->getToolkit()
        ->arguments()
        ->getDestinationLocalPath());
      $this
        ->assertSame('GIF', $image
        ->getToolkit()
        ->arguments()
        ->getDestinationFormat());
      $image
        ->getToolkit()
        ->setSourceFormatFromExtension('jpg');
      $image
        ->getToolkit()
        ->setDestinationFormatFromExtension('jpg');
      $this
        ->assertSame('JPEG', $image
        ->getToolkit()
        ->arguments()
        ->getSourceFormat());
      $this
        ->assertSame('JPEG', $image
        ->getToolkit()
        ->arguments()
        ->getDestinationFormat());
    }
  }

  // Test loading non-existing image files.
  $this
    ->assertFalse($this->imageFactory
    ->get('/generic.png')
    ->isValid());
  $this
    ->assertFalse($this->imageFactory
    ->get('public://generic.png')
    ->isValid());

  // Test retrieval of EXIF information.
  file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-exif.jpeg', 'public://', FILE_EXISTS_REPLACE);

  // The image files that will be tested.
  $image_files = [
    [
      'path' => drupal_get_path('module', 'imagemagick') . '/misc/test-exif.jpeg',
      'orientation' => 8,
    ],
    [
      'path' => 'public://test-exif.jpeg',
      'orientation' => 8,
    ],
    [
      'path' => 'dummy-remote://test-exif.jpeg',
      'orientation' => 8,
    ],
    [
      'path' => 'public://image-test.jpg',
      'orientation' => NULL,
    ],
    [
      'path' => 'public://image-test.png',
      'orientation' => NULL,
    ],
    [
      'path' => 'public://image-test.gif',
      'orientation' => NULL,
    ],
    [
      'path' => NULL,
      'orientation' => NULL,
    ],
  ];
  foreach ($image_files as $image_file) {

    // Get image using 'getimagesize'.
    \Drupal::configFactory()
      ->getEditable('imagemagick.settings')
      ->set('use_identify', FALSE)
      ->save();
    $image = $this->imageFactory
      ->get($image_file['path']);
    $this
      ->assertSame($image_file['orientation'], $image
      ->getToolkit()
      ->getExifOrientation());
  }
}