GithubHelp home page GithubHelp logo

xuweilu / wx-image-uploader Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 6.0 34 KB

An image uploader for weixin mini program, could compress the original image and export image to base64 with weixin canvas, also fixed the wrong orientation issue when export image in iOS.

License: MIT License

JavaScript 100.00%
wxapp weixin image-compress image-base64 javacript

wx-image-uploader's Introduction

Transform image to base64 data in weixin mini program.

Use weixin mini program native wx.canvasGetImageData and upng API to transform image to base64.

Contents

Main

src/
├── index.js        (Main output)
├── checkOriention.js    (check if the output image's orientation is right)
├── getBase64Image.js    (get base64 data from an image)
└── upng-js (dependency)

Getting Started

Usage

Syntax

Because weixin mini program cannot use npm package, you should copy src directory to your program root directory.

var helper = require('../../src/index.js');

helper.checkOrientation(checkCanvasId);

helper.getBase64Image(workCanvasId, imgUrl, callback, imgWidth, imgHeight);

checkCanvasId

  • Type: String

  • Required

A hidden canvas just for checking if the orientation is right, should be like

<canvas style="width:2px;height:2px;visibility:hidden;" canvas-id="checkCanvas"></canvas>

Then the checkCanvasId is 'checkCanvas'

workCanvasId

  • Type: String
  • Required

This is the main canvas to handle all logics to transform the image, we should put our image into this canvas and then pass its id to the function.

 <canvas style="width: 300px; height: 200px;" canvas-id="workCanvas"></canvas>

Then the workCanvasId is 'workCanvas'

Example

<canvas style="width: 300px; height: 200px;" canvas-id="workCanvas"></canvas>
<canvas style="width:2px;height:2px;visibility:hidden;" canvas-id="checkCanvas"></canvas>

When first time load the app, run this command:

var helper = require('../../src/index.js');
Page({
...
  onReady: function() {
    // 首次进入,利用隐藏的judgeCanvas判断当前导出图像是否颠倒
    helper.checkOrientation('checkCanvas');
  },
     
  // Then use helper.getBase64Image to transfrom image anywhere you want.
  transformImage: function () {
    var _this = this;
    helper.getBase64Image('workCanvas', imgUrl, function(base64data){
        // work with the base64 data
        ...
    });
  }
...
})

全文请详见微信小程序图片压缩及base64化上传

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.