/core/modules/image/src/Tests/ImageDimensionsTest.php

https://gitlab.com/reasonat/test8 · PHP · 290 lines · 221 code · 31 blank · 38 comment · 0 complexity · e16e0b703e7086c1ba4b7a84832ecb83 MD5 · raw file

  1. <?php
  2. namespace Drupal\image\Tests;
  3. use Drupal\image\Entity\ImageStyle;
  4. use Drupal\simpletest\WebTestBase;
  5. /**
  6. * Tests that images have correct dimensions when styled.
  7. *
  8. * @group image
  9. */
  10. class ImageDimensionsTest extends WebTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = array('image', 'image_module_test');
  17. protected $profile = 'testing';
  18. /**
  19. * Test styled image dimensions cumulatively.
  20. */
  21. function testImageDimensions() {
  22. $image_factory = $this->container->get('image.factory');
  23. // Create a working copy of the file.
  24. $files = $this->drupalGetTestFiles('image');
  25. $file = reset($files);
  26. $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  27. // Create a style.
  28. /** @var $style \Drupal\image\ImageStyleInterface */
  29. $style = ImageStyle::create(array('name' => 'test', 'label' => 'Test'));
  30. $style->save();
  31. $generated_uri = 'public://styles/test/public/' . \Drupal::service('file_system')->basename($original_uri);
  32. $url = file_url_transform_relative($style->buildUrl($original_uri));
  33. $variables = array(
  34. '#theme' => 'image_style',
  35. '#style_name' => 'test',
  36. '#uri' => $original_uri,
  37. '#width' => 40,
  38. '#height' => 20,
  39. );
  40. // Verify that the original image matches the hard-coded values.
  41. $image_file = $image_factory->get($original_uri);
  42. $this->assertEqual($image_file->getWidth(), $variables['#width']);
  43. $this->assertEqual($image_file->getHeight(), $variables['#height']);
  44. // Scale an image that is wider than it is high.
  45. $effect = array(
  46. 'id' => 'image_scale',
  47. 'data' => array(
  48. 'width' => 120,
  49. 'height' => 90,
  50. 'upscale' => TRUE,
  51. ),
  52. 'weight' => 0,
  53. );
  54. $style->addImageEffect($effect);
  55. $style->save();
  56. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="120" height="60" alt="" class="image-style-test" />');
  57. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  58. $this->drupalGet($this->getAbsoluteUrl($url));
  59. $this->assertResponse(200, 'Image was generated at the URL.');
  60. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  61. $image_file = $image_factory->get($generated_uri);
  62. $this->assertEqual($image_file->getWidth(), 120);
  63. $this->assertEqual($image_file->getHeight(), 60);
  64. // Rotate 90 degrees anticlockwise.
  65. $effect = array(
  66. 'id' => 'image_rotate',
  67. 'data' => array(
  68. 'degrees' => -90,
  69. 'random' => FALSE,
  70. ),
  71. 'weight' => 1,
  72. );
  73. $style->addImageEffect($effect);
  74. $style->save();
  75. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="60" height="120" alt="" class="image-style-test" />');
  76. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  77. $this->drupalGet($this->getAbsoluteUrl($url));
  78. $this->assertResponse(200, 'Image was generated at the URL.');
  79. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  80. $image_file = $image_factory->get($generated_uri);
  81. $this->assertEqual($image_file->getWidth(), 60);
  82. $this->assertEqual($image_file->getHeight(), 120);
  83. // Scale an image that is higher than it is wide (rotated by previous effect).
  84. $effect = array(
  85. 'id' => 'image_scale',
  86. 'data' => array(
  87. 'width' => 120,
  88. 'height' => 90,
  89. 'upscale' => TRUE,
  90. ),
  91. 'weight' => 2,
  92. );
  93. $style->addImageEffect($effect);
  94. $style->save();
  95. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
  96. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  97. $this->drupalGet($this->getAbsoluteUrl($url));
  98. $this->assertResponse(200, 'Image was generated at the URL.');
  99. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  100. $image_file = $image_factory->get($generated_uri);
  101. $this->assertEqual($image_file->getWidth(), 45);
  102. $this->assertEqual($image_file->getHeight(), 90);
  103. // Test upscale disabled.
  104. $effect = array(
  105. 'id' => 'image_scale',
  106. 'data' => array(
  107. 'width' => 400,
  108. 'height' => 200,
  109. 'upscale' => FALSE,
  110. ),
  111. 'weight' => 3,
  112. );
  113. $style->addImageEffect($effect);
  114. $style->save();
  115. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
  116. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  117. $this->drupalGet($this->getAbsoluteUrl($url));
  118. $this->assertResponse(200, 'Image was generated at the URL.');
  119. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  120. $image_file = $image_factory->get($generated_uri);
  121. $this->assertEqual($image_file->getWidth(), 45);
  122. $this->assertEqual($image_file->getHeight(), 90);
  123. // Add a desaturate effect.
  124. $effect = array(
  125. 'id' => 'image_desaturate',
  126. 'data' => array(),
  127. 'weight' => 4,
  128. );
  129. $style->addImageEffect($effect);
  130. $style->save();
  131. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
  132. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  133. $this->drupalGet($this->getAbsoluteUrl($url));
  134. $this->assertResponse(200, 'Image was generated at the URL.');
  135. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  136. $image_file = $image_factory->get($generated_uri);
  137. $this->assertEqual($image_file->getWidth(), 45);
  138. $this->assertEqual($image_file->getHeight(), 90);
  139. // Add a random rotate effect.
  140. $effect = array(
  141. 'id' => 'image_rotate',
  142. 'data' => array(
  143. 'degrees' => 180,
  144. 'random' => TRUE,
  145. ),
  146. 'weight' => 5,
  147. );
  148. $style->addImageEffect($effect);
  149. $style->save();
  150. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
  151. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  152. $this->drupalGet($this->getAbsoluteUrl($url));
  153. $this->assertResponse(200, 'Image was generated at the URL.');
  154. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  155. // Add a crop effect.
  156. $effect = array(
  157. 'id' => 'image_crop',
  158. 'data' => array(
  159. 'width' => 30,
  160. 'height' => 30,
  161. 'anchor' => 'center-center',
  162. ),
  163. 'weight' => 6,
  164. );
  165. $style->addImageEffect($effect);
  166. $style->save();
  167. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="30" height="30" alt="" class="image-style-test" />');
  168. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  169. $this->drupalGet($this->getAbsoluteUrl($url));
  170. $this->assertResponse(200, 'Image was generated at the URL.');
  171. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  172. $image_file = $image_factory->get($generated_uri);
  173. $this->assertEqual($image_file->getWidth(), 30);
  174. $this->assertEqual($image_file->getHeight(), 30);
  175. // Rotate to a non-multiple of 90 degrees.
  176. $effect = array(
  177. 'id' => 'image_rotate',
  178. 'data' => array(
  179. 'degrees' => 57,
  180. 'random' => FALSE,
  181. ),
  182. 'weight' => 7,
  183. );
  184. $effect_id = $style->addImageEffect($effect);
  185. $style->save();
  186. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="41" height="41" alt="" class="image-style-test" />');
  187. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  188. $this->drupalGet($this->getAbsoluteUrl($url));
  189. $this->assertResponse(200, 'Image was generated at the URL.');
  190. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  191. $image_file = $image_factory->get($generated_uri);
  192. $this->assertEqual($image_file->getWidth(), 41);
  193. $this->assertEqual($image_file->getHeight(), 41);
  194. $effect_plugin = $style->getEffect($effect_id);
  195. $style->deleteImageEffect($effect_plugin);
  196. // Ensure that an effect can unset dimensions.
  197. $effect = array(
  198. 'id' => 'image_module_test_null',
  199. 'data' => array(),
  200. 'weight' => 8,
  201. );
  202. $style->addImageEffect($effect);
  203. $style->save();
  204. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
  205. // Test URI dependent image effect.
  206. $style = ImageStyle::create(['name' => 'test_uri', 'label' => 'Test URI']);
  207. $effect = [
  208. 'id' => 'image_module_test_uri_dependent',
  209. 'data' => [],
  210. 'weight' => 0,
  211. ];
  212. $style->addImageEffect($effect);
  213. $style->save();
  214. $variables = [
  215. '#theme' => 'image_style',
  216. '#style_name' => 'test_uri',
  217. '#uri' => $original_uri,
  218. '#width' => 40,
  219. '#height' => 20,
  220. ];
  221. // PNG original image. Should be resized to 100x100.
  222. $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
  223. $url = file_url_transform_relative($style->buildUrl($original_uri));
  224. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="100" height="100" alt="" class="image-style-test-uri" />');
  225. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  226. $this->drupalGet($this->getAbsoluteUrl($url));
  227. $this->assertResponse(200, 'Image was generated at the URL.');
  228. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  229. $image_file = $image_factory->get($generated_uri);
  230. $this->assertEqual($image_file->getWidth(), 100);
  231. $this->assertEqual($image_file->getHeight(), 100);
  232. // GIF original image. Should be resized to 50x50.
  233. $file = $files[1];
  234. $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  235. $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
  236. $url = file_url_transform_relative($style->buildUrl($original_uri));
  237. $variables['#uri'] = $original_uri;
  238. $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="50" height="50" alt="" class="image-style-test-uri" />');
  239. $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  240. $this->drupalGet($this->getAbsoluteUrl($url));
  241. $this->assertResponse(200, 'Image was generated at the URL.');
  242. $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  243. $image_file = $image_factory->get($generated_uri);
  244. $this->assertEqual($image_file->getWidth(), 50);
  245. $this->assertEqual($image_file->getHeight(), 50);
  246. }
  247. /**
  248. * Render an image style element.
  249. *
  250. * drupal_render() alters the passed $variables array by adding a new key
  251. * '#printed' => TRUE. This prevents next call to re-render the element. We
  252. * wrap drupal_render() in a helper protected method and pass each time a
  253. * fresh array so that $variables won't get altered and the element is
  254. * re-rendered each time.
  255. */
  256. protected function getImageTag($variables) {
  257. return str_replace("\n", NULL, \Drupal::service('renderer')->renderRoot($variables));
  258. }
  259. }