Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Image / ToolkitGdTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Image;
4
5 use Drupal\Core\Image\ImageInterface;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Site\Settings;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests that core image manipulations work properly: scale, resize, rotate,
12  * crop, scale and crop, and desaturate.
13  *
14  * @group Image
15  */
16 class ToolkitGdTest extends KernelTestBase {
17
18   /**
19    * The image factory service.
20    *
21    * @var \Drupal\Core\Image\ImageFactory
22    */
23   protected $imageFactory;
24
25   // Colors that are used in testing.
26   protected $black       = [0, 0, 0, 0];
27   protected $red         = [255, 0, 0, 0];
28   protected $green       = [0, 255, 0, 0];
29   protected $blue        = [0, 0, 255, 0];
30   protected $yellow      = [255, 255, 0, 0];
31   protected $white       = [255, 255, 255, 0];
32   protected $transparent = [0, 0, 0, 127];
33   // Used as rotate background colors.
34   protected $fuchsia           = [255, 0, 255, 0];
35   protected $rotateTransparent = [255, 255, 255, 127];
36
37   protected $width = 40;
38   protected $height = 20;
39
40   /**
41    * Modules to enable.
42    *
43    * @var array
44    */
45   public static $modules = ['system', 'simpletest'];
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp() {
51     parent::setUp();
52
53     // Set the image factory service.
54     $this->imageFactory = $this->container->get('image.factory');
55   }
56
57   protected function checkRequirements() {
58     // GD2 support is available.
59     if (!function_exists('imagegd2')) {
60       return [
61         'Image manipulations for the GD toolkit cannot run because the GD toolkit is not available.',
62       ];
63     }
64     return parent::checkRequirements();
65   }
66
67   /**
68    * Function to compare two colors by RGBa.
69    */
70   public function colorsAreEqual($color_a, $color_b) {
71     // Fully transparent pixels are equal, regardless of RGB.
72     if ($color_a[3] == 127 && $color_b[3] == 127) {
73       return TRUE;
74     }
75
76     foreach ($color_a as $key => $value) {
77       if ($color_b[$key] != $value) {
78         return FALSE;
79       }
80     }
81
82     return TRUE;
83   }
84
85   /**
86    * Function for finding a pixel's RGBa values.
87    */
88   public function getPixelColor(ImageInterface $image, $x, $y) {
89     $toolkit = $image->getToolkit();
90     $color_index = imagecolorat($toolkit->getResource(), $x, $y);
91
92     $transparent_index = imagecolortransparent($toolkit->getResource());
93     if ($color_index == $transparent_index) {
94       return [0, 0, 0, 127];
95     }
96
97     return array_values(imagecolorsforindex($toolkit->getResource(), $color_index));
98   }
99
100   /**
101    * Since PHP can't visually check that our images have been manipulated
102    * properly, build a list of expected color values for each of the corners and
103    * the expected height and widths for the final images.
104    */
105   public function testManipulations() {
106
107     // Test that the image factory is set to use the GD toolkit.
108     $this->assertEqual($this->imageFactory->getToolkitId(), 'gd', 'The image factory is set to use the \'gd\' image toolkit.');
109
110     // Test the list of supported extensions.
111     $expected_extensions = ['png', 'gif', 'jpeg', 'jpg', 'jpe'];
112     $supported_extensions = $this->imageFactory->getSupportedExtensions();
113     $this->assertEqual($expected_extensions, array_intersect($expected_extensions, $supported_extensions));
114
115     // Test that the supported extensions map to correct internal GD image
116     // types.
117     $expected_image_types = [
118       'png' => IMAGETYPE_PNG,
119       'gif' => IMAGETYPE_GIF,
120       'jpeg' => IMAGETYPE_JPEG,
121       'jpg' => IMAGETYPE_JPEG,
122       'jpe' => IMAGETYPE_JPEG,
123     ];
124     $image = $this->imageFactory->get();
125     foreach ($expected_image_types as $extension => $expected_image_type) {
126       $image_type = $image->getToolkit()->extensionToImageType($extension);
127       $this->assertSame($expected_image_type, $image_type);
128     }
129
130     // Typically the corner colors will be unchanged. These colors are in the
131     // order of top-left, top-right, bottom-right, bottom-left.
132     $default_corners = [$this->red, $this->green, $this->blue, $this->transparent];
133
134     // A list of files that will be tested.
135     $files = [
136       'image-test.png',
137       'image-test.gif',
138       'image-test-no-transparency.gif',
139       'image-test.jpg',
140     ];
141
142     // Setup a list of tests to perform on each type.
143     $operations = [
144       'resize' => [
145         'function' => 'resize',
146         'arguments' => ['width' => 20, 'height' => 10],
147         'width' => 20,
148         'height' => 10,
149         'corners' => $default_corners,
150       ],
151       'scale_x' => [
152         'function' => 'scale',
153         'arguments' => ['width' => 20],
154         'width' => 20,
155         'height' => 10,
156         'corners' => $default_corners,
157       ],
158       'scale_y' => [
159         'function' => 'scale',
160         'arguments' => ['height' => 10],
161         'width' => 20,
162         'height' => 10,
163         'corners' => $default_corners,
164       ],
165       'upscale_x' => [
166         'function' => 'scale',
167         'arguments' => ['width' => 80, 'upscale' => TRUE],
168         'width' => 80,
169         'height' => 40,
170         'corners' => $default_corners,
171       ],
172       'upscale_y' => [
173         'function' => 'scale',
174         'arguments' => ['height' => 40, 'upscale' => TRUE],
175         'width' => 80,
176         'height' => 40,
177         'corners' => $default_corners,
178       ],
179       'crop' => [
180         'function' => 'crop',
181         'arguments' => ['x' => 12, 'y' => 4, 'width' => 16, 'height' => 12],
182         'width' => 16,
183         'height' => 12,
184         'corners' => array_fill(0, 4, $this->white),
185       ],
186       'scale_and_crop' => [
187         'function' => 'scale_and_crop',
188         'arguments' => ['width' => 10, 'height' => 8],
189         'width' => 10,
190         'height' => 8,
191         'corners' => array_fill(0, 4, $this->black),
192       ],
193       'convert_jpg' => [
194         'function' => 'convert',
195         'width' => 40,
196         'height' => 20,
197         'arguments' => ['extension' => 'jpeg'],
198         'corners' => $default_corners,
199       ],
200       'convert_gif' => [
201         'function' => 'convert',
202         'width' => 40,
203         'height' => 20,
204         'arguments' => ['extension' => 'gif'],
205         'corners' => $default_corners,
206       ],
207       'convert_png' => [
208         'function' => 'convert',
209         'width' => 40,
210         'height' => 20,
211         'arguments' => ['extension' => 'png'],
212         'corners' => $default_corners,
213       ],
214     ];
215
216     // Systems using non-bundled GD2 don't have imagerotate. Test if available.
217     // @todo Remove the version check once
218     //   https://www.drupal.org/project/drupal/issues/2670966 is resolved.
219     if (function_exists('imagerotate') && (version_compare(phpversion(), '7.0.26') < 0)) {
220       $operations += [
221         'rotate_5' => [
222           'function' => 'rotate',
223           // Fuchsia background.
224           'arguments' => ['degrees' => 5, 'background' => '#FF00FF'],
225           'width' => 41,
226           'height' => 23,
227           'corners' => array_fill(0, 4, $this->fuchsia),
228         ],
229         'rotate_90' => [
230           'function' => 'rotate',
231           // Fuchsia background.
232           'arguments' => ['degrees' => 90, 'background' => '#FF00FF'],
233           'width' => 20,
234           'height' => 40,
235           'corners' => [$this->transparent, $this->red, $this->green, $this->blue],
236         ],
237         'rotate_transparent_5' => [
238           'function' => 'rotate',
239           'arguments' => ['degrees' => 5],
240           'width' => 41,
241           'height' => 23,
242           'corners' => array_fill(0, 4, $this->rotateTransparent),
243         ],
244         'rotate_transparent_90' => [
245           'function' => 'rotate',
246           'arguments' => ['degrees' => 90],
247           'width' => 20,
248           'height' => 40,
249           'corners' => [$this->transparent, $this->red, $this->green, $this->blue],
250         ],
251       ];
252     }
253
254     // Systems using non-bundled GD2 don't have imagefilter. Test if available.
255     if (function_exists('imagefilter')) {
256       $operations += [
257         'desaturate' => [
258           'function' => 'desaturate',
259           'arguments' => [],
260           'height' => 20,
261           'width' => 40,
262           // Grayscale corners are a bit funky. Each of the corners are a shade of
263           // gray. The values of these were determined simply by looking at the
264           // final image to see what desaturated colors end up being.
265           'corners' => [
266             array_fill(0, 3, 76) + [3 => 0],
267             array_fill(0, 3, 149) + [3 => 0],
268             array_fill(0, 3, 29) + [3 => 0],
269             array_fill(0, 3, 225) + [3 => 127],
270           ],
271         ],
272       ];
273     }
274
275     // Prepare a directory for test file results.
276     $directory = Settings::get('file_public_path') . '/imagetest';
277     file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
278
279     foreach ($files as $file) {
280       foreach ($operations as $op => $values) {
281         // Load up a fresh image.
282         $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file);
283         $toolkit = $image->getToolkit();
284         if (!$image->isValid()) {
285           $this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
286           continue 2;
287         }
288         $image_original_type = $image->getToolkit()->getType();
289
290         // All images should be converted to truecolor when loaded.
291         $image_truecolor = imageistruecolor($toolkit->getResource());
292         $this->assertTrue($image_truecolor, new FormattableMarkup('Image %file after load is a truecolor image.', ['%file' => $file]));
293
294         // Store the original GD resource.
295         $old_res = $toolkit->getResource();
296
297         // Perform our operation.
298         $image->apply($values['function'], $values['arguments']);
299
300         // If the operation replaced the resource, check that the old one has
301         // been destroyed.
302         $new_res = $toolkit->getResource();
303         if ($new_res !== $old_res) {
304           $this->assertFalse(is_resource($old_res), new FormattableMarkup("'%operation' destroyed the original resource.", ['%operation' => $values['function']]));
305         }
306
307         // To keep from flooding the test with assert values, make a general
308         // value for whether each group of values fail.
309         $correct_dimensions_real = TRUE;
310         $correct_dimensions_object = TRUE;
311
312         if (imagesy($toolkit->getResource()) != $values['height'] || imagesx($toolkit->getResource()) != $values['width']) {
313           $correct_dimensions_real = FALSE;
314         }
315
316         // Check that the image object has an accurate record of the dimensions.
317         if ($image->getWidth() != $values['width'] || $image->getHeight() != $values['height']) {
318           $correct_dimensions_object = FALSE;
319         }
320
321         $file_path = $directory . '/' . $op . image_type_to_extension($image->getToolkit()->getType());
322         $image->save($file_path);
323
324         $this->assertTrue($correct_dimensions_real, new FormattableMarkup('Image %file after %action action has proper dimensions.', ['%file' => $file, '%action' => $op]));
325         $this->assertTrue($correct_dimensions_object, new FormattableMarkup('Image %file object after %action action is reporting the proper height and width values.', ['%file' => $file, '%action' => $op]));
326
327         // JPEG colors will always be messed up due to compression. So we skip
328         // these tests if the original or the result is in jpeg format.
329         if ($image->getToolkit()->getType() != IMAGETYPE_JPEG && $image_original_type != IMAGETYPE_JPEG) {
330           // Now check each of the corners to ensure color correctness.
331           foreach ($values['corners'] as $key => $corner) {
332             // The test gif that does not have transparency color set is a
333             // special case.
334             if ($file === 'image-test-no-transparency.gif') {
335               if ($op == 'desaturate') {
336                 // For desaturating, keep the expected color from the test
337                 // data, but set alpha channel to fully opaque.
338                 $corner[3] = 0;
339               }
340               elseif ($corner === $this->transparent) {
341                 // Set expected pixel to yellow where the others have
342                 // transparent.
343                 $corner = $this->yellow;
344               }
345             }
346
347             // Get the location of the corner.
348             switch ($key) {
349               case 0:
350                 $x = 0;
351                 $y = 0;
352                 break;
353               case 1:
354                 $x = $image->getWidth() - 1;
355                 $y = 0;
356                 break;
357               case 2:
358                 $x = $image->getWidth() - 1;
359                 $y = $image->getHeight() - 1;
360                 break;
361               case 3:
362                 $x = 0;
363                 $y = $image->getHeight() - 1;
364                 break;
365             }
366             $color = $this->getPixelColor($image, $x, $y);
367             // We also skip the color test for transparency for gif <-> png
368             // conversion. The convert operation cannot handle that correctly.
369             if ($image->getToolkit()->getType() == $image_original_type || $corner != $this->transparent) {
370               $correct_colors = $this->colorsAreEqual($color, $corner);
371               $this->assertTrue($correct_colors, new FormattableMarkup('Image %file object after %action action has the correct color placement at corner %corner.',
372                 ['%file' => $file, '%action' => $op, '%corner' => $key]));
373             }
374           }
375         }
376
377         // Check that saved image reloads without raising PHP errors.
378         $image_reloaded = $this->imageFactory->get($file_path);
379         $resource = $image_reloaded->getToolkit()->getResource();
380       }
381     }
382
383     // Test creation of image from scratch, and saving to storage.
384     foreach ([IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG] as $type) {
385       $image = $this->imageFactory->get();
386       $image->createNew(50, 20, image_type_to_extension($type, FALSE), '#ffff00');
387       $file = 'from_null' . image_type_to_extension($type);
388       $file_path = $directory . '/' . $file;
389       $this->assertEqual(50, $image->getWidth(), new FormattableMarkup('Image file %file has the correct width.', ['%file' => $file]));
390       $this->assertEqual(20, $image->getHeight(), new FormattableMarkup('Image file %file has the correct height.', ['%file' => $file]));
391       $this->assertEqual(image_type_to_mime_type($type), $image->getMimeType(), new FormattableMarkup('Image file %file has the correct MIME type.', ['%file' => $file]));
392       $this->assertTrue($image->save($file_path), new FormattableMarkup('Image %file created anew from a null image was saved.', ['%file' => $file]));
393
394       // Reload saved image.
395       $image_reloaded = $this->imageFactory->get($file_path);
396       if (!$image_reloaded->isValid()) {
397         $this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
398         continue;
399       }
400       $this->assertEqual(50, $image_reloaded->getWidth(), new FormattableMarkup('Image file %file has the correct width.', ['%file' => $file]));
401       $this->assertEqual(20, $image_reloaded->getHeight(), new FormattableMarkup('Image file %file has the correct height.', ['%file' => $file]));
402       $this->assertEqual(image_type_to_mime_type($type), $image_reloaded->getMimeType(), new FormattableMarkup('Image file %file has the correct MIME type.', ['%file' => $file]));
403       if ($image_reloaded->getToolkit()->getType() == IMAGETYPE_GIF) {
404         $this->assertEqual('#ffff00', $image_reloaded->getToolkit()->getTransparentColor(), new FormattableMarkup('Image file %file has the correct transparent color channel set.', ['%file' => $file]));
405       }
406       else {
407         $this->assertEqual(NULL, $image_reloaded->getToolkit()->getTransparentColor(), new FormattableMarkup('Image file %file has no color channel set.', ['%file' => $file]));
408       }
409     }
410
411     // Test failures of the 'create_new' operation.
412     $image = $this->imageFactory->get();
413     $image->createNew(-50, 20);
414     $this->assertFalse($image->isValid(), 'CreateNew with negative width fails.');
415     $image->createNew(50, 20, 'foo');
416     $this->assertFalse($image->isValid(), 'CreateNew with invalid extension fails.');
417     $image->createNew(50, 20, 'gif', '#foo');
418     $this->assertFalse($image->isValid(), 'CreateNew with invalid color hex string fails.');
419     $image->createNew(50, 20, 'gif', '#ff0000');
420     $this->assertTrue($image->isValid(), 'CreateNew with valid arguments validates the Image.');
421   }
422
423   /**
424    * Tests that GD resources are freed from memory.
425    */
426   public function testResourceDestruction() {
427     // Test that an Image object going out of scope releases its GD resource.
428     $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png');
429     $res = $image->getToolkit()->getResource();
430     $this->assertTrue(is_resource($res), 'Successfully loaded image resource.');
431     $image = NULL;
432     $this->assertFalse(is_resource($res), 'Image resource was destroyed after losing scope.');
433
434     // Test that 'create_new' operation does not leave orphaned GD resources.
435     $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png');
436     $old_res = $image->getToolkit()->getResource();
437     // Check if resource has been created successfully.
438     $this->assertTrue(is_resource($old_res));
439     $image->createNew(20, 20);
440     $new_res = $image->getToolkit()->getResource();
441     // Check if the original resource has been destroyed.
442     $this->assertFalse(is_resource($old_res));
443     // Check if a new resource has been created successfully.
444     $this->assertTrue(is_resource($new_res));
445   }
446
447   /**
448    * Tests for GIF images with transparency.
449    */
450   public function testGifTransparentImages() {
451     // Prepare a directory for test file results.
452     $directory = Settings::get('file_public_path') . '/imagetest';
453     file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
454
455     // Test loading an indexed GIF image with transparent color set.
456     // Color at top-right pixel should be fully transparent.
457     $file = 'image-test-transparent-indexed.gif';
458     $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file);
459     $resource = $image->getToolkit()->getResource();
460     $color_index = imagecolorat($resource, $image->getWidth() - 1, 0);
461     $color = array_values(imagecolorsforindex($resource, $color_index));
462     $this->assertEqual($this->rotateTransparent, $color, "Image {$file} after load has full transparent color at corner 1.");
463
464     // Test deliberately creating a GIF image with no transparent color set.
465     // Color at top-right pixel should be fully transparent while in memory,
466     // fully opaque after flushing image to file.
467     $file = 'image-test-no-transparent-color-set.gif';
468     $file_path = $directory . '/' . $file;
469     // Create image.
470     $image = $this->imageFactory->get();
471     $image->createNew(50, 20, 'gif', NULL);
472     $resource = $image->getToolkit()->getResource();
473     $color_index = imagecolorat($resource, $image->getWidth() - 1, 0);
474     $color = array_values(imagecolorsforindex($resource, $color_index));
475     $this->assertEqual($this->rotateTransparent, $color, "New GIF image with no transparent color set after creation has full transparent color at corner 1.");
476     // Save image.
477     $this->assertTrue($image->save($file_path), "New GIF image {$file} was saved.");
478     // Reload image.
479     $image_reloaded = $this->imageFactory->get($file_path);
480     $resource = $image_reloaded->getToolkit()->getResource();
481     $color_index = imagecolorat($resource, $image_reloaded->getWidth() - 1, 0);
482     $color = array_values(imagecolorsforindex($resource, $color_index));
483     // Check explicitly for alpha == 0 as the rest of the color has been
484     // compressed and may have slight difference from full white.
485     $this->assertEqual(0, $color[3], "New GIF image {$file} after reload has no transparent color at corner 1.");
486
487     // Test loading an image whose transparent color index is out of range.
488     // This image was generated by taking an initial image with a palette size
489     // of 6 colors, and setting the transparent color index to 6 (one higher
490     // than the largest allowed index), as follows:
491     // @code
492     // $image = imagecreatefromgif('core/modules/simpletest/files/image-test.gif');
493     // imagecolortransparent($image, 6);
494     // imagegif($image, 'core/modules/simpletest/files/image-test-transparent-out-of-range.gif');
495     // @endcode
496     // This allows us to test that an image with an out-of-range color index
497     // can be loaded correctly.
498     $file = 'image-test-transparent-out-of-range.gif';
499     $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file);
500     $toolkit = $image->getToolkit();
501
502     if (!$image->isValid()) {
503       $this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
504     }
505     else {
506       // All images should be converted to truecolor when loaded.
507       $image_truecolor = imageistruecolor($toolkit->getResource());
508       $this->assertTrue($image_truecolor, new FormattableMarkup('Image %file after load is a truecolor image.', ['%file' => $file]));
509     }
510   }
511
512   /**
513    * Tests calling a missing image operation plugin.
514    */
515   public function testMissingOperation() {
516
517     // Test that the image factory is set to use the GD toolkit.
518     $this->assertEqual($this->imageFactory->getToolkitId(), 'gd', 'The image factory is set to use the \'gd\' image toolkit.');
519
520     // An image file that will be tested.
521     $file = 'image-test.png';
522
523     // Load up a fresh image.
524     $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file);
525     if (!$image->isValid()) {
526       $this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
527     }
528
529     // Try perform a missing toolkit operation.
530     $this->assertFalse($image->apply('missing_op', []), 'Calling a missing image toolkit operation plugin fails.');
531   }
532
533 }