1 image.test public ImageFieldValidateTestCase::testOrientation()

Test image rotation resulting from EXIF data. Requires a special test image, file rotate90cw.jpg.

File

core/modules/image/tests/image.test, line 1094
Tests for image.module.

Class

ImageFieldValidateTestCase
Test class to check for various validations.

Code

public function testOrientation() {
  // Create an image field.
  $field_name = strtolower($this->randomName());
  $instance_settings = array(
    'orientate' => 1,
  );
  $this->createImageField($field_name, 'post', array(), $instance_settings);
  // Upload our test image to this field.
  $orientation_test_image = image_load(backdrop_get_path('module', 'image') . '/images/rotate90cw.jpg');
  $uri = $orientation_test_image->source;
  $img = image_load($uri);
  $this->assertTrue(is_object($img), 'Image data is available.');
  $orientation_test_image->uri = $uri;

  if (!$orientation_test_image) {
    $this->fail(t('Could not load image %file.', array('%file' => 'rotate90cw.jpg')));
  }
  $nid = $this->uploadNodeImage($orientation_test_image, $field_name, 'post');
  $this->assertText(t('The image was rotated by 90 degrees.'), 'The image was correctly re-oriented.');

  $node = node_load($nid, NULL, TRUE);
  $uri = $node->{$field_name}[LANGUAGE_NONE][0]['uri'];

  // Check orientation.
  $img = image_load($uri);
  $this->assertTrue(is_object($img), 'Image data is available.');

  // Test the aspect ratio.
  $this->assertTrue($img->info['width'] > $img->info['height'], 'The image format is landscape.');

  // Verify the rotation by color inspection.
  $rgb = imagecolorat($img->resource, 10, 10);
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8) & 0xFF;
  $b = $rgb & 0xFF;
  // The top left corner should be red.
  $this->assertTrue(abs($r - 255) < 5, 'Red color component is close to 255.');
  $this->assertTrue($g < 5, 'Green color component is close to 0.');
  $this->assertTrue($b < 5, 'Blue color component is close to 0.');
}