npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

picture-compressor

v1.1.0

Published

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

Downloads

53

Readme

picture-compressor

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

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

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

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

支持图片旋转

安装

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 选项

| name | type | 描述 | 是否必选 | 默认值 | | ------- | ------ | ----------------------------------------- | -------- | ------ | | img | String | 图片资源的 url 或者 base64 | Y | - | | width | Number | 生成图片的宽度 > 0 | Y | - | | height | Number | 生成图片的高度 > 0 | Y | - | | quality | Number | 生产图片质量 | N | 0.92 | | type | String | 生成图片类型(jpg or png) | N | jpg | | fit | String | 图片填充方式(scale:等比缩放 or fill:填充) | N | scale | | rotate | Number | 图片旋转(0,90,-90,180)度 | N | 0 |

returns 返回值

| name | type | 描述 | | ------ | ------ | ----------- | | img | String | 图片 base64 | | width | Number | 图片宽度 | | height | Number | 图片高度 |