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  if ($icon_type === 'icon') {
39  $entity_coords = [
40  'x1' => $entity->x1,
41  'y1' => $entity->y1,
42  'x2' => $entity->x2,
43  'y2' => $entity->y2,
44  ];
45  } elseif (isset($entity->{"{$icon_type}_coords"})) {
46  $entity_coords = unserialize($entity->{"{$icon_type}_coords"});
47  }
48 
49  // cast to ints
50  array_walk($entity_coords, function(&$value) {
51  $value = (int) $value;
52  });
53 
54  // remove invalid values
55  $entity_coords = array_filter($entity_coords, function($value) {
56  return $value >= 0;
57  });
58 
59  // still enough for cropping
60  if (isset($entity_coords['x1'], $entity_coords['x2'], $entity_coords['y1'], $entity_coords['y2'])) {
61  $cropper_data['data'] = [
62  'x' => $entity_coords['x1'],
63  'y' => $entity_coords['y1'],
64  'width' => $entity_coords['x2'] - $entity_coords['x1'],
65  'height' => $entity_coords['y2'] - $entity_coords['y1'],
66  ];
67 
68  if (!empty($cropper_data['data']['width']) && $cropper_data['data']['height']) {
69  $cropper_data['existingAspectRatio'] = $cropper_data['data']['width'] / $cropper_data['data']['height'];
70  }
71  }
72 }
73 
74 // detect cropping aspect ratio
75 if (!isset($cropper_data['aspectRatio'])) {
76  $detect_aspect_ratio = function($vars) use (&$cropper_data) {
77  $cropper_aspect_ratio_size = elgg_extract('cropper_aspect_ratio_size', $vars, 'master');
78  if ($cropper_aspect_ratio_size === false) {
79  return;
80  }
81 
82  $icon_type = elgg_extract('icon_type', $vars, 'icon');
83  $entity_type = elgg_extract('entity_type', $vars);
84  $entity_subtype = elgg_extract('entity_subtype', $vars);
85 
86  $sizes = elgg_get_icon_sizes($entity_type, $entity_subtype, $icon_type);
87  if (empty($sizes)) {
88  // no way to read the config
89  return;
90  }
91 
92  if (!isset($sizes[$cropper_aspect_ratio_size]) && $cropper_aspect_ratio_size !== 'master') {
93  // fallback to master if custom ratio is missing
94  $cropper_aspect_ratio_size = 'master';
95  }
96 
97  if (!isset($sizes[$cropper_aspect_ratio_size])) {
98  // return if ratio is not present
99  return;
100  }
101 
102  $width = (int) elgg_extract('w', $sizes[$cropper_aspect_ratio_size]);
103  $height = (int) elgg_extract('h', $sizes[$cropper_aspect_ratio_size]);
104 
105  if (empty($width) || empty($height)) {
106  return;
107  }
108 
109  $cropper_data['aspectRatio'] = $width / $height;
110  $cropper_data['initialAspectRatio'] = $cropper_data['aspectRatio'];
111  };
112  $detect_aspect_ratio($vars);
113 }
114 
115 $img_url = null;
116 if ($entity instanceof ElggEntity && $entity->hasIcon('master', $icon_type)) {
117  $img_url = $entity->getIconURL([
118  'size' => 'master',
119  'type' => $icon_type,
120  ]);
121 }
122 
123 if (isset($cropper_data['existingAspectRatio'], $cropper_data['aspectRatio'])) {
124  // prevents math rounding issues with non square aspect ratios when showing existing cropped area
125  $cropper_data['aspectRatio'] = $cropper_data['existingAspectRatio'];
126  unset($cropper_data['existingAspectRatio']);
127 }
128 
130  'data-icon-cropper' => json_encode($cropper_data),
131  'src' => $img_url,
132 ]);
133 
134 echo elgg_format_element('div', ['class' => ['elgg-entity-edit-icon-crop-wrapper', 'hidden', 'mbm']], $img);
135 
136 $input = '';
137 foreach (['x1', 'y1', 'x2', 'y2'] as $coord) {
139  '#type' => 'hidden',
140  'name' => "{$input_name}_{$coord}",
141  'value' => elgg_extract($coord, $entity_coords),
142  ]);
143 }
144 
145 if (!empty($img_url)) {
147  '#type' => 'hidden',
148  'name' => "_entity_edit_icon_crop[{$input_name}][guid]",
149  'value' => $entity->guid,
150  ]);
152  '#type' => 'hidden',
153  'name' => "_entity_edit_icon_crop[{$input_name}][type]",
154  'value' => $icon_type,
155  ]);
156 }
157 
158 echo elgg_format_element('div', ['class' => ['elgg-entity-edit-icon-crop-input', 'hidden']], $input);
159 
160 echo elgg_view('entity/edit/icon/crop_messages', $vars);
161 
162 ?>
163 <script>
164  require(['entity/edit/icon/crop'], function(Cropper) {
165  var cropper = new Cropper();
166 
167  cropper.init('input[type="file"][name="<?php echo elgg_extract('name', $vars, 'icon'); ?>"]');
168  });
169 </script>
if($entity instanceof ElggEntity) if(!isset($cropper_data['aspectRatio'])) $img_url
Definition: crop.php:115
$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.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$value
Definition: generic.php:51
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
$input
Definition: crop.php:136
$vars['head']
Definition: html.php:24
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:129
$site name
Definition: settings.php:15