compress_y

compress image

Usage no npm install needed!

<script type="module">
  import compressY from 'https://cdn.skypack.dev/compress_y';
</script>

README

参数解释:

  • maxWidth: 允许的最大图片宽度(必填, number类型)
  • maxHeight: 允许的最大图片高度(必填, number类型)
  • selector: 图片预览的容器ID(不需此功能传空 '', string类型)
  • ifCut: 是否剪裁图片(默认取图片中心,剪裁成正方形, boolean类型)
  • cb: 回调函数(用于获取数据, function),有两个参数:1.当前图片的base64值 2.多个图片的base64数组

使用方法:

  • 1.引入
  • 2.创建Compress对象
  • 3.调用Compress对象的readImg方法(需传入event事件对象)
  • 4.通过回调函数获取base64值或数组,进行后续操作
<!-- html -->
<input type="file" accept="image/jpg,image/jpeg,image/png,image/gif" onchange="uploadImg(event)">

<!-- 引入 -->
var Compress = require('compress_y')
or
import Compress from 'compress_y'

<!-- js -->
function uploadImg (event) {
   var compressObj = new Compress(300, 300, '', true, getBase64);
   compressObj.readImg(event);
}

function get getBase64 (base64, base64Arr) {
   // base64 string
   // an array of base64 string
}

功能解释: 当上传图片尺寸大于参数maxWidth, maxHeight任意一项,则对图片进行等比缩放。暂无自定义剪裁功能