-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEJcrop.php
122 lines (109 loc) · 3.57 KB
/
EJcrop.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Jcrop Yii extension
*
* Select a cropping area fro an image using the Jcrop jQuery tool and crop
* it using PHP's GD functions.
*
* @copyright © Digitick <www.digitick.net> 2011
* @license GNU Lesser General Public License v3.0
* @author Jacques Basseck
* @author Ianaré Sévi
*
*/
Yii::import('zii.widgets.jui.CJuiWidget');
/**
* Base class.
*/
class EJcrop extends CJuiWidget
{
/**
* @var string URL of the picture to crop.
*/
public $url;
/**
* @var type Alternate text for the full size image image.
*/
public $alt;
/**
* @var array to set buttons options
*/
public $buttons = array();
/**
* @var string URL for the AJAX request
*/
public $ajaxUrl;
/**
* @var array Extra parameters to send with the AJAX request.
*/
public $ajaxParams = array();
public function init()
{
$assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets');
if (!isset($this->htmlOptions['id'])) {
$this->htmlOptions['id'] = $this->getId();
}
$this->id = $id = $this->htmlOptions['id'];
echo CHtml::image($this->url, $this->alt, $this->htmlOptions);
if (!empty($this->buttons)) {
echo "<div class='jcrop-buttons' id='{$this->id}_buttons'>" .
CHtml::button($this->buttons['start']['label'], $this->getHtmlOptions('start', 'inline'));
echo CHtml::button($this->buttons['crop']['label'], $this->getHtmlOptions('crop'));
echo CHtml::button($this->buttons['cancel']['label'], $this->getHtmlOptions('cancel')) .
'</div>';
}
echo CHtml::hiddenField($id . '_x', 0, array('class' => 'coords'));
echo CHtml::hiddenField($id . '_y', 0, array('class' => 'coords'));
echo CHtml::hiddenField($id . '_w', 0, array('class' => 'coords'));
echo CHtml::hiddenField($id . '_h', 0, array('class' => 'coords'));
echo CHtml::hiddenField($id . '_x2', 0, array('class' => 'coords'));
echo CHtml::hiddenField($id . '_y2', 0, array('class' => 'coords'));
$cls = Yii::app()->getClientScript();
$cls->registerScriptFile($assets . '/js/jquery.Jcrop.min.js');
$cls->registerScriptFile($assets . '/js/ejcrop.js', CClientScript::POS_HEAD);
$cls->registerCssFile($assets . '/css/jquery.Jcrop.css');
$this->options['onChange'] = "js:function(c) {ejcrop_getCoords(c,'{$id}'); ejcrop_showThumb(c,'{$id}');}";
$this->options['ajaxUrl'] = $this->ajaxUrl;
$this->options['ajaxParams'] = $this->ajaxParams;
$options = CJavaScript::encode($this->options);
if (!empty($this->buttons)) {
$js = "ejcrop_initWithButtons('{$id}', {$options});";
}
else {
$js = "jQuery('#{$id}').Jcrop({$options});";
}
$cls->registerScript(__CLASS__ . '#' . $id, $js, CClientScript::POS_READY);
}
/**
* Get the HTML options for the buttons.
*
* @param string $name button name
* @return array HTML options
*/
protected function getHtmlOptions($name, $display='none')
{
if (isset($this->buttons[$name]['htmlOptions'])) {
if (isset($this->buttons[$name]['htmlOptions']['id'])) {
throw new CException("id for jcrop '{$name}' button may not be set manually.");
}
$options = $this->buttons[$name]['htmlOptions'];
if (isset($options['class'])) {
$options['class'] = $options['class'] . " jcrop-{$name}";
}
else {
$options['class'] = "jcrop-{$name}";
}
if (isset($options['style'])) {
$options['style'] = $options['style'] . " display:{$display};";
}
else {
$options['style'] = "display:{$display};";
}
$options['id'] = $name . '_' . $this->id;
}
else {
$options = array('id' => $name . '_' . $this->id, 'style' => "display:{$display};", 'class' => "jcrop-{$name}");
}
return $options;
}
}