压缩动态图片gif 和 静态图片的方法,返回Blob对象

server/2025/2/6 8:40:11/

1、定义--压缩动态图片方法

export const gifCompress = (file, url, max, min, times) => {

  if (window.FileReader) {

    let colors = 255

    let count = 0

    const fr = new FileReader()

    fr.readAsArrayBuffer(file)

    return new Promise((resolve) => {

      fr.onload = async(e) => {

        const fn = async() => {

          count++

          // eslint-disable-next-line no-undef

          const result = await gifmin(fr.result, colors) // 二进制文件流

          const blob = await new Blob([result], { // 转换成Blob对象

            type: 'application/octet-stream'

          })

          if (blob.size > max && count < times) {

            // eslint-disable-next-line require-atomic-updates

            colors = Math.floor(colors / 2)

            fn()

          } else {

            resolve({ blob, url })

          }

        }

        fn()

      }

    })

  } else {

    imgCompress(file, url)

  }

}

2、定义--压缩静态图片方法

export const staticCompress = (file, url, max, min, times) => {

  const img = new Image()

  img.src = url

  return new Promise((resolve) => {

    img.onload = async() => {

      const canvas = document.createElement('canvas')

      const { width: originWidth, height: originHeight } = img

      canvas.width = originWidth

      canvas.height = originHeight

      const ctx = canvas.getContext('2d')

      ctx.drawImage(img, 0, 0, originWidth, originHeight)

      let blob = await new Promise((resolve1) => {

        canvas.toBlob((res) => {

          resolve1(res)

        }, 'image/jpeg')

      })

      if (blob.size > max) {

        const resp = await compress(canvas, max, min, times)

        blob = resp.blob

      }

      resolve({ blob, url })

    }

  })

}

 3、图片压缩 

export const imgCompress = async(file, max = 200 * 1024, min = 10 * 1024, times = 3) => {

  const URL = window.URL || window.webkitURL

  const url = URL.createObjectURL(file)

  if (file.type === 'image/gif') {

    return gifCompress(file, url, max, min, times)

  } else {

    return staticCompress(file, url, max, min, times)

  }

}


http://www.ppmy.cn/server/165367.html

相关文章

计算机网络的基础设备

1. 集线器&#xff08;Hub&#xff09; 基本概念&#xff1a; 集线器是一种工作在OSI模型第一层&#xff08;物理层&#xff09;的设备&#xff0c;它将多个网络设备连接在一起形成一个星型拓扑结构。当任何设备发送数据时&#xff0c;集线器会将数据复制并传送到连接到其所有端…

DeepSeek的出现对全球GPT产业产生的冲击

引言 近年来&#xff0c;人工智能技术的迅猛发展推动了自然语言处理&#xff08;NLP&#xff09;领域的革命性进步。特别是以GPT&#xff08;Generative Pre-trained Transformer&#xff09;系列模型为代表的大规模预训练语言模型&#xff0c;已经在全球范围内引发了广泛关注…

二叉树原理及其C语言实现

目录 二叉树原理 应用场景 C语言实现 总结 扩展&#xff1a;平衡二叉树&#xff08;AVL 树&#xff09; 二叉树原理 二叉树是一种 非线性数据结构&#xff0c;是数据结构中的核心构造&#xff0c;每个节点最多有两个子节点&#xff0c;通常被称为左子节点&#xff08;left…

CommonAPI学习笔记-2

一. 概述 ​ 这篇文章主要是想整理并且分析CommonAPI代码生成工具根据fidl和fdepl配置文件生成出来的代码的结构和作用。 二. fidl ​ 用户根据业务需求在fidl文件中定义业务服务接口的结构以及自定义数据类型&#xff0c;然后使用core生成工具传入fidl文件生成该fidl的核心…

限流策略实战指南:从算法选择到阈值设置,打造高可用系统

前言 本文将深入探讨常见的限流算法及其适用场景&#xff0c;并详细解析基于 QPS 的限流方案。从如何设置合理的限流阈值&#xff0c;到请求被限流后的处理策略。 常见的限流算法 漏桶 核心原理 请求以任意速率进桶&#xff0c;以 恒定速率 出桶。若桶满则丢弃或排队等待适…

pytorch实现文本摘要

人工智能例子汇总&#xff1a;AI常见的算法和例子-CSDN博客 import numpy as npfrom modelscope.hub.snapshot_download import snapshot_download from transformers import BertTokenizer, BertModel import torch# 下载模型到本地目录 model_dir snapshot_download(tians…

Java项目: 基于SpringBoot+mybatis+maven+mysql实现的疾病防控综合管理系统(含源码+数据库+毕业论文)

一、项目简介 本项目是一套基于SpringBootmybatismavenmysql实现的疾病防控综合管理系统 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse或者idea 确保可以运行&#xff01; 该系统功能完善、…

DeepSeek R1 Ascend全国产化大模型推理本地化部署教程它来了

DeepSeekR1在昇腾Atlas300IPro使用mindie推理套件在本地化部署大模型推理 更详细的mindie使用指导可参考这篇文章mindie官方指导文档 年前、年后deepseek火了一把&#xff0c;现在还是非常厉害。确实给Z国长脸了。现在也有很多客户想跑一跑deepseek R1以下我将基于华为的Mind…