Elgg  Version master
crop.php
Go to the documentation of this file.
1 <?php
16 if (elgg_extract('cropper_enabled', $vars, true) === false) {
17  return;
18 }
19 
20 elgg_require_css('cropperjs/cropper');
21 
22 $entity = elgg_extract('entity', $vars);
23 $icon_type = elgg_extract('icon_type', $vars, 'icon');
24 $input_name = elgg_extract('name', $vars, 'icon');
25 
26 // build cropper configuration
28  'viewMode' => 2,
29  'background' => false,
30  'autoCropArea' => 1,
31 ];
32 
33 $cropper_data = array_merge($default_config, (array) elgg_extract('cropper_config', $vars, []));
34 
35 // determine current cropping coordinates
37 if ($entity instanceof \ElggEntity) {
38  $entity_coords = $entity->getIconCoordinates($icon_type);
39 
40  // still enough for cropping
41  if (isset($entity_coords['x1'], $entity_coords['x2'], $entity_coords['y1'], $entity_coords['y2'])) {
42  $cropper_data['data'] = [
43  'x' => $entity_coords['x1'],
44  'y' => $entity_coords['y1'],
45  'width' => $entity_coords['x2'] - $entity_coords['x1'],
46  'height' => $entity_coords['y2'] - $entity_coords['y1'],
47  ];
48 
49  if (!empty($cropper_data['data']['width']) && $cropper_data['data']['height']) {
50  $cropper_data['existingAspectRatio'] = $cropper_data['data']['width'] / $cropper_data['data']['height'];
51  }
52  }
53 }
54 
55 // detect cropping aspect ratio
56 if (!isset($cropper_data['aspectRatio'])) {
57  $detect_aspect_ratio = function($vars) use (&$cropper_data) {
58  $cropper_aspect_ratio_size = elgg_extract('cropper_aspect_ratio_size', $vars, 'master');
59  if ($cropper_aspect_ratio_size === false) {
60  return;
61  }
62 
63  $icon_type = elgg_extract('icon_type', $vars, 'icon');
64  $entity_type = elgg_extract('entity_type', $vars);
65  $entity_subtype = elgg_extract('entity_subtype', $vars);
66 
67  $sizes = elgg_get_icon_sizes($entity_type, $entity_subtype, $icon_type);
68  if (empty($sizes)) {
69  // no way to read the config
70  return;
71  }
72 
73  if (!isset($sizes[$cropper_aspect_ratio_size]) && $cropper_aspect_ratio_size !== 'master') {
74  // fallback to master if custom ratio is missing
75  $cropper_aspect_ratio_size = 'master';
76  }
77 
78  if (!isset($sizes[$cropper_aspect_ratio_size])) {
79  // return if ratio is not present
80  return;
81  }
82 
83  $width = (int) elgg_extract('w', $sizes[$cropper_aspect_ratio_size]);
84  $height = (int) elgg_extract('h', $sizes[$cropper_aspect_ratio_size]);
85 
86  if (empty($width) || empty($height)) {
87  return;
88  }
89 
90  $cropper_data['aspectRatio'] = $width / $height;
91  $cropper_data['initialAspectRatio'] = $cropper_data['aspectRatio'];
92  };
93  $detect_aspect_ratio($vars);
94 }
95 
96 $img_url = null;
97 if ($entity instanceof \ElggEntity && $entity->hasIcon('master', $icon_type)) {
98  $img_url = $entity->getIconURL([
99  'size' => 'master',
100  'type' => $icon_type,
101  ]);
102 }
103 
104 if (isset($cropper_data['existingAspectRatio'], $cropper_data['aspectRatio'])) {
105  // prevents math rounding issues with non square aspect ratios when showing existing cropped area
106  $cropper_data['aspectRatio'] = $cropper_data['existingAspectRatio'];
107  unset($cropper_data['existingAspectRatio']);
108 }
109 
111  'data-icon-cropper' => json_encode($cropper_data),
112  'src' => $img_url,
113  'alt' => elgg_echo('entity:edit:icon:crop:img:alt'),
114 ]);
115 
116 echo elgg_format_element('div', ['class' => ['elgg-entity-edit-icon-crop-wrapper', 'hidden', 'mbm']], $img);
117 
118 $input = '';
119 foreach (['x1', 'y1', 'x2', 'y2'] as $coord) {
121  '#type' => 'hidden',
122  'name' => "{$input_name}_{$coord}",
123  'value' => elgg_extract($coord, $entity_coords),
124  ]);
125 }
126 
127 if (!empty($img_url)) {
129  '#type' => 'hidden',
130  'name' => "_entity_edit_icon_crop[{$input_name}][guid]",
131  'value' => $entity->guid,
132  ]);
134  '#type' => 'hidden',
135  'name' => "_entity_edit_icon_crop[{$input_name}][type]",
136  'value' => $icon_type,
137  ]);
138 }
139 
140 echo elgg_format_element('div', ['class' => ['elgg-entity-edit-icon-crop-input', 'hidden']], $input);
141 
142 echo elgg_view('entity/edit/icon/crop_messages', $vars);
143 
144 ?>
145 <script>
146  import('entity/edit/icon/crop').then((Cropper) => {
147  var cropper = new Cropper.default();
148  cropper.init('input[type="file"][name="<?php echo elgg_extract('name', $vars, 'icon'); ?>"]');
149  });
150 </script>
$entity_coords
Definition: crop.php:36
elgg_require_css(string $view)
Register a CSS view name to be included in the HTML head.
$input_name
Definition: crop.php:24
elgg_view_field(array $params=[])
Renders a form field, usually with a wrapper element, a label, help text, etc.
Definition: views.php:1133
elgg_get_icon_sizes(string $entity_type=null, string $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if($entity instanceof\ElggEntity) if(!isset($cropper_data['aspectRatio'])) $img_url
Definition: crop.php:96
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:177
$data icon
Definition: default.php:28
$entity
Definition: crop.php:22
$default_config
Definition: crop.php:27
$icon_type
Definition: crop.php:23
$cropper_data
Definition: crop.php:33
$vars
Definition: theme.php:5
$input
Definition: crop.php:118
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
if($entity instanceof\ElggEntity &&$entity->hasIcon('master', $icon_type)) if(isset($cropper_data['existingAspectRatio'], $cropper_data['aspectRatio'])) $img
Definition: crop.php:110
$site name
Definition: settings.php:15