/** * @return string */ public function actionUploadImage() { $returnData = []; $className = \Yii::$app->request->get('model_name'); $attribute = \Yii::$app->request->get('attribute'); if ($className && $attribute) { $model = new $className(); $modelName = $model->formName(); $files = UploadedFile::getInstances($model, $attribute); foreach ($files as $file) { $originalName = $file->baseName . '.' . $file->extension; $fileId = FPM::transfer()->saveUploadedFile($file); if ($fileId) { $existModelId = \Yii::$app->request->post('id'); $tempSign = \Yii::$app->request->post('sign'); $savedImage = EntityToFile::add($modelName, $existModelId, $fileId, $tempSign, \Yii::$app->request->get('entity_attribute')); if (!$savedImage) { $returnData['error'][] = 'Не получилось связать файл ' . $originalName . ' с моделью'; } else { $returnData = ['deleteUrl' => ImagesUploadModel::deleteImageUrl(['id' => $savedImage->id]), 'cropUrl' => ImagesUploadModel::getCropUrl(['id' => $savedImage->id]), 'id' => $savedImage->id, 'imgId' => $savedImage->file_id]; } } else { $returnData['error'][] = 'Не получилось сохранить файл ' . $originalName; } } } return Json::encode($returnData); }
public function run() { if (!$this->model || !$this->attribute) { return null; } $extraData = $this->model->isNewRecord ? ['sign' => $this->model->sign] : ['id' => $this->model->id]; $previewImages = []; $previewImagesConfig = []; $existModelImages = EntityToFile::find()->where('entity_model_name = :emn', [':emn' => $this->model->formName()]); if ($this->saveAttribute !== null) { $existModelImages->andWhere('attribute = :attr', [':attr' => $this->saveAttribute]); } $existModelImages = $this->model->isNewRecord ? $existModelImages->andWhere('temp_sign = :ts', [':ts' => $this->model->sign]) : $existModelImages->andWhere('entity_model_id = :id', [':id' => $this->model->id]); $existModelImages = $existModelImages->orderBy('position DESC')->all(); /** * @var \common\models\EntityToFile $file */ foreach ($existModelImages as $image) { $fileName = $image->file->base_name . '.' . $image->file->extension; $previewImages[] = Html::img(FPM::originalSrc($image->file_id), ['class' => 'file-preview-image', 'id' => 'preview-image-' . $image->file_id]); $previewImagesConfig[] = ['caption' => $fileName, 'width' => '120px', 'url' => ImagesUploadModel::deleteImageUrl(['id' => $image->id]), 'key' => $image->id]; } $output = Html::hiddenInput('urlForSorting', ImagesUploadModel::sortImagesUrl(), ['id' => 'urlForSorting']); $output .= Html::hiddenInput('aspectRatio', $this->aspectRatio, ['class' => 'aspect-ratio']); $output .= FileInput::widget(['model' => $this->model, 'attribute' => $this->attribute, 'options' => ['multiple' => $this->multiple, 'accept' => 'image/*'], 'pluginOptions' => ['dropZoneEnabled' => false, 'browseClass' => 'btn btn-success', 'browseIcon' => '<i class="glyphicon glyphicon-picture"></i> ', 'removeClass' => "btn btn-danger", 'removeIcon' => '<i class="glyphicon glyphicon-trash"></i> ', 'uploadClass' => "btn btn-info", 'uploadIcon' => '<i class="glyphicon glyphicon-upload"></i> ', 'uploadUrl' => Url::to($this->uploadUrl), 'allowedFileTypes' => ['image'], 'allowedPreviewTypes' => ['image'], 'uploadExtraData' => $extraData, 'initialPreview' => $previewImages, 'initialPreviewConfig' => $previewImagesConfig, 'overwriteInitial' => false, 'showRemove' => false, 'otherActionButtons' => $this->render('_crop_button'), 'fileActionSettings' => ['indicatorSuccess' => $this->render('_success_buttons_template')]], 'pluginEvents' => ['fileuploaded' => 'function(event, data, previewId, index) { var elem = $("#"+previewId).find(".file-actions .file-upload-indicator .kv-file-remove"); var cropElem = $("#"+previewId).find(".file-actions .crop-link"); var img = $("#"+previewId).find("img"); //id for cropped image replace img.attr("id", "preview-image-"+data.response.imgId); elem.attr("data-url", data.response.deleteUrl); elem.attr("data-key", data.response.id); cropElem.attr("href", data.response.cropUrl); //Resort images saveSort(); //Fix crop url for old images fixMultiUploadImageCropUrl(); }', 'fileloaded' => 'function(file, previewId, index, reader) { //Fix url for old images fixMultiUploadImageCropUrl(); }']]); $output .= '<br>'; $output .= $this->render('_modal'); return $output; }
</div> <div class="img-preview col-sm-6"></div> <div class="col-sm-12"> <?php echo Html::hiddenInput('dataX', null, ['id' => 'dataX']); ?> <?php echo Html::hiddenInput('dataY', null, ['id' => 'dataY']); ?> <?php echo Html::hiddenInput('dataHeight', null, ['id' => 'dataHeight']); ?> <?php echo Html::hiddenInput('dataWidth', null, ['id' => 'dataWidth']); ?> <?php echo Html::hiddenInput('fileId', $id, ['id' => 'fileId']); ?> </div> <div class="col-sm-8 margined centered"> <?php echo Html::a('Сохранить', ImagesUploadModel::getSaveCroppedImageUrl(), ['class' => 'btn btn-info save-cropped']); ?> </div> </div>
/** * @return array */ public function getFormConfig() { return ['label' => ['type' => ActiveFormBuilder::INPUT_TEXT, 'options' => ['maxlength' => true, 'class' => 's_name']], 'alias' => ['type' => ActiveFormBuilder::INPUT_TEXT, 'options' => ['maxlength' => true, 'class' => 's_alias']], 'category_id' => ['type' => ActiveFormBuilder::INPUT_DROPDOWN_LIST, 'items' => \common\models\ArticleCategory::getItems(), 'options' => ['prompt' => '']], 'announce' => ['type' => ActiveFormBuilder::INPUT_WIDGET, 'widgetClass' => \backend\components\ImperaviContent::className(), 'options' => ['model' => $this, 'attribute' => 'announce']], 'content' => ['type' => ActiveFormBuilder::INPUT_WIDGET, 'widgetClass' => \backend\components\ImperaviContent::className(), 'options' => ['model' => $this, 'attribute' => 'content']], 'date' => ['type' => ActiveFormBuilder::INPUT_WIDGET, 'widgetClass' => \metalguardian\dateTimePicker\Widget::className(), 'options' => ['mode' => \metalguardian\dateTimePicker\Widget::MODE_DATE, 'options' => ['class' => 'form-control']]], 'titleImage' => ['type' => ActiveFormBuilder::INPUT_RAW, 'value' => ImageUpload::widget(['model' => $this, 'attribute' => 'titleImage', 'saveAttribute' => EntityToFile::TYPE_ARTICLE_TITLE_IMAGE, 'multiple' => false, 'aspectRatio' => 848 / 350, 'uploadUrl' => ImagesUploadModel::uploadUrl(['model_name' => static::className(), 'attribute' => 'titleImage', 'entity_attribute' => EntityToFile::TYPE_ARTICLE_TITLE_IMAGE])])], 'tags' => ['type' => ActiveFormBuilder::INPUT_TEXT, 'options' => ['maxlength' => true]], 'views' => ['type' => ActiveFormBuilder::INPUT_TEXT], 'published' => ['type' => ActiveFormBuilder::INPUT_CHECKBOX], 'position' => ['type' => ActiveFormBuilder::INPUT_TEXT], 'sign' => ['type' => ActiveFormBuilder::INPUT_RAW, 'value' => Html::activeHiddenInput($this, 'sign')]]; }
<?php use backend\modules\imagesUpload\models\ImagesUploadModel; ?> <a class="crop-link btn btn-xs btn-default pull-right" data-toggle="modal" href="<?php echo ImagesUploadModel::getCropUrl(['id' => '']); ?> " {dataKey} data-target=".modal-hidden"> <i class="glyphicon glyphicon glyphicon-scissors file-icon-large text-success"></i> </a>