使用
JavaScript,我想转换像这样的img标签:
<img width="11" height="14" src="http://mysite/file.gif" alt="File Icon">
进入一个像这样的dataurl:
<img width="11" height="14" src="data:image/gif;base64,R0lGODlhCwAOAMQfAP////7+/vj4+Hh4eHd3d/v7+/Dw8HV1dfLy8ubm5vX19e3t7fr6+nl5edra2nZ2dnx8fMHBwYODg/b29np6eujo6JGRkeHh4eTk5LCwsN3d3dfX13Jycp2dnevr6////yH5BAEAAB8ALAAAAAALAA4AAAVq4NFw1DNAX/o9imAsBtKpxKRd1+YEWUoIiUoiEWEAApIDMLGoRCyWiKThenkwDgeGMiggDLEXQkDoThCKNLpQDgjeAsY7MHgECgx8YR8oHwNHfwADBACGh4EDA4iGAYAEBAcQIg0Dk gcEIQA7" alt="File Icon">
这可能吗?
解决方法
首先,将图像加载到画布中
var canvas = document.createElement("canvas"); context = canvas.getContext('2d'); make_base(); function make_base() { base_image = new Image(); base_image.src = 'img/base.png'; base_image.onload = function(){ context.drawImage(base_image,100,100); } }
确保更新context.drawImage(base_image,100);适合您的应用的价值.
资料来源:https://stackoverflow.com/a/6011402/3969707
然后将画布转换为数据.
var jpegUrl = canvas.toDataURL("image/jpeg"); var pngUrl = canvas.toDataURL(); // PNG is the default