如何获取文件对象中图像的尺寸(宽度和高度)?

时间:2021-01-31 09:01:40

标签: javascript reactjs image promise size

我想获取文件对象中图像的尺寸(宽度和高度)。

我的问题是我尝试在上传文件期间执行此操作。但是在这一步,解析器是一个 Promise,它解析为只有流、文件名、mimetype 和编码属性的上传类型。

请听我的代码:

import { ApolloServer, gql } from 'apollo-server'

const server = new ApolloServer({
  typeDefs: gql`
    type Query {
      hello: String!
    }
    type UploadedFileResponse {
      filename: String!
      mimetype: String!
      encoding: String!
      url: String!
    }
    type Mutation {
      singleUpload(file: Upload!): UploadedFileResponse!
    }
  `,
  resolvers: {
    Query: {
      hello: () => "Hey!"
    },
    Mutation: {
      singleUpload: async (parent, { file }) => {
        const { stream, filename, mimetype, encoding } = await file;

        // Here I want to find the dimensions of my image file

        return { filename, mimetype, encoding, url: '' }
      }
    }
  }
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});

在此先感谢大家的帮助。

0 个答案:

没有答案