JS generates image processing method in Word

First function is to ask the front desk to export Word, but the front and back is separated, and there is a background, so there is a cross-domain problem.

The export text is no problem (jQuery.word), but there is a problem with the export image:

The picture is in the form of the link to Word, so if it is a VPN The website will have a generated word in the case where there is no VPN, and edit must be enabled if there is a VPN.

Solution: Convert pictures into DATA URL format, then export.

The detailed code is as follows:

function changeimgtodataURL () {var charimg = document.all (“exportdom”). GetElementsBytagname (” IMG “); var imgurls =” “; for (var i = 0; i
 The above JS generates the picture processing method is to share it with you. All content, I hope I can give you a reference, I also hope that everyone will support Tumi Cloud.  < charImg.length; i++) { 
   var imgURL = charImg[i].currentSrc; 
   getBase64(imgURL,charImg[i]);
 } 
}
function getBase64(url,charImg){
  var Img = new Image();
  Img.crossOrigin = "Anonymous";//跨域必须使用,且后台也得设置允许跨域
  dataURL='';
  Img.src=url;
  Img.onload=function(){ //要先确保图片完整获取到,这是个异步事件
   var canvas = document.createElement("canvas"), //创建canvas元素
   width=Img.width, //确保canvas的尺寸和图片一样
   height=Img.height;
   canvas.width=width;
   canvas.height=height;
   canvas.getContext("2d").drawImage(Img,0,0,width,height); //将图片绘制到canvas中
   dataURL=canvas.toDataURL('image/jpg'); //转换图片为dataURL
   condataurl?condataurl(dataURL,charImg):null; //调用回调函数
  };
} 
function condataurl(dataURL,charImg){
 charImg.src=dataURL;
 //console.log(charImg);
}                    
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment