vue-simple
Version:
Use Vue in the simplest and easiest way, contain more than one of plugins and other to do that, i hope you will like it.
63 lines (52 loc) • 1.92 kB
JavaScript
import _Promise from 'babel-runtime/core-js/promise';
import { MIME, Downloader } from '../BlobFile';
import { base64toBlob, objectIndexOf } from '../utils';
/**
* 将图片元素通过 canvas 读取 base64 编码
* @param {HTMLImageElement} imageElement
* @param {Number} [width] 新的宽度(不设置直接使用图片元素宽度)
* @param {Number} [height] 新的高度(不设置直接使用图片元素高度)
* @returns {string}
*/
function getBase64FromImage(imageElement, width, height) {
// width、height调用时传入具体像素值,控制大小 ,不传则默认图像大小
var canvas = document.createElement('canvas');
canvas.width = width ? width : imageElement.width;
canvas.height = height ? height : imageElement.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(imageElement, 0, 0, canvas.width, canvas.height);
return canvas.toDataURL();
}
/**
* 将远程图片读取成 base64 编码
* @param {String} url 图片地址
* @returns {Promise<any>}
*/
function loadImageBase64(url) {
var image = new Image();
image.crossOrigin = 'anonymous';
image.src = url;
return new _Promise(function (resolve, reject) {
// 加载成功
image.onload = function () {
resolve(getBase64FromImage(image)); // 将base64传给done上传处理
};
// 加载失败
image.onerror = function (error) {
reject(error);
};
});
}
/**
* 使用 base64 编码进行图片下载
* @param {String} base64 图片的 Base64 编码
* @param {String} [fullname] 文件名称(不需要扩展名)
*/
function downloadImageBase64(base64, fullname) {
var blob = base64toBlob(base64);
var type = objectIndexOf(MIME, blob.type);
var ext = type ? type.key : 'unknow';
var lastName = (fullname ? fullname : 'unknow') + '.' + ext;
Downloader(blob, lastName);
}
export { loadImageBase64, getBase64FromImage, downloadImageBase64 };