forked from krivochenko/yii2-cropper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget.php
113 lines (98 loc) · 3.24 KB
/
Widget.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
<?php
namespace budyaga\cropper;
use budyaga\cropper\assets\CropperAsset;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use Yii;
class Widget extends InputWidget
{
public $uploadParameter = 'file';
public $width = 200;
public $height = 200;
public $label = '';
public $uploadUrl;
public $noPhotoImage = '';
public $maxSize = 2097152;
public $thumbnailWidth = 300;
public $thumbnailHeight = 300;
public $cropAreaWidth = 300;
public $cropAreaHeight = 300;
public $extensions = 'jpeg, jpg, png, gif';
public $onCompleteJcrop;
public $pluginOptions = [];
public $aspectRatio = null;
/**
* @inheritdoc
*/
public function init()
{
// blah
parent::init();
self::registerTranslations();
if ($this->uploadUrl === null) {
throw new InvalidConfigException(Yii::t('cropper', 'MISSING_ATTRIBUTE', ['attribute' => 'uploadUrl']));
} else {
$this->uploadUrl = rtrim(Yii::getAlias($this->uploadUrl), '/') . '/';
}
if ($this->label == '') {
$this->label = Yii::t('cropper', 'DEFAULT_LABEL');
}
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerClientAssets();
return $this->render('widget', [
'model' => $this->model,
'widget' => $this
]);
}
/**
* Register widget asset.
*/
public function registerClientAssets()
{
$view = $this->getView();
$assets = CropperAsset::register($view);
if ($this->noPhotoImage == '') {
$this->noPhotoImage = $assets->baseUrl . '/img/nophoto.png';
}
$settings = array_merge([
'url' => $this->uploadUrl,
'name' => $this->uploadParameter,
'maxSize' => $this->maxSize / 1024,
'allowedExtensions' => explode(', ', $this->extensions),
'size_error_text' => Yii::t('cropper', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]),
'ext_error_text' => Yii::t('cropper', 'EXTENSION_ERROR', ['formats' => $this->extensions]),
'accept' => 'image/*',
], $this->pluginOptions);
if(is_numeric($this->aspectRatio)) {
$settings['aspectRatio'] = $this->aspectRatio;
}
if ($this->onCompleteJcrop)
$settings['onCompleteJcrop'] = $this->onCompleteJcrop;
$view->registerJs(
'jQuery("#' . $this->options['id'] . '").parent().find(".new-photo-area").cropper(' . Json::encode($settings) . ', ' . $this->width . ', ' . $this->height . ');',
$view::POS_READY
);
}
/**
* Register widget translations.
*/
public static function registerTranslations()
{
if (!isset(Yii::$app->i18n->translations['cropper']) && !isset(Yii::$app->i18n->translations['cropper/*'])) {
Yii::$app->i18n->translations['cropper'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@budyaga/cropper/messages',
'forceTranslation' => true,
'fileMap' => [
'cropper' => 'cropper.php'
]
];
}
}
}