Elgg  Version 4.3
crop.php
Go to the documentation of this file.
1 <?php
16 if (elgg_extract('cropper_enabled', $vars, false) === 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 // determin 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  // remove invalid values
54  $entity_coords = array_filter($entity_coords, function($value) {
55  return $value >= 0;
56  });
57 
58  // still enough for cropping
59  if (isset($entity_coords['x1'], $entity_coords['x2'], $entity_coords['y1'], $entity_coords['y2'])) {
60  $cropper_data['data'] = [
61  'x' => $entity_coords['x1'],
62  'y' => $entity_coords['y1'],
63  'width' => $entity_coords['x2'] - $entity_coords['x1'],
64  'height' => $entity_coords['y2'] - $entity_coords['y1'],
65  ];
66  }
67 }
68 
69 // detect cropping aspect ratio
70 if (!isset($cropper_data['aspectRatio'])) {
71  $detect_aspect_ratio = function($vars) use (&$cropper_data) {
72  $cropper_aspect_ratio_size = elgg_extract('cropper_aspect_ratio_size', $vars, 'master');
73  if ($cropper_aspect_ratio_size === false) {
74  return;
75  }
76 
77  $icon_type = elgg_extract('icon_type', $vars, 'icon');
78  $entity_type = elgg_extract('entity_type', $vars);
79  $entity_subtype = elgg_extract('entity_subtype', $vars);
80 
81  $sizes = elgg_get_icon_sizes($entity_type, $entity_subtype, $icon_type);
82  if (empty($sizes)) {
83  // no way to read the config
84  return;
85  }
86 
87  if (!isset($sizes[$cropper_aspect_ratio_size]) && $cropper_aspect_ratio_size !== 'master') {
88  // fallback to master if custom ratio is missing
89  $cropper_aspect_ratio_size = 'master';
90  }
91 
92  if (!isset($sizes[$cropper_aspect_ratio_size])) {
93  // return if ratio is not present
94  return;
95  }
96 
97  $width = (int) elgg_extract('w', $sizes[$cropper_aspect_ratio_size]);
98  $height = (int) elgg_extract('h', $sizes[$cropper_aspect_ratio_size]);
99 
100  if (empty($width) || empty($height)) {
101  return;
102  }
103 
104  $cropper_data['aspectRatio'] = $width / $height;
105  };
106  $detect_aspect_ratio($vars);
107 }
108 
109 $img_url = null;
110 if ($entity instanceof ElggEntity && $entity->hasIcon('master', $icon_type)) {
111  $img_url = $entity->getIconURL([
112  'size' => 'master',
113  'type' => $icon_type,
114  ]);
115 }
116 
118  'data-icon-cropper' => json_encode($cropper_data),
119  'src' => $img_url,
120 ]);
121 
122 echo elgg_format_element('div', ['class' => ['elgg-entity-edit-icon-crop-wrapper', 'hidden', 'mbm']], $img);
123 
124 $input ='';
125 foreach (['x1', 'y1', 'x2', 'y2'] as $coord) {
127  '#type' => 'hidden',
128  'name' => $coord,
129  'value' => elgg_extract($coord, $entity_coords),
130  ]);
131 }
132 
133 if (!empty($img_url)) {
135  '#type' => 'hidden',
136  'name' => '_entity_edit_icon_crop_guid',
137  'value' => $entity->guid,
138  ]);
140  '#type' => 'hidden',
141  'name' => '_entity_edit_icon_crop_type',
142  'value' => $icon_type,
143  ]);
145  '#type' => 'hidden',
146  'name' => '_entity_edit_icon_crop_input',
147  'value' => $input_name,
148  ]);
149 }
150 
151 echo elgg_format_element('div', ['class' => ['elgg-entity-edit-icon-crop-input', 'hidden']], $input);
152 
153 echo elgg_view('entity/edit/icon/crop_messages', $vars);
154 
155 ?>
156 <script>
157  require(['entity/edit/icon/crop'], function(Cropper) {
158  var cropper = new Cropper();
159 
160  cropper.init('input[type="file"][name="<?php echo elgg_extract('name', $vars, 'icon'); ?>"]');
161  });
162 </script>
if($entity instanceof ElggEntity) if(!isset($cropper_data['aspectRatio'])) $img_url
Definition: crop.php:109
$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:1143
$site name
Definition: settings.php:21
elgg_get_icon_sizes($entity_type=null, $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
if($entity instanceof ElggEntity &&$entity->hasIcon('master', $icon_type)) $img
Definition: crop.php:117
$value
Definition: generic.php:51
elgg_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:135
$data icon
Definition: default.php:28
elgg require
Throw an error if the required package isn&#39;t present.
Definition: deprecated.js:176
$entity
Definition: crop.php:22
$default_config
Definition: crop.php:27
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
$icon_type
Definition: crop.php:23
$cropper_data
Definition: crop.php:33
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$input
Definition: crop.php:124
elgg echo
Translates a string.
Definition: deprecated.js:530
$vars['head']
Definition: html.php:24
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:179