简体   繁体   English

如何从 javascript 中的 object 获取宽度和高度值

[英]how do I get the width and height values from this object in javascript

I am using dropzone and cropper to upload a file.我正在使用 dropzone 和cropper 上传文件。 I can't figure out how to reference the chosen images width and height.我不知道如何引用所选图像的宽度和高度。

This is the relevant code:这是相关代码:

Dropzone.options.myDropzone = {
    url: '{{ route('user.upload') }}',
    transformFile: function(file, done) {

        console.log(file);
        //console.log(file[height]);

        for (var property in file) {
            output = property + ': ' + file[property]+'; ';
            console.log(output);
        }

The console.log(file) line outputs this: console.log(file) 行输出如下:

在此处输入图像描述

So it has the height and width.所以它有高度和宽度。

The looping through the properties of file output this:遍历文件 output 的属性:

在此处输入图像描述

Anyone know how to access height and width here?有人知道如何在这里访问高度和宽度吗?

Thanks.谢谢。

EDIT编辑

Thanks to @kmoser this is the code that is working now.感谢@kmoser,这是现在正在运行的代码。

transformFile: function(file, done) {

    console.log(file['height']);

    var width = 0;
    var height = 0;
    var reader = new FileReader(); 
    reader.onload = (function(file) {
        var image = new Image();
        image.src = file.target.result;
        image.onload = function() { 
            height = this.height;
            width = this.width;
            console.log('1 '+width);
            console.log('1 '+height);
        }; 
    });
    reader.readAsDataURL(file)

    console.log('2 '+width);
    console.log('2 '+height);
    if (width > height)
    {
        console.log('wider');
    }
    else
    {
        console.log('tall');
    }

The only issue is that the console.log('2 '+width);唯一的问题是 console.log('2 '+width); outputs before the console.log('1 '+width);在 console.log('1 '+width); 之前输出

Can I make it wait?我可以让它等待吗?

EDIT 2编辑 2

Figured this out as well.这也想通了。

async function readFileAsDataURL(file) {
    let result_base64 = await new Promise((resolve) => {

        let reader = new FileReader(); 
        reader.onload = (function(file) {
            var image = new Image();
            image.src = file.target.result;
            image.onload = function(file) { 
                height = this.height;
                width = this.width;
                console.log('1 '+width);
                console.log('1 '+height);
                resolve(reader.result);
            }; 
        });
        reader.readAsDataURL(file)

    });
    return result_base64;
}

Dropzone.options.myDropzone = {
    url: '{{ route('user.upload') }}',
    transformFile: async function(file, done) {

        let result = await readFileAsDataURL(file);

        console.log('2 '+width);
        console.log('2 '+height);
        if (width > height)
        {
            console.log('wider');
        }
        else
        {
            console.log('tall');
        }

Works great!效果很好! Thanks for the help!谢谢您的帮助!

Final Solution最终解决方案

async function readFileAsDataURL(file) {
    let result_base64 = await new Promise((resolve) => {

        let reader = new FileReader(); 
        reader.onload = (function(file) {
            var image = new Image();
            image.src = file.target.result;
            image.onload = function(file) { 
                height = this.height;
                width = this.width;
                console.log('1 '+width);
                console.log('1 '+height);
                resolve(reader.result);
            }; 
        });
        reader.readAsDataURL(file)

    });
    return result_base64;
}

Dropzone.options.myDropzone = {
    url: '{{ route('user.upload') }}',
    transformFile: async function(file, done) {

        let result = await readFileAsDataURL(file);

        console.log('2 '+width);
        console.log('2 '+height);
        if (width > height)
        {
            console.log('wider');
        }
        else
        {
            console.log('tall');
        }

To call the attributes use dot调用属性使用点

console.log(file.height);

To access Height and Width, you can use either of them.要访问高度和宽度,您可以使用它们中的任何一个。

console.log(file['height']); 

OR或者

console.log(file.height);

Both will give same result.两者都会给出相同的结果。 Dot is used when you are sure of some value present in the attribute, while other way you can use when you are not sure whether it will be undefined or some value will be there当您确定属性中存在某些值时使用点,而当您不确定它是否未定义或是否存在某些值时可以使用其他方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取存储在javascript对象中的图像的宽度和高度? - How do I get the width and height of an image stored inside a javascript object? 如何使用javascript从文件获取图像尺寸(高度,宽度) - How do i get the image dimensions (height, width) from a file by using javascript 如何从 html 输入标签创建图像 object 并获取其宽度/高度? - How do I create an image object from an html input tag and get its width/height? 如何从 Javascript 中的嵌套对象获取值数组 - How do I get array of values from a nested object in Javascript 如何使用变量中的javascript设置对象的宽度和位置? - How do I set width and positioning of an object with javascript from a variable? 如何使用javascript获取宽度和高度? - How to get width and height with javascript? 如何在JavaScript中获取浏览器视口的宽度和高度并将其应用于CSS ID? - How Do I Get Browser Viewport Width & Height In Javascript And Apply To CSS ID's? 如何使用javascript获取div元素的高度和宽度的有用值? - How do I get a useful value for height and width of a div element using javascript? 如何通过CSS或JavaScript在iframe中调整宽度和高度? - How do I adjust width and height in iframe via css or javascript? JavaScript如何从XMLHttpRequest获取图像的宽度和高度 - JavaScript How to get an image width and height from an XMLHttpRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM