安装:
  npm i image-conversion --save 引入:
  <script src="https://cdn.jsdelivr.net/gh/WangYuLue/image-conversion/build/conversion.js"></script>
  or
  const imageConversion = require("image-conversion") 用例:
  <input id="demo" type="file" onchange="view()">

  一、将图像压缩到200kb

  function view(){
  const file = document.getElementById('demo').files[0];
  console.log(file);
  imageConversion.compressAccurately(file,200).then(res=>{
  //The res in the promise is a compressed Blob type (which can be treated as a File type) file;
  console.log(res);
  })
  }   // or use an async function
  async function view() {
  const file = document.getElementById('demo').files[0];
  console.log(file);
  const res = await imageConversion.compressAccurately(file,200)
  console.log(res);
  }

  二、图像精度0.9

  function view(){
  const file = document.getElementById('demo').files[0];
  console.log(file);
  imageConversion.compress(file,0.9).then(res=>{
  console.log(res);
  })
  }

imageConversion的一些方法:

  ①imagetoCanvas()----缩放和旋转图片

    例子:

    imageConversion.imagetoCanvas(image);

    //or

    imageConversion.imagetoCanvas(image,{
    width: 300, //result image's width
    height: 200, //result image's height
    orientation:2,//image rotation direction
    scale: 0.5, //the zoom ratio relative to the original image, range 0-10;
    //Setting config.scale will override the settings of
    //config.width and config.height;
    })

      

  ②dataURLtoFile()----确定转换后的图像类型:“Image/png”、“Image/jpeg”、“Image/gif”

  ③compress()----如果传入的是数字,表示图片质量;如果传入的是对象,表示将参数传递给imagetoCanvasdataURLtoFile方法

    例子:

    // number
    imageConversion.compress(file,0.8)     //or     // object
    imageConversion.compress(file,{
    quality: 0.8,
    type: "image/jpeg",//如果压缩PNG透明图像,请选择“Image/png”类型
    width: 300,
    height: 200,
     orientation:2,
     scale: 0.5,
    })

  ④compressAccurately()----如果是传入的是数字,表示指定压缩后图像的大小(KB);如果传入的是对象,表示将参数传递给imagetoCanvasdataURLtoFile方法

    例子:

      // number
    imageConversion.compressAccurately(file,100); //The compressed image size is 100kb
    // object
    imageConversion.compressAccurately(file,{
     size: 100, //The compressed image size is 100kb
     accuracy: 0.9,//the accuracy of image compression size,range 0.8-0.99,default 0.95;
     //this means if the picture size is set to 1000Kb and the
     //accuracy is 0.9, the image with the compression result
     //of 900Kb-1100Kb is considered acceptable;
     type: "image/jpeg",
     width: 300,
     height: 200,
     orientation:2,
     scale: 0.5,
    })

参考:GitHub

图片转换工具:http://www.wangyulue.com/assets/image-comversion/example/index.html

 

JS通过指定大小来压缩图片的更多相关文章

  1. GIF图片裁剪出指定大小的GIF图片

    前言 最近在博客后台上传图片的时候,突然发现上传gif图片的时候裁剪图片有问题.既没法裁剪gif指定区域的图片,又没法裁剪指定区域生成一个新的指定大小的gif图.本来想直接去找个裁剪的库直接放上去的, ...

  2. gulpfile.js(编译sass,压缩图片,自动刷新浏览器)

    var gulp = require('gulp'),     sass = require('gulp-sass'),     watch = require('gulp-watch'),      ...

  3. js使用canvas在前端压缩图片

    HTML代码: <input id="file" type="file"> JS代码: var eleFile = document.querySe ...

  4. 用gulp-imageisux智图api压缩图片

    ➣ 智图平台是什么? 智图是腾讯ISUX前端团队开发的一个专门用于图片压缩和图片格式转换的平台,其功能包括针对png,jpeg,gif等各类格式图片的压缩,以及为上传图片自动选择最优的图片格式.同时, ...

  5. 等比例压缩图片到指定的KB大小

    基本原理: 取原来的图片,长宽乘以比例,重新生成一张图片,获取这张图片的大小,如果还是超过预期大小,继续在此基础上乘以压缩比例,生成图片,直到达到预期 /** * @获取远程图片的体积大小 单位byt ...

  6. C# 图片超过指定大小将压缩到指定大小不失真

    using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...

  7. iOS学习——图片压缩到指定大小以内

    一.图片压缩简述 在我们开发过程中,有可能会遇到拍照.或者从相册中选择图片,要么单选或者多选,然后上传图片到服务器,一般情况下一张图片可能3-4M,如果类似微信朋友圈上传9张图片大约是 35M左右,如 ...

  8. C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

    class Program { static void Main(string[] args) {//G:\zhyue\backup\projects\Test\ConsoleApplication1 ...

  9. 【问题帖】压缩图片大小至指定Kb以下

    像PS,QQ影像等都有该功能,将图片大小压缩至指定kb以下. 我也来山寨一把,到目前为止,控制图片的大小,平时的解决方案通过分辨率和质量来控制的. 假定最后压缩的大小是100kb,那么在保证不大于10 ...

随机推荐

  1. 第02组 Beta冲刺(5/5)

    队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 验收了小程序的主要功能 制作Beta展示所需要用到的视频 制作Beta展示PPT 准备Beta答辩 提交记录(全组共用) ...

  2. 如何编写一个 SendFile 服务器

    如何编写一个 SendFile 服务器 前言 之前讨论零拷贝的时候,我们知道,两台机器之间传输文件,最快的方式就是 send file,众所周知,在 Java 中,该技术对应的则是 FileChann ...

  3. Django常用知识整理

    Django 的认识,面试题 1. 对Django的认识? #1.Django是走大而全的方向,它最出名的是其全自动化的管理后台:只需要使用起ORM,做简单的对象定义,它就能自动生成数据库结构.以及全 ...

  4. 此贴告诉你:为啥shell脚本人,不建议学python

    py很强大,我承认.但在运维方面,py不但不强大,还有硬伤.正因为有下述硬伤,所以我们运维,还是用shell多,用py极少.我看到用shell的人很多,你建议人用python,人说py是很好,但下一秒 ...

  5. CentOS7-安装后常见问题--ssh慢,汉字乱码gbk,-locale设置等

    00.ssh 慢问题解决修改:  [test@centos ~]$ sudo vi /etc/ssh/sshd_config /** 使用/命令查找 API 字符串*/ # GSSAPI option ...

  6. ubuntu 16.04 上编译和安装C++机器学习工具包mlpack并编写mlpack-config.cmake | tutorial to compile and install mplack on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/1cd6a04d/,欢迎阅读最新内容! tutorial to compile and install mplack on ubun ...

  7. oracle的instr()函数

    我们知道很多语言都提供了indexOf()和lastIndexOf()函数,以便能查找某个字符在某个字符串中的出现的位置和最后一次出现的位置. 但是Oracle没有提供这两个函数,事实上,它提供了一个 ...

  8. RootKit随手记(一)RootKit的驱动隐藏、读取配置、卸载安装

    边学习边更新这专题,随手记录一下用到的思路,给自己看的(所以读者看可能有些懵,不好意思...) RootKit随手记(一)RootKit的驱动隐藏.读取配置.卸载安装 一.驱动隐藏 1. 隐藏原理 一 ...

  9. 【微信原生支付】服务商模式-小微商户专属接口:小微商户新增对应APPID关联API

    文档地址:https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=20_3&index=3 这个接口比较特殊不需要nonce_st ...

  10. Winform(C#)中Chart控件鼠标点击显示波形上相应点对应坐标轴的x,y值

    方法一:鼠标点击波形 鼠标点击波形,显示点击位置的x,y值 private void chart1_MouseClick(object sender, MouseEventArgs e)  //cha ...