1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dominus77\jcrop; |
4
|
|
|
|
5
|
|
|
use yii\base\Widget; |
6
|
|
|
use yii\helpers\Json; |
7
|
|
|
use yii\helpers\Html; |
8
|
|
|
use yii\web\JsExpression; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class JCrop |
12
|
|
|
* @package dominus77\jcrop |
13
|
|
|
* |
14
|
|
|
* Jcrop is a powerful image cropping tool for jQuery. |
15
|
|
|
* @see http://jcrop.org/ |
16
|
|
|
* |
17
|
|
|
* <?= \dominus77\jcrop\JCrop::widget([ |
18
|
|
|
* 'image' => Yii::getAlias('@web/uploads/jcrop/image1.jpg'), |
19
|
|
|
* 'pluginOptions' => [...], // see http://jcrop.org/doc/options |
20
|
|
|
* 'callBack' => "function(){}", |
21
|
|
|
* ]); ?> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
class JCrop extends Widget |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* id image |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $selector; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $image; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public $imageOptions = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Plugin options Jcrop |
44
|
|
|
* @see http://jcrop.org/doc/options |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
public $pluginOptions = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @see http://jcrop.org/doc/api |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $callBack = 'function(){}'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
1 |
|
public function init() |
59
|
|
|
{ |
60
|
1 |
|
parent::init(); |
61
|
1 |
|
if (empty($this->selector)) { |
62
|
1 |
|
$this->selector = $this->id; |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
1 |
|
public function run() |
70
|
|
|
{ |
71
|
1 |
|
if (!empty($this->image)) { |
72
|
1 |
|
$this->registerClientScript(); |
73
|
1 |
|
$this->imageOptions['id'] = $this->selector; |
74
|
1 |
|
echo Html::img($this->image, $this->imageOptions); |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Register resource |
80
|
|
|
*/ |
81
|
1 |
|
public function registerClientScript() |
82
|
|
|
{ |
83
|
1 |
|
$options = empty($this->pluginOptions) ? '{}' : Json::encode($this->pluginOptions); |
84
|
1 |
|
$view = $this->getView(); |
85
|
1 |
|
JCropAsset::register($view); |
86
|
1 |
|
$selector = '#' . $this->selector; |
87
|
1 |
|
$js = new JsExpression(" |
88
|
1 |
|
$('{$selector}').Jcrop({$options},{$this->callBack}); |
89
|
|
|
"); |
90
|
1 |
|
$view->registerJs($js); |
91
|
1 |
|
} |
92
|
|
|
} |
93
|
|
|
|