V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
lovezww2011
V2EX  ›  分享创造

基于 png2icojs 开发的 png2ico 转换器

  •  
  •   lovezww2011 ·
    geeeeeeeek · 124 天前 · 1753 次点击
    这是一个创建于 124 天前的主题,其中的信息可能已经有所发展或是发生改变。

    由于鄙人经常开发各类网站,需要经常用到 ico 图标,故鄙人基于 github 开源项目开发了一个 png2ico 转换器,需要 png 转 ico 的同学可以参考之。

    预览地址

    https://png2ico.cn

    简介

    png2icojs 是一个简单便捷的 javascript es6 类库,运用它可以将 png 转换为 ico ICO file. 它可以运行在各类主流浏览器上面

    关键代码如下:

    import { PngIcoConverter } from "../src/png2icojs.js";
    
    // ...
    
    const inputs = [...files].map(file => ({
        png: file
    }));
    
    // Result is a Blob
    const resultBlob1 = await converter.convertToBlobAsync(inputs); // Default mime type is image/x-icon
    const resultBlob2 = await converter.convertToBlobAsync(inputs, "image/your-own-mime");
    
    // Result is an Uint8Array
    const resultArr = await converter.convertAsync(inputs);
    

    API 公开了具有许多受保护函数的 PngIcoConverter 类,因此您可以根据需要覆盖它们。

    方法

    PngIcoConverter 公开了以下方法: async convertToBlobSync (输入:IConvertInputItem[],mime=IcoMime ):Promise < Blob >; 将 PNG 文件转换为具有可选 mime 类型的 ICO Blob 。默认值:图像/x-icon 。 async convertAsync ( inputs:IConvertInputItem[]):Promise < Uint8Array >; 将 PNG 文件转换为 Uint8Array 。

    输入选项

    IConverInputItem 具有以下属性: png:png 文件。可以是 Blob 或 ArrayBuffer 。 bpp (可选,默认为 0 ):每像素位数。用于 ICO 图像的标题。在我的实验中,大多数应用程序只是完全忽略这个值,并使用 PNG 图像中的值。 ignoreSize (可选,默认为 false ):由于 ICO 的大小字节只有 1 个字节,图标的最大大小为 256px 。然而,我已经尝试制作一个 512px 的图标,到目前为止它是有效的。如果您的图像大小超过 256px ,库仍然会抛出错误。将此设置为 true 可忽略它。

    duke807
        1
    duke807  
       124 天前 via Android
    为何不直接用 png 图标
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3001 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:44 · PVG 16:44 · LAX 01:44 · JFK 04:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.