public function run() { $data = $this->model[$this->attribute]; //определяем картинку в зависимости от типа входных данных стринг/массив $imgSrc = ''; $coords = 'null'; if (is_array($data)) { if (array_key_exists('imgSrc', $data)) { $imgSrc = $data['imgSrc']; } if (array_key_exists('x1', $data) && $data['x1'] != '' && array_key_exists('y1', $data) && $data['y1'] != '' && array_key_exists('x2', $data) && $data['x2'] != '' && array_key_exists('y2', $data) && $data['y2'] != '') { $x1 = $data['x1']; $y1 = $data['y1']; $x2 = $data['x2']; $y2 = $data['y2']; $coords = '{"x1":' . $x1 . ',"y1":' . $y1 . ',"x2":' . $x2 . ',"y2":' . $y2 . '}'; } } elseif (!empty($data)) { $imgSrc = $data; } //рендерим блок с картинкой $previewId = $this->id . '_preview_img'; $previewImgClass = 'form-img-preview-crop-image'; $inputId = BaseHtml::getInputId($this->model, $this->attribute); if (empty($imgSrc)) { $preview = "<div id='{$previewId}'></div>"; } else { $preview = Html::tag('div', Html::img($imgSrc, ['class' => $previewImgClass . ' crop-image', 'data-forinput' => $inputId, 'data-coords' => $coords]), ['id' => $previewId]); } $this->template = strtr($this->template, ['{preview}' => $preview]); //рендерим инпуты $inputs = Html::activeTextInput($this->model, $this->attribute . '[imgSrc]', $this->options); $inputs .= Html::activeTextInput($this->model, $this->attribute . '[x1]', ['type' => 'hidden']); $inputs .= Html::activeTextInput($this->model, $this->attribute . '[y1]', ['type' => 'hidden']); $inputs .= Html::activeTextInput($this->model, $this->attribute . '[x2]', ['type' => 'hidden']); $inputs .= Html::activeTextInput($this->model, $this->attribute . '[y2]', ['type' => 'hidden']); $this->template = strtr($this->template, ['{inputs}' => $inputs]); parent::run(); //css и js $view = $this->getView(); //подключаем ресурсы CropImageAsset::register($view); //стили для отображения картинки $view->registerCss(".{$previewImgClass} {max-width:{$this->imgMaxWidth}; max-height:{$this->imgMaxHeight}}"); //подключаем файловый менеджер сервера $view->registerJs("mihaildev.elFinder.register(" . Json::encode($this->options['id']) . ",\n function(file, id){\n UnInitImageAreaSelect('{$inputId}');\n \$('#' + id).val(file.url);\n \$('#{$previewId}').html('<img class=\"{$previewImgClass} crop-image\" src=\"'+file.url+'\" data-forinput=\"{$inputId}\" >');\n \$('#{$previewId} .{$previewImgClass}').load(function(){ InitImageAreaSelect() });\n return true;}); "); }
public function run() { if ($this->hasModel()) { $src = \yii\helpers\Html::getAttributeValue($this->model, $this->attribute); } else { $src = $this->value; } $previewId = $this->id . '_preview_img'; if (empty($src)) { $preview = '<div id="' . $previewId . '"></div>'; } else { $preview = Html::tag('div', Html::img($src, ['class' => 'form-img-preview']), ['id' => $previewId]); } $replace['{preview}'] = $preview; $this->template = strtr($this->template, $replace); parent::run(); $this->getView()->registerJs("mihaildev.elFinder.register(" . Json::encode($this->options['id']) . ", function(file, id){ \$('#' + id).val(file.url); \$('#{$previewId}').html('<img class=\"form-img-preview\" src=\"'+file.url+'\">'); return true;}); "); $this->view->registerCss('.form-img-preview {max-width:100px;}'); }