h5上傳圖片壓縮

涉及知識點(diǎn)

  • uniapp項(xiàng)目h5,涉及到前端圖片壓縮上傳
  • uni.chooseImage()
  • canvas壓縮
<template>
    <view class="uploadImages" @click="upImg()">
        <view class="defaultBg" style="width: 280rpx;" v-if="!isShow">
              點(diǎn)擊上傳  
        </view>
        <img v-if="isShow" :src="nFileBase64">
    </view>

  <helang-compress ref="helangCompress" :helangCompressType="uploadImgType"></helang-compress>

</template>
<script>
import helangCompress from '../helang-compress/helang-compress'

    components: {
      helangCompress,
     },
    props: ["uploadImgType"],
    export default {
        data() {
            return {
                nFileBase64: "",
            }
        },
        methods: {
            upImg() {
                var cthis = this;
                uni.chooseImage({
                    count: 1,
                    sizeType: ['compressed'], //此處指定是原圖還是壓縮圖
                    sourceType: ['album'], //從相冊選還是相機(jī)拍照
                    success(res) {
                        uni.showLoading()
                        cthis.$refs.helangCompress.compress(res.tempFiles[0], async (base64Codes) => {
                            const FileBase64 = base64Codes.replace(/^data:image\/\w+;base64,/, "")
                            cthis[`${type}FileBase64`] = base64Codes;
                            cthis.$emit("success", {
                                FileBase64: FileBase64,
                                FileExt: base64Codes.split(";")[0].split("/")[1],
                                type
                            })

                            setTimeout(function() {
                                uni.hideLoading();
                            }, 200);
                        })
                    },

                })
            }
        }
    }
</script>
<template>
    <view class="compress">
        <canvas :style="{ width: canvasSize.width,height: canvasSize.height}" :canvas-id="helangCompressType"></canvas>
    </view>
</template>

<script>
    export default {
        props:["helangCompressType"],
        data() {
            return {
                canvasSize: {
                    width: 0,
                    height: 0
                }
            }
        },
        methods: {
            // 壓縮
            compress(file, objDiv) {
                var ready = new FileReader();
                ready.readAsDataURL(file);
                let _this = this;
                ready.onload = function() {
                    var fileResult = this.result;
                    _this.canvasDataURL(fileResult, objDiv)
                }
            },
            canvasDataURL(path, callback) {
                var img = new Image();
                img.src = path;
                var objCompressed = {}
                var _this = this;
                img.onload = function() {
                    //默認(rèn)壓縮后圖片規(guī)格
                    var quality = 0.7;
                    var w = this.width;
                    var h = this.height;
                    //實(shí)際要求
                    if (w > h) {
                        let scale = h/w
                        h = 1300;
                        w = h/scale;
                    } else {
                        let scale = w/h
                        w = 1300;
                        h = w/scale;
                    }
                    // w = objCompressed.width || w;
                    // h = objCompressed.height || (w / scale);
                    //生成canvas
                    var canvas = document.createElement('canvas');
                    var ctx = canvas.getContext('2d');
                    // 創(chuàng)建屬性節(jié)點(diǎn)
                    var anw = document.createAttribute("width");
                    anw.nodeValue = w;
                    var anh = document.createAttribute("height");
                    anh.nodeValue = h;
                    canvas.setAttributeNode(anw);
                    canvas.setAttributeNode(anh);
                    ctx.drawImage(this, 0, 0, w, h);
            
                    var base64 = canvas.toDataURL('image/jpeg', quality);
                    // 回調(diào)函數(shù)返回base64的值
                    callback(base64);
                }
            },
        }
    }
</script>

<style lang="scss" scoped>
    .compress{
        position: fixed;
        width: 12px;
        height: 12px;
        overflow: hidden;
        top: -99999px;
        left: 0;
    }
</style>

參考文章:https://blog.csdn.net/baidu_41604826/article/details/11122460

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容