Converting a png/jpg to .ico in javascript

╄→гoц情女王★ 提交于 2021-02-05 12:30:42

问题


So i want a tool that generates a .ico file from a jpg/png. I have generated the jpg from a canvas using this code:

var img    = c.toDataURL("image/png");
document.write('<img src="'+img+'"/>');

Which takes from this canvas:

<canvas id="myCanvas" width="16" height="16">

So the qustion is; is it possible to convert the generated png to a ico?


回答1:


In Firefox you can do this directly from canvas:

// Make ICO files (Firefox only)
var ctx = c.getContext("2d");
ctx.arc(c.width>>1, c.height>>1, c.width>>1, 0, 6.28);
ctx.fill();

c.toBlob(function(blob) {
  console.log(blob)
}, 'image/vnd.microsoft.icon', '-moz-parse-options:format=bmp;bpp=32');
<canvas id=c width=32 height=32></canvas>

otherwise, to support other browsers you will have to build the ico file manually. See format description and for example this answer on how you can do that.



来源:https://stackoverflow.com/questions/48304752/converting-a-png-jpg-to-ico-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!