comparison core/modules/image/image.admin.inc @ 0:c75dbcec494b

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