Coder Social home page Coder Social logo

imagefile's Introduction

ImageFile

image util to handle image file, canvas/image/blob/file translate

USAGE

copy dist imageFile.umd.js for usage

<input type="file" accept="image/*" onchange="fileChange(event)">
<script src="../dist/imageFile.umd.js"></script>
<script>
function fileChange(ev) {
  let file = ev.target.files[0];
  ImageFile.getImageFileData(file, { width: 600, height: 800, cover: false }).then(({ blob, base64 }) => {
    let img = ImageFile.blobToImage(blob);
    img.style.width = '300px';
    console.log(blob, base64.length)
    document.body.appendChild(img);
  });
}
</script>

API

getImageFileData(file, option);

get image file input data, can compress size with option with and height

参数 类型 说明
file file type 文件类型
option object 配置项
width 宽度
height 高度
cover 是否覆盖整个区域,默认false
function fileChange(ev) {
  let file = ev.target.files[0];
  ImageFile.getImageFileData(file, { width: 300, height: 400, cover: true }).then(blob => {
    let img = ImageFile.blobToImage(blob);
    document.body.appendChild(img);
  })
}

blobToImage(blob)

blob translate to image, use for ImageFileData() result

参数 类型 说明
blob blob 二进制文件
let img = ImageFile.blobToImage(blob);

fileToCanvas(file, option);

file translate to canvas and image, get canvas and image

参数 类型 说明
file file type 文件类型
option object 配置项
width 宽度
height 高度
cover 是否覆盖整个区域,默认false
ImageFile.fileToCanvas(file, { width: 400, height: 400 }).then(({ canvas, image }) => {
  document.body.appendChild(canvas);
})

fileToImage(file);

file translate to image

参数 类型 说明
file file type 文件类型
ImageFile.fileToImage(file).then(img => {
    document.body.appendChild(img);
})

imageToCanvas(img);

image translate to canvas

参数 类型 说明
img image element 图片
let canvas = ImageFile.imageToCanvas(img);

canvasToImage(canvas);

canvas tranlate to image, return promise

参数 类型 说明
canvas canvas canvas
ImageFile.canvasToImage(cvs, 'image/png').then(canvas => {
  document.body.append(canvas);
})

canvasToFile(canvas);

canvas translate to file

let file = ImageFile.canvasToFile(cvs);

canvasToBase64(canvas, type = 'image/png', encoderOptions = '0.92');

canvas to base64

let base64 = ImageFile.canvasToBase64(cvs);

imageToBase64(img);

image translate to base64

let base64 = ImageFile.imageToBase64(img);

rotate(canvas, image, degree);

rotate image by canvas and return canvas

参数 类型 说明
canvas canvas 需要绘制的canvs
image img img元素
degree int 角度
ImageFile.rotate(cvs, img, degree);

imagefile's People

Contributors

kelen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.