/
Cropper.php
93 lines (70 loc) · 2.41 KB
/
Cropper.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
<?php
namespace servsol\cropper;
use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Url;
/**
* Cropper renders a jquery Cropper plugin for image crop.
*/
class Cropper extends InputWidget
{
/**
* @var boolean crop ratio
* Set the aspect ratio of the crop box.
* By default, the crop box is free ratio.
*/
public $aspectRatio;
public $clientOptions = [];
/**
* @var string url where uploaded files are stored
* if empty and has model, will be got from CropImageBehavior
*/
public $uploadUrl;
public $uploadCroppedUrl;
public $changeUrl;
public $name;
public $model;
public $attribute;
/**
* @inheritdoc
*/
public function run()
{
$jsOptions = [
'clientOptions' => $this->clientOptions,
];
$this->options['id'] = 'input-id';
if ($this->hasModel()) {
echo Html::activeInput('file', $this->model, $this->attribute, $this->options);
echo Html::img('#', ['id' => 'cropper-box']);
echo Html::button('Crop', ['id' => 'cropImage']);
$input_name = Html::getInputName($this->model, $this->attribute);
$input_id = Html::getInputId($this->model, $this->attribute);
echo Html::hiddenInput($input_name . '[file]', '', ['id' => $input_id . '_image']);
$jsOptions['model'] = $this->model;
$jsOptions['attribute'] = $this->attribute;
} else {
echo Html::fileInput($this->name, $this->value, $this->options);
echo Html::img('#', ['id' => 'cropper-box']);
echo Html::button('Crop', ['id' => 'cropImage']);
}
if ($this->uploadUrl)
$this->uploadUrl = \Yii::getAlias($this->uploadUrl);
$jsOptions['uploadUrl'] = $this->uploadUrl;
$jsOptions['uploadCroppedUrl'] = $this->uploadCroppedUrl;
$jsOptions['changeUrl'] = $this->changeUrl;
$jsOptions['name'] = $this->name;
$jsOptions['aspectRatio'] = $this->aspectRatio;
$this->registerPlugin($jsOptions);
}
/**
* Registers jqueryCrop
*/
protected function registerPlugin($options)
{
$view = $this->getView();
CropperAsset::register($view);
$id = $this->options['id'];
$view->registerJs("jQuery('#{$id}').cropImage(".json_encode($options).");");
}
}