3 namespace Drupal\Tests\crop\Kernel;
6 * Tests the crop image effect.
10 class CropEffectTest extends CropUnitTestBase {
17 public static $modules = ['user', 'image', 'crop', 'file', 'system'];
20 * Tests manual crop image effect.
22 public function testCropEffect() {
23 // Create image to be cropped.
24 $file = $this->getTestFile();
29 'type' => $this->cropType->id(),
30 'entity_id' => $file->id(),
31 'entity_type' => 'file',
32 'uri' => $file->getFileUri(),
38 /** @var \Drupal\crop\CropInterface $crop */
39 $crop = $this->container->get('entity.manager')->getStorage('crop')->create($values);
42 $derivative_uri = $this->testStyle->buildUri($file->getFileUri());
43 $this->testStyle->createDerivative($file->getFileUri(), $derivative_uri);
45 $this->assertTrue(file_exists($derivative_uri), 'Image derivative file exists on the filesystem.');
47 // Test if cropped version looks like expected. Basically loop pixels,
48 // in derivative image and check if they look the same as pixels,
49 // in corresponding region on original image.
50 $original_image = imagecreatefrompng($file->getFileUri());
51 $derivative_image = imagecreatefrompng($derivative_uri);
52 $orig_start = $crop->anchor();
54 for ($x = 0; $x < $values['width']; $x++) {
55 for ($y = 0; $y < $values['height']; $y++) {
56 if (imagecolorat($derivative_image, $x, $y) != imagecolorat($original_image, $orig_start['x'] + $x, $orig_start['y'] + $y)) {
62 $this->assertTrue($matches, 'Cropped image looks the same as region on original.');
66 * Test image crop effect dimensions.
68 public function testCropDimenssions() {
69 // Create image to be cropped.
70 $file = $this->getTestFile();
72 $file_uri = $file->getFileUri();
76 'type' => $this->cropType->id(),
77 'entity_id' => $file->id(),
78 'entity_type' => 'file',
85 $dimensions = ['width' => 0, 'height' => 0];
87 /** @var \Drupal\crop\CropInterface $crop */
88 $crop = $this->container->get('entity_type.manager')->getStorage('crop')->create($values);
91 /** @var $effect \Drupal\crop\Plugin\ImageEffect\CropEffect */
92 $effect = $this->imageEffectManager->createInstance('crop_crop', ['data' => ['crop_type' => $this->cropType->id()]]);
93 $effect->transformDimensions($dimensions, $file_uri);
95 $this->assertEquals($crop->size(), $dimensions, t('CropEffect::transformDimensions() transform image dimensions correctly.'));