gpt4 book ai didi

javascript - 如何在javascript客户端将图像解析为ICO格式

转载 作者:行者123 更新时间:2023-12-01 15:38:12 25 4
gpt4 key购买 nike

我想在前端使用 javascript 将图像 (png/jpeg) 转换为 ICO。
在网上搜索时,我在 github 上看到了这段代码:https://gist.github.com/twolfson/7656254但不幸的是它使用 fs nodejs 的模块(+ 代码很难理解)。
有人可以告诉我应该搜索什么/或通过在前端使用 javascript 将 png/jpeg 转换为 ico 的方法吗?
我尝试过的替代方案?
使用了这个仓库:https://github.com/fiahfy/ico-convert但他们使用尖锐和尖锐在客户端不支持

最佳答案

在谷歌上搜索,我得到 this Mozilla post , 附实例,提供以下代码转换为ICO格式(仅限火狐浏览器),

A way to convert a canvas to an ico (Mozilla only)

This uses -moz-parse to convert the canvas to ico. Windows XP doesn'tsupport converting from PNG to ico, so it uses bmp instead. A downloadlink is created by setting the download attribute. The value of thedownload attribute is the name it will use as the file name.


编码:
var canvas = document.getElementById('canvas');
var d = canvas.width;
ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(d / 2, 0);
ctx.lineTo(d, d);
ctx.lineTo(0, d);
ctx.closePath();
ctx.fillStyle = 'yellow';
ctx.fill();

function blobCallback(iconName) {
return function(b) {
var a = document.createElement('a');
a.textContent = 'Download';
document.body.appendChild(a);
a.style.display = 'block';
a.download = iconName + '.ico';
a.href = window.URL.createObjectURL(b);
}
}
canvas.toBlob(blobCallback('passThisString'),
'image/vnd.microsoft.icon',
'-moz-parse-options:format=bmp;bpp=32'
);
除此之外,我还发现了 没有其他方法将 png/jpeg 转换为 ICO 格式。或者,您可以使用以下任何模块在服务器端进行转换:
  • to-ico
  • image-to-icon
  • png-to-ico
  • 关于javascript - 如何在javascript客户端将图像解析为ICO格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63558462/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com