VUE- Select a local image, customize the ratio of the cropped image vue-cropper

Crop the picture, crop according to the ratio, step by step

1: el-upload selects local pictures (two steps of selecting local and uploading)

2: Get the picture selected by el-upload in the on-change callback method and display it on vueCropper ().

2.1: Before displaying vueCropper, use el-dialog bullet box to display.

3: Submit: Get the cropped image in the callback method realTime of vueCropper, convert it into base64, and then convert it into a file file and submit it to the server.

Look at the code steps

一:electric upload

el-upload link: Upload | Element Plus 

Briefly, this is a function of selecting local pictures to upload. The two functions of "selecting local pictures" and "uploading server" can be implemented together or separately. Because I need them here, they are implemented separately.

The selection and upload function component of elementplus is chosen here, of course, you can also write it yourself.

 <el-upload 
    accept="image/jpeg, image/gif, image/png, image/jpg" 
    ref='upload'           
    :action="uploadAction"          //上传方法,
    :auto-upload="false"            //是否自动上传文件。这里需要false
    :headers="uploadHeaders"     
    :limit="1" 
    :show-file-list="false"         //上传显示的列表
    :on-change="onChange"           //选择文件或上传文件成功或上传文件失败时的钩子功能
    >
    <el-button type="default">选择图片</el-button>
 </el-upload>
// 选择本地图片
        onChange(file, fileList) {
            this.$nextTick(() => {
                this.option.img = URL.createObjectURL(file.raw);
                this.$refs.upload.clearFiles();
                this.dialogVisibleImgs = true; // 控制显示弹框
            })
        },

Two: Get the picture and display it in the pop-up box (here are steps)

Get the img from the onchange method of el-upload. Let the el-dialog pop-up box display vuecropper.

Official website: https://github.com/xyxiao001/vue-cropper

and upload

<el-dialog class="cropperDialog" 
        :close-on-click-modal="false" 
        title="图片裁剪" 
        :visible.sync="dialogVisibleImgs"
        width="40%" 
        @close="close"
        >
            <div class="cropper-content" v-if="option.img">
                <div class="cropper" style="text-align: center;">
                    <vueCropper     
                        ref="cropper" 
                        :img="option.img" 
                        :output-size="option.size" 
                        :output-type="option.outputType"
                        :info="true" 
                        :full="option.full" 
                        :fixed="option.fixed" 
                        :fixed-number="option.fixedNumber"
                        :can-move="option.canMove" 
                        :can-move-box="option.canMoveBox" 
                        :fixed-box="option.fixedBox"
                        :original="option.original"      //上传图片按照原始比例渲染 原图裁切大小的需写这个
                        :auto-crop="option.autoCrop" 
                        :auto-crop-width="option.autoCropWidth"  // 默认生成裁图框宽度 原图裁切大小的需写这个" 
                        :auto-crop-height="option.autoCropHeight"  //     默认生成裁图框高度 原图裁切大小的需写这个
                        :center-box="option.centerBox" 
                        :high="option.high"
                        model="cover" 
                        :max-img-size="option.max" 
                        :info-true="option.infoTrue" 
                        @realTime="realTime"    //实时预览函数
                        >
                    </vueCropper>
                </div>


            </div>
            <div class="dialogbottom" style="display: flex; align-items: center; justify-content: space-between;">
                <div class="preview">
                    <!-- 这里传入封装的裁切比例 -->
                    <div class="title">封面预览 图片比例({
   
   { fixedNum[0] }}:{
   
   {fixedNum[1]}})</div>
                    <div class="preview_clumk">
                        <img :src="previewImageHeight" alt="" style="width:90px;"
                            object-fit="contain">
                    </div>
                </div>
                <div>
                    <el-button style="margin-left: 10px; margin-top: 20px; " type="success" :loading="loading"
                        @click="uploadEnd">上传并保存</el-button>
                </div>
            </div>
        </el-dialog>
// // 实时预览函数
        realTime(data) {
            // ①获取截图的 base64 数据
            this.$refs.cropper.getCropData((data) => {
                this.previewImageHeight = data;
            })
        },
        //将base64转换为file文件流
        base64toFile(dataurl, filename = 'file') {
            let arr = dataurl.split(',')
            let mime = arr[0].match(/:(.*?);/)[1]
            let suffix = mime.split('/')[1]
            let bstr = atob(arr[1])
            let n = bstr.length
            let u8arr = new Uint8Array(n)
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n)
            }
            return new File([u8arr], `${filename}.${suffix}`, {
                type: mime,
            })
        },
        // 自定义上传给后台,这里能最大限度自定义
        uploadEnd() {
              
            if (!this.option.img && this.option.img == '')
                return this.ME('请先选择图片')
            this.loading = true;

            if (this.previewImageHeight !== '') {
                const optionImg = this.base64toFile(this.previewImageHeight);
                const formData = new FormData()
                formData.append('file', optionImg);

                this.$api.Media.Image.Upload(formData).then((res) => {
                    this.loading = false;
                    this.imageUrl = res.data.url;
                    this.$message.success('上传成功');
                    this.$emit("input", this.imageUrl);
                    this.close();

                }).catch(() => {
                    this.loading = false;

                })
            }
        }

.sync:https://www.cnblogs.com/weiziyu/p/12012498.html

Guess you like

Origin blog.csdn.net/qq_27909209/article/details/131786130