TaoLer/public/static/component/pear/module/convert.js

28 lines
668 B
JavaScript
Raw Normal View History

2023-03-16 22:40:15 +08:00
layui.define(['jquery', 'element'], function(exports) {
"use strict";
/**
*
* */
var MOD_NAME = 'convert',
$ = layui.jquery,
element = layui.element;
var convert = new function() {
// image 转 base64
this.imageToBase64 = function(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
var dataURL = canvas.toDataURL("image/"+ext);
return dataURL;
}
}
exports(MOD_NAME, convert);
});