コード例 #1
0
    public function run()
    {
        /* @var $model ActiveRecord|ImageUploaderBehavior */
        $model = $this->model;
        /* @var $behavior ImageUploaderBehavior */
        $behavior = $model->geImageBehavior();
        $wigetId = $this->id;
        $img_hint = '<div class="hint-block">';
        $img_hint .= 'Поддерживаемые форматы: ' . $behavior->getImageConfigParam('fileTypes') . '.
	Максимальный размер файла: ' . ceil($behavior->getImageConfigParam('maxFileSize') / 1024 / 1024) . 'мб.';
        $img_hint .= '</div><!-- /.hint-block -->';
        $imageVal = $model->getAttribute($behavior->getImageConfigParam('imageAttribute'));
        if (!$model->isNewRecord && !empty($imageVal)) {
            $img_hint .= '<div id="' . $wigetId . '" class="row">';
            $img_hint .= '<div class="col-md-12">';
            $img_hint .= Html::img($this->imageSrc, ['class' => 'pull-left uploaded-image-preview']);
            // $img_hint .= '<div class="pull-left" style="margin-left: 5px;">';
            $img_hint .= '<div class="btn-group-vertical pull-left"  style="margin-left: 5px;" role="group">';
            $img_hint .= Html::a('Delete <i class="glyphicon glyphicon-trash"></i>', '#', ['onclick' => new JsExpression('
                        if (!confirm("Вы действительно хотите удалить изображение?")) {
                            return false;
                        }

                        $.ajax({
                            type: "post",
                            cache: false,
                            url: "' . Url::to($this->deleteUrl) . '",
                            success: function() {
                                $("#' . $wigetId . '").remove();
                            }
                        });

                        return false;
                    '), 'class' => 'btn btn-danger']);
            if (!empty($this->cropUrl)) {
                Yii::$app->response->headers->add('Access-Control-Allow-Origin', '*');
                $pluginOptions = $this->cropPluginOptions;
                $validatorParams = $behavior->getImageConfigParam('imageValidatorParams');
                if (isset($validatorParams['minWidth'])) {
                    $pluginOptions['minCropBoxWidth'] = $validatorParams['minWidth'];
                }
                if (isset($validatorParams['minHeight'])) {
                    $pluginOptions['minCropBoxHeight'] = $validatorParams['minHeight'];
                }
                $img_hint .= Cropper::widget(['modal' => true, 'cropUrl' => $this->cropUrl, 'image' => ImageUploaderBehavior::addPostfixToFile($model->getImageSrc(), '_original'), 'aspectRatio' => $behavior->getImageConfigParam('aspectRatio'), 'pluginOptions' => $pluginOptions, 'ajaxOptions' => ['success' => new JsExpression(<<<JS
function(data) {
    // Refresh image src value to show new cropped image
    var img = \$("#{$wigetId} img.uploaded-image-preview");
    img.attr("src", img.attr("src").replace(/\\?.*/, '') + "?" + new Date().getTime());
}
JS
)]]);
            }
            $img_hint .= '</div><!-- /.btn-group -->';
            $img_hint .= '</div><!-- /.col-md-12 -->';
            $img_hint .= '</div><!-- /.row -->';
        }
        $imgAttr = $behavior->getImageConfigParam('imageAttribute');
        return Html::activeFileInput($model, $imgAttr) . $img_hint;
    }
コード例 #2
0
ファイル: user.php プロジェクト: kentuck13/blog-yii2
        <li><?php 
echo $form->field($model, 'username');
?>
</li>
        <li><?php 
echo $form->field($model, 'email');
?>
</li>
        <li><?php 
echo $form->field($model, 'name_fml');
?>
</li>
        <li><?php 
echo Cropper::widget(['modal' => true, 'aspectRatio' => 4 / 3, 'image' => Yii::$app->request->baseUrl . '/images/0_9b02a_8e1907d0_orig.jpg', 'pluginOptions' => ['minCropBoxWidth' => 400, 'minCropBoxHeight' => 300], 'ajaxOptions' => ['success' => new JsExpression(<<<JS
            function(data) {
                // data - your JSON response from [[cropUrl]]
                //\$("#myImage").attr("src", data.croppedImageSrc);
                console.log(data);
            }
JS
)]]);
?>
</li>
        <button class="btn btn-success">Сохранить</button>
        <br><br>
        <?php 
ActiveForm::end();
?>
        <a href="change-password" class="btn btn-danger">Сменить пароль</a><br><br>
    </ul>
</div>