zoukankan      html  css  js  c++  java
  • canvas处理图片清晰度后使用七牛云上传

    引入七牛插件
    import * as qiniu from 'qiniu-js'
     
     
     
    // 处理图片
      private transformCanvas(file: any, cb: any) {
        const img = new Image()
        img.onload = function () {
          const canvas = document.createElement('canvas')
          const [w, h] = [img.width, img.height]
          canvas.width = w
          canvas.height = h
          const ctx: any = canvas.getContext('2d')
          ctx.drawImage(img, 0, 0, w, h)
          canvas.toBlob(
            function (blobObj) {
              cb(blobObj)
            },       
       'image/jpeg',
            0.8  //当请求图片格式为image/jpeg或者image/webp时用来指定图片展示质量。如果这个参数的值不在指定类型与范围之内,则使用默认值,其余参数将被忽略。
          )
        }
        img.src = window.URL.createObjectURL(file)
      }
     
     
     
    private uploadImg(uploader: any) {
        //获取76云key,token
        this.transformCanvas(uploader.file, (file: any) => {
          this.$service('upload-token', 'get').then(
            (res: any) => {
              const { key, token, qiniuDomain } = res.data
              //创建76云上传
              let observable = this.$qiniu.upload(file, key, token, {}, {})
              const that = this
              const subscription = observable.subscribe({
                next(res: any) {},
                error: (err: any) => {
                  this.$message.error(err.message)
                  subscription.unsubscribe()
                },
                complete(res: any) {
                  that.downloadUrl(res.key, uploader.file)
                }
              })
            }
          )
        })
      }
     
    // 下载图片
      private downloadUrl(key: any, file: any) {
        let params = {
          key: key
        }
        this.imgLoading = true
        this.$service(url', 'get', params).then(
          (res: any) => {
            this.fileLists.push({
              name: file.name,
              uid: file.uid,
              key: key,
              url: res.data
            })
            this.imgLoading = false
          }
        )
      }
     
     
  • 相关阅读:
    如何描述一个前端开发程序员
    解决电脑性能一般,打开webstorm后,电脑比较卡的问题
    HTML5的5个的新特性
    js 数组的拼接
    移动端性能
    如何学习前端
    实战:上亿数据如何秒查
    读懂Java中的Socket编程
    远程管理软件
    dedecms 安装后 管理后台ie假死 无响应的解决方法
  • 原文地址:https://www.cnblogs.com/lucy1111/p/13408305.html
Copyright © 2011-2022 走看看