* @since 1.0 */ namespace app\components\admin\uploadcrop; use Yii; use yii\base\InvalidParamException; use yii\base\Widget; use yii\bootstrap4\Modal; use yii\helpers\Html; use yii\helpers\Json; use yii\widgets\ActiveForm; //use \dpodium\yii2\widget\upload\crop; class UploadCrop extends Widget { /** @var \yii\db\ActiveRecord */ var $model; /** @var string */ var $attribute; /* @var ActiveForm */ var $form = NULL; /** @var boolean */ var $enableClientValidation; /** @var array */ var $imageOptions; /** @var array */ var $jcropOptions = array(); /** @var integer */ var $maxSize = 400; var $title = 'Crop image'; var $changePhotoTitle = ''; var $imageSrc; /** * only call this method after a form closing and * when user hasn't used in the widget call the parameter $form * this adds to every form in the view the field validation. * * @param array $config * @return string * @throws \yii\base\InvalidConfigException */ static function manualValidation($config = []) { if (!array_key_exists('model', $config) || !array_key_exists('attribute', $config)) { throw new InvalidParamException('Config array must have a model and attribute.'); } $view = Yii::$app->getView(); $field_id = Html::getInputId($config['model'], $config['attribute']); $view->registerJs('$("#' . $field_id . '").urlParser("launchValidation");'); } /** * Renders the field. */ public function run() { if (is_null($this->imageOptions)) { $this->imageOptions = [ 'alt' => 'Crop this image' ]; } $this->imageOptions['id'] = Yii::$app->getSecurity()->generateRandomString(10); $inputField = Html::getInputId($this->model, $this->attribute, ['data-image_id' => $this->imageOptions['id']]); $default_jcropOptions = [ 'dashed' => FALSE, 'rotatable' => FALSE]; $this->jcropOptions = array_merge($default_jcropOptions, $this->jcropOptions); if (is_null($this->form)) { $this->form = new ActiveForm(); if (!is_null($this->enableClientValidation)) { $this->form->enableClientValidation = $this->enableClientValidation; } } $view = $this->getView(); $assets = \dpodium\yii2\widget\upload\crop\UploadCropAsset::register($view); echo Html::beginTag('div', ['class' => 'uploadcrop']); if ($this->imageSrc == '') { $img = $assets->baseUrl . '/img/nophoto.png'; $pimg = ''; } else { $img = $this->imageSrc; $pimg = $this->imageSrc; } echo $this->form->field($this->model, $this->attribute)->fileInput(['style'=>'cursor:pointer;'])->label(Html::img($img, ['width'=>300,'height'=>169])); echo Html::beginTag('span', ['class'=>'upload-title']); echo $this->changePhotoTitle; echo Html::endTag('span'); echo Html::beginTag('div', ['id' => 'preview-pane']); echo Html::beginTag('div', ['class' => 'preview-container']); echo Html::img($pimg, ['class' => 'preview_image', 'style'=>'width:300px;']); echo Html::endTag('div'); echo Html::endTag('div'); ?>
Modal::begin([ 'id' => 'cropper-modal-'. $this->imageOptions['id'], 'title' => '