Drupal investigation

image.admin.inc 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Administration pages for image settings.
  5. */
  6. use Drupal\Core\Render\Element;
  7. /**
  8. * Prepares variables for image style preview templates.
  9. *
  10. * Default template: image-style-preview.html.twig.
  11. *
  12. * @param array $variables
  13. * An associative array containing:
  14. * - style: \Drupal\image\ImageStyleInterface image style being previewed.
  15. */
  16. function template_preprocess_image_style_preview(&$variables) {
  17. // Style information.
  18. $style = $variables['style'];
  19. $variables['style_id'] = $style->id();
  20. $variables['style_name'] = $style->label();
  21. // Cache bypass token.
  22. $variables['cache_bypass'] = REQUEST_TIME;
  23. // Sample image info.
  24. $sample_width = 160;
  25. $sample_height = 160;
  26. $image_factory = \Drupal::service('image.factory');
  27. // Set up original file information.
  28. $original_path = \Drupal::config('image.settings')->get('preview_image');
  29. $original_image = $image_factory->get($original_path);
  30. $variables['original'] = [
  31. 'url' => file_url_transform_relative(file_create_url($original_path)),
  32. 'width' => $original_image->getWidth(),
  33. 'height' => $original_image->getHeight(),
  34. ];
  35. if ($variables['original']['width'] > $variables['original']['height']) {
  36. $variables['preview']['original']['width'] = min($variables['original']['width'], $sample_width);
  37. $variables['preview']['original']['height'] = round($variables['preview']['original']['width'] / $variables['original']['width'] * $variables['original']['height']);
  38. }
  39. else {
  40. $variables['preview']['original']['height'] = min($variables['original']['height'], $sample_height);
  41. $variables['preview']['original']['width'] = round($variables['preview']['original']['height'] / $variables['original']['height'] * $variables['original']['width']);
  42. }
  43. // Set up derivative file information.
  44. $preview_file = $style->buildUri($original_path);
  45. // Create derivative if necessary.
  46. if (!file_exists($preview_file)) {
  47. $style->createDerivative($original_path, $preview_file);
  48. }
  49. $preview_image = $image_factory->get($preview_file);
  50. $variables['derivative'] = [
  51. 'url' => file_url_transform_relative(file_create_url($preview_file)),
  52. 'width' => $preview_image->getWidth(),
  53. 'height' => $preview_image->getHeight(),
  54. ];
  55. if ($variables['derivative']['width'] > $variables['derivative']['height']) {
  56. $variables['preview']['derivative']['width'] = min($variables['derivative']['width'], $sample_width);
  57. $variables['preview']['derivative']['height'] = round($variables['preview']['derivative']['width'] / $variables['derivative']['width'] * $variables['derivative']['height']);
  58. }
  59. else {
  60. $variables['preview']['derivative']['height'] = min($variables['derivative']['height'], $sample_height);
  61. $variables['preview']['derivative']['width'] = round($variables['preview']['derivative']['height'] / $variables['derivative']['height'] * $variables['derivative']['width']);
  62. }
  63. // Build the preview of the original image.
  64. $variables['original']['rendered'] = [
  65. '#theme' => 'image',
  66. '#uri' => $original_path,
  67. '#alt' => t('Sample original image'),
  68. '#title' => '',
  69. '#attributes' => [
  70. 'width' => $variables['original']['width'],
  71. 'height' => $variables['original']['height'],
  72. 'style' => 'width: ' . $variables['preview']['original']['width'] . 'px; height: ' . $variables['preview']['original']['height'] . 'px;',
  73. ],
  74. ];
  75. // Build the preview of the image style derivative. Timestamps are added
  76. // to prevent caching of images on the client side.
  77. $variables['derivative']['rendered'] = [
  78. '#theme' => 'image',
  79. '#uri' => $variables['derivative']['url'] . '?cache_bypass=' . $variables['cache_bypass'],
  80. '#alt' => t('Sample modified image'),
  81. '#title' => '',
  82. '#attributes' => [
  83. 'width' => $variables['derivative']['width'],
  84. 'height' => $variables['derivative']['height'],
  85. 'style' => 'width: ' . $variables['preview']['derivative']['width'] . 'px; height: ' . $variables['preview']['derivative']['height'] . 'px;',
  86. ],
  87. ];
  88. }
  89. /**
  90. * Prepares variables for image anchor templates.
  91. *
  92. * Default template: image-anchor.html.twig.
  93. *
  94. * @param array $variables
  95. * An associative array containing:
  96. * - element: An associative array containing the image.
  97. */
  98. function template_preprocess_image_anchor(&$variables) {
  99. $element = $variables['element'];
  100. $rows = [];
  101. $row = [];
  102. foreach (Element::children($element) as $n => $key) {
  103. $element[$key]['#attributes']['title'] = $element[$key]['#title'];
  104. unset($element[$key]['#title']);
  105. $row[] = [
  106. 'data' => $element[$key],
  107. ];
  108. if ($n % 3 == 3 - 1) {
  109. $rows[] = $row;
  110. $row = [];
  111. }
  112. }
  113. $variables['table'] = [
  114. '#type' => 'table',
  115. '#header' => [],
  116. '#rows' => $rows,
  117. '#attributes' => [
  118. 'class' => ['image-anchor'],
  119. ],
  120. ];
  121. }