【问题标题】:Nuxt SSR return loaded image dimensions to serverNuxt SSR 将加载的图像尺寸返回给服务器
【发布时间】:2019-05-16 21:15:57
【问题描述】:

我正在尝试在现有 img 元素上预览个人资料照片我的问题是新的图像尺寸在 img 加载函数之外变得未定义。

如何将这些尺寸传递给服务器,以便正确调整 img 元素的大小?

我也尝试使用 Vuex Store 来传递维度,但结果相同。

我还有一个监听调整大小的函数,在用户更改窗口后,img 会正确调整大小,但是我试图在没有事件触发器的情况下执行此操作。即使我尝试使用 jQuery 手动触发调整大小事件,它也不会调整大小。我的印象是,在调整大小甚至刷新尺寸之前,新的 img 源的尺寸没有被正确设置?

<b-img id="profilePhoto" v-bind="profile" :src="this.photo" class="profilePhoto" @change="handleResize()"></b-img>
export default {
        data() {
            return {
                errors:{},
                profile: {},
                fileDimensions: {},
                photo: '',
                form: this.$vform({
                    id: '',
                    name: '',
                    email: '',
                    password: '',
                    role: '',
                    bio: '',
                    photo: ''
                })
            }
        }
    }
getPhoto(e) {
    let file = e.target.files[0];
    if (typeof file !== 'undefined'){
        let reader = new FileReader()
        let limit = 1024 * 1024 * 2
        if (file.size > limit) {
            return false;
        }

        reader.onloadend = (file) => {
            this.form.photo = reader.result
            this.photo = this.form.photo
        }
        reader.readAsDataURL(file)

        $("<img/>",{
            load : function(){
                // will return dimensions fine if i use alert here
                this.fileDimensions = { width:this.width, height:this.height} ;
            },
            src  : window.URL.createObjectURL(file)
        });

        // becomes undefined outside of the load functions
        var aw = this.fileDimensions.width
        var ah = this.fileDimensions.height
        var ph = $('.profile').height()
        var pw = $('.profile').width()
        console.log(this.fileDimensions.width)
        if (ah>aw){
            this.profile = { width: ph, height: 'auto'}
        } else if (aw>ah) {
            this.profile = { width: 'auto', height: ph}
        } else {
            this.profile = { width: ph+10, height: pw+10}
        }
    }
}

我希望获得尺寸,以便我可以确定如何使用新的 src 设置 img 元素的宽度和高度属性,但是尺寸变得未定义。

【问题讨论】:

    标签: image server-side-rendering nuxt.js


    【解决方案1】:

    我通过另一篇文章弄清楚了。我必须创造一个承诺。 async await in image loading

    getDimensions(src){
        return new Promise((resolve,reject) => {
            let img = new Image()
            img.onload = () => resolve({ height: img.height, width: img.width })
            img.onerror = reject
            img.src = src
        })
    },
    getPhoto(e) {
        let file = e.target.files[0];
        if (typeof file !== 'undefined'){
            let reader = new FileReader()
            let limit = 1024 * 1024 * 2
            if (file.size > limit) {
                return false;
            }
    
            reader.onloadend = (file) => {
                this.form.photo = reader.result
                this.photo = this.form.photo
            }
            reader.readAsDataURL(file)
    
            this.getDimensions(URL.createObjectURL(file))
            .then((dimensions) => {
                if (process.client){
                    var ah = $('.profile').height()
                    var aw = $('.profile').width()
                    var ph = dimensions.height
                    var pw = dimensions.width
    
                    if (ph>pw){
                        this.profile = { width: ah+10, height: 'auto'}
                    } else if (pw>ph) {
                        this.profile = { width: 'auto', height: ah+10}
                    } else {
                        this.profile = { width: ah+10, height: aw+10}
                    }
                }
            })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2018-07-17
      • 1970-01-01
      相关资源
      最近更新 更多