1.1.0 • Published 5 years ago

picture-compressor v1.1.0

Weekly downloads
8
License
ISC
Repository
github
Last release
5 years ago

picture-compressor

前端图片压缩工具,使用 canvas 对图片做压缩处理。

使用时,可以设置目标图片的宽高 返回的目标图片填充模式有两种

  • scale 等比例缩放,保证图片在设置的宽高之内完成显示

npm.io

  • fill 平铺显示,将图片尺寸设置为参数宽高,可能引起图片变形

npm.io

支持图片旋转

npm.io

安装

npm install picture-compressor --save

使用

<template>
  <div id="app">
    <input ref="upload" type="file" value="上传" @change="fileUpload" />
  </div>
</template>
<script>
  import pictureCompress from "picture-compressor"
  export default {
    name: "APP",
    methods: {
      fileUpload: function() {
        var file = this.$refs["upload"].files[0]
        var rotate = 90
        var reads = new FileReader()
        reads.readAsDataURL(file)
        reads.onload = function() {
          pictureCompress({
            img: this.result,
            width: 400,
            height: 400,
            rotate: rotate
          }).then(res => {
            var img = new Image()
            img.src = res.img
            document.body.appendChild(img)
          })
        }
      }
    }
  }
</script>

or

<input type="file" id="file" />
<script src="../dist/picture-compressor.js"></script>
<script>
  rotate = 0
  var files = document.getElementById("file")
  files.addEventListener("change", () => {
    var file = files.files[0]
    var reads = new FileReader()
    reads.readAsDataURL(file)
    reads.onload = function() {
      pictureCompress({
        img: this.result,
        width: 100,
        height: 100,
        rotate: rotate
      }).then(res => {
        var img = new Image()
        img.src = res.img
        document.body.appendChild(img)
      })
    }
  })
</script>

options 选项

nametype描述是否必选默认值
imgString图片资源的 url 或者 base64Y-
widthNumber生成图片的宽度 > 0Y-
heightNumber生成图片的高度 > 0Y-
qualityNumber生产图片质量N0.92
typeString生成图片类型(jpg or png)Njpg
fitString图片填充方式(scale:等比缩放 or fill:填充)Nscale
rotateNumber图片旋转(0,90,-90,180)度N0

returns 返回值

nametype描述
imgString图片 base64
widthNumber图片宽度
heightNumber图片高度
1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago