aboutsummaryrefslogtreecommitdiff
path: root/views/default/forms/avatar/crop.php
blob: b97db939c2851d52969d9e2759050a43e20d2414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
 * Avatar crop form
 *
 * @uses $vars['entity']
 */

elgg_load_js('jquery.imgareaselect');
elgg_load_css('jquery.imgareaselect');

$master_image = $vars['entity']->getIconUrl('master');

?>
<div class="clearfix">
	<img id="user-avatar" class="mrl" src="<?php echo $master_image; ?>" alt="<?php echo elgg_echo('avatar'); ?>" />
</div>
<div class="elgg-foot">
<?php
$coords = array('x1', 'x2', 'y1', 'y2');
foreach ($coords as $coord) {
	echo elgg_view('input/hidden', array('name' => $coord, 'value' => $vars['entity']->$coord));
}

echo elgg_view('input/hidden', array('name' => 'guid', 'value' => $vars['entity']->guid));

echo elgg_view('input/submit', array('value' => elgg_echo('avatar:create')));

?>
</div>
<!-- grab the required js for icon cropping -->
<?php //@todo JS 1.8: no ?>
<script type="text/javascript">

	// display a preview of the users cropped section
	function preview(img, selection) {
		// catch for the first click on the image
		if (selection.width == 0 || selection.height == 0) {
			return;
		}

		var origWidth = $("#user-avatar").width(); //get the width of the users master photo
		var origHeight = $("#user-avatar").height(); //get the height of the users master photo
		var scaleX = 100 / selection.width;
		var scaleY = 100 / selection.height;
		$('#user-avatar-preview > img').css({
			width: Math.round(scaleX * origWidth) + 'px',
			height: Math.round(scaleY * origHeight) + 'px',
			marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
			marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
		});
	}

	function selectChange(img, selection) {
		// populate the form with the correct coordinates once a user has cropped their image
		$('input[name=x1]').val(selection.x1);
		$('input[name=x2]').val(selection.x2);
		$('input[name=y1]').val(selection.y1);
		$('input[name=y2]').val(selection.y2);
	}

	$(document).ready(function() {
		$('<div id="user-avatar-preview"><img src="<?php echo $master_image; ?>" /></div>').insertAfter($('#user-avatar'));
		$('<div id="user-avatar-preview-title"><label><?php echo elgg_echo('avatar:preview'); ?></label></div>').insertBefore($('#user-avatar-preview'));

		// this produces the coordinates
		$('#user-avatar').imgAreaSelect({ selectionOpacity: 0, onSelectEnd: selectChange });
		// show the preview
		$('#user-avatar').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview });
	});
</script>