问题
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