美文网首页
uni-app图片压缩尺寸vue图片压缩尺寸

uni-app图片压缩尺寸vue图片压缩尺寸

作者: yichen_china | 来源:发表于2021-07-30 17:20 被阅读0次
<template>
    <view class="content">
        <view class="">
            {{'height:'+ h+'px;width:'+w+'px=='+'height:'+ mh+'px;width:'+mw+'px'}}
        </view>
        <button type="default" @click="onSinngle">单张压缩</button>
        <image :src="imageblod" :style="'height:'+ h+'px;width:'+w+'px'"></image>
        <image :src="minImageblod" :style="'height:'+ mh+'px;width:'+mw+'px'"></image>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                h: 0,
                w: 0,
                mh: 0,
                mw: 0,
                imageblod: null,
                minImageblod: null,
            }
        },
        methods: {
            onSinngle() {
                uni.chooseImage({
                    count: 1,
                    success: res => {
                        uni.showLoading({
                            title: '图片压缩中...',
                            mask: true
                        })
                        this.imageblod = res.tempFilePaths[0]
                        this.getImageInfo(res.tempFilePaths[0])
                    }
                })
            },

            // 上传图片
            uploadFile(filePath) {
                this.minImageblod = filePath
                let _this = this
                uni.uploadFile({
                    url: '/api/api/Profile/UploadImg',
                    name: 'file',
                    filePath,
                    formData: {},
                    success: (res) => {
                        let {
                            data
                        } = JSON.parse(res.data)
                        _this.imgResults.push(data)
                    },
                    fail: (err) => {
                        uni.showToast({
                            title: err.errMsg,
                            icon: 'none'
                        })
                    }
                });
            },
            // 获取图片信息
            getImageInfo(src) {
                let _this = this
                uni.getImageInfo({
                    src,
                    success(res) {
                        console.log('压缩前', res)
                        let canvasWidth = res.width //图片原始长宽
                        let canvasHeight = res.height
                        _this.h = canvasWidth
                        _this.w = canvasHeight
                        let img = new Image()
                        img.src = res.path
                        let canvas = document.createElement('canvas');
                        let ctx = canvas.getContext('2d')
                        canvas.width = canvasWidth / 4
                        canvas.height = canvasHeight / 4
                        _this.mh = canvas.height
                        _this.mw = canvas.width
                        ctx.drawImage(img, 0, 0, canvasWidth / 4, canvasHeight / 4)
                        canvas.toBlob(function(fileSrc) {
                            let imgSrc = window.URL.createObjectURL(fileSrc)
                            console.log('压缩后', imgSrc)
                            _this.uploadFile(imgSrc)
                        })
                    }
                })
            },
        }
    }
</script>

<style>
    /*  .image {
        width: 750rpx;
        height: 420rpx;
    } */
</style>

相关文章

  • uni-app图片压缩尺寸vue图片压缩尺寸

  • 图片压缩

    图片压缩质量压缩,比例压缩,采样率压缩,JPEG压缩思路:根据控件的尺寸或图片要放大显示的尺寸作为参数去压缩图片原...

  • iOS 压缩图片不失真

    下面这个方法的确能压缩图片,但是图片会变形 因为是按照指定尺寸压缩,导致图片压缩之后模糊,失真 按照屏幕的尺寸压缩...

  • vue(2019/7/15)

    vue实现照片选择或者拍照功能 照片格式校验, 图片质量压缩, 图片尺寸压缩, 图片离线保存, 图片base64编...

  • 压缩并用 Alamofire 上传多张图片

    附:压缩图片尺寸并压缩图片大小

  • iOS - 图片压缩

    1,图片尺寸压缩 方法举例: 2,尺寸不变,大小压缩 举例:ASI上传图片时, 3,QBImagePickerCo...

  • Android图片压缩

    主要有两种形式: 压缩图片的尺寸大小 压缩图片的质量(尺寸不变) 1.压缩尺寸 关键的在于设置options.in...

  • 图片压缩

    Android图片压缩常用的有质量压缩、尺寸压缩、采样率压缩以及通过JNI调用libjpeg库来进行压缩(尺寸压缩...

  • iOS 图片压缩方法

    两种图片压缩方法 两种图片压缩方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 通过...

  • iOS 图片压缩限制大小最优解

    iOS 图片压缩限制大小最优解 图片的两种压缩方法 1.1 压缩图片质量 1.2 压缩图片尺寸 压缩图片使图片文件...

网友评论

      本文标题:uni-app图片压缩尺寸vue图片压缩尺寸

      本文链接:https://www.haomeiwen.com/subject/uhdcvltx.html