favicon
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenerate a complete set of favicons from the source image at and update the project's HTML with the appropriate link tags.
$1从路径为的源图片生成一套完整的网站图标(Favicon),并更新项目的HTML文件,添加对应的链接标签。
$1Prerequisites
前置条件
First, verify ImageMagick v7+ is installed by running:
bash
which magickIf not found, stop and instruct the user to install it:
- macOS:
brew install imagemagick - Linux:
sudo apt install imagemagick
首先,运行以下命令验证是否已安装ImageMagick v7+版本:
bash
which magick如果未找到,请停止操作并指导用户安装:
- macOS:
brew install imagemagick - Linux:
sudo apt install imagemagick
Step 1: Validate Source Image
步骤1:验证源图片
- Verify the source image exists at the provided path:
$1 - Check the file extension is a supported format (PNG, JPG, JPEG, SVG, WEBP, GIF)
- If the file doesn't exist or isn't a valid image format, report the error and stop
Note whether the source is an SVG file - if so, it will also be copied as .
favicon.svg- 验证提供的路径下是否存在源图片
$1 - 检查文件扩展名是否为支持的格式(PNG、JPG、JPEG、SVG、WEBP、GIF)
- 如果文件不存在或格式无效,报错并停止操作
注意源文件是否为SVG格式——如果是,还需要将其复制为。
favicon.svgStep 2: Detect Project Type and Static Assets Directory
步骤2:检测项目类型与静态资源目录
Detect the project type and determine where static assets should be placed. Check in this order:
| Framework | Detection | Static Assets Directory |
|---|---|---|
| Rails | | |
| Next.js | | |
| Gatsby | | |
| SvelteKit | | |
| Astro | | |
| Hugo | | |
| Jekyll | | Root directory (same as |
| Vite | | |
| Create React App | | |
| Vue CLI | | |
| Angular | | |
| Eleventy | | Check |
| Static HTML | | Same directory as |
Important: If existing favicon files are found (e.g., , ), use their location as the target directory regardless of framework detection.
favicon.icoapple-touch-icon.pngReport the detected project type and the static assets directory that will be used.
When in doubt, ask: If you are not 100% confident about where static assets should be placed (e.g., ambiguous project structure, multiple potential locations, unfamiliar framework), use to confirm the target directory before proceeding. It's better to ask than to put files in the wrong place.
AskUserQuestionTool检测项目类型,确定静态资源的存放位置。按以下顺序检查:
| 框架 | 检测方式 | 静态资源目录 |
|---|---|---|
| Rails | 存在 | |
| Next.js | 存在 | |
| Gatsby | 存在 | |
| SvelteKit | 存在 | |
| Astro | 存在 | |
| Hugo | 存在 | |
| Jekyll | 存在带有Jekyll标记的 | 根目录(与 |
| Vite | 存在 | |
| Create React App | | |
| Vue CLI | 存在 | |
| Angular | 存在 | |
| Eleventy | 存在 | 检查 |
| 静态HTML | 根目录下存在 | 与 |
重要提示:如果发现已存在的图标文件(如、),无论框架检测结果如何,都将其所在位置作为目标目录。
favicon.icoapple-touch-icon.png报告检测到的项目类型以及将使用的静态资源目录。
存疑时请询问:如果对静态资源的存放位置没有100%把握(例如项目结构模糊、存在多个潜在位置、不熟悉的框架),请使用确认目标目录后再继续操作。宁可询问也不要将文件放错位置。
AskUserQuestionToolStep 3: Determine App Name
步骤3:确定应用名称
Find the app name from these sources (in priority order):
- Existing - Check the detected static assets directory for an existing manifest and extract the
site.webmanifestfieldname - - Extract the
package.jsonfield if it existsname - Rails - Extract the module name (e.g.,
config/application.rb→ "MyApp")module MyApp - Directory name - Use the current working directory name as fallback
Convert the name to title case if needed (e.g., "my-app" → "My App").
按以下优先级顺序查找应用名称:
- 已存在的——在检测到的静态资源目录中查找已存在的manifest文件,提取
site.webmanifest字段name - ——如果存在,提取
package.json字段name - Rails的——提取模块名称(例如
config/application.rb→ "MyApp")module MyApp - 目录名称——将当前工作目录名称作为备选
必要时将名称转换为标题格式(例如"my-app" → "My App")。
Step 4: Ensure Static Assets Directory Exists
步骤4:确保静态资源目录存在
Check if the detected static assets directory exists. If not, create it.
检查检测到的静态资源目录是否存在。如果不存在,则创建该目录。
Step 5: Generate Favicon Files
步骤5:生成图标文件
Run these ImageMagick commands to generate all favicon files. Replace with the detected static assets directory from Step 2.
[STATIC_DIR]Important: The flag must come BEFORE the input file to properly preserve transparency when rendering SVGs. Placing it after the input will result in a white background.
-background none运行以下ImageMagick命令生成所有图标文件。将替换为步骤2中检测到的静态资源目录。
[STATIC_DIR]重要提示:参数必须放在输入文件之前,才能在渲染SVG时正确保留透明度。如果放在输入文件之后,会导致背景变为白色。
-background nonefavicon.ico (multi-resolution: 16x16, 32x32, 48x48)
favicon.ico(多分辨率:16x16、32x32、48x48)
bash
magick -background none "$1" \
\( -clone 0 -resize 16x16 \) \
\( -clone 0 -resize 32x32 \) \
\( -clone 0 -resize 48x48 \) \
-delete 0 -alpha on \
[STATIC_DIR]/favicon.icobash
magick -background none "$1" \
\( -clone 0 -resize 16x16 \) \
\( -clone 0 -resize 32x32 \) \
\( -clone 0 -resize 48x48 \) \
-delete 0 -alpha on \
[STATIC_DIR]/favicon.icofavicon-96x96.png
favicon-96x96.png
bash
magick -background none "$1" -resize 96x96 -alpha on [STATIC_DIR]/favicon-96x96.pngbash
magick -background none "$1" -resize 96x96 -alpha on [STATIC_DIR]/favicon-96x96.pngapple-touch-icon.png (180x180)
apple-touch-icon.png(180x180)
bash
magick -background none "$1" -resize 180x180 -alpha on [STATIC_DIR]/apple-touch-icon.pngbash
magick -background none "$1" -resize 180x180 -alpha on [STATIC_DIR]/apple-touch-icon.pngweb-app-manifest-192x192.png
web-app-manifest-192x192.png
bash
magick -background none "$1" -resize 192x192 -alpha on [STATIC_DIR]/web-app-manifest-192x192.pngbash
magick -background none "$1" -resize 192x192 -alpha on [STATIC_DIR]/web-app-manifest-192x192.pngweb-app-manifest-512x512.png
web-app-manifest-512x512.png
bash
magick -background none "$1" -resize 512x512 -alpha on [STATIC_DIR]/web-app-manifest-512x512.pngbash
magick -background none "$1" -resize 512x512 -alpha on [STATIC_DIR]/web-app-manifest-512x512.pngfavicon.svg (only if source is SVG)
favicon.svg(仅当源文件为SVG时)
If the source file has a extension, copy it:
.svgbash
cp "$1" [STATIC_DIR]/favicon.svg如果源文件的扩展名为,则复制该文件:
.svgbash
cp "$1" [STATIC_DIR]/favicon.svgStep 6: Create/Update site.webmanifest
步骤6:创建/更新site.webmanifest
Create or update with this content (substitute the detected app name):
[STATIC_DIR]/site.webmanifestjson
{
"name": "[APP_NAME]",
"short_name": "[APP_NAME]",
"icons": [
{
"src": "/web-app-manifest-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "/web-app-manifest-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
],
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
}If already exists in the static directory, preserve the existing , , and values while updating the , , and array.
site.webmanifesttheme_colorbackground_colordisplaynameshort_nameicons使用以下内容创建或更新(替换为检测到的应用名称):
[STATIC_DIR]/site.webmanifestjson
{
"name": "[APP_NAME]",
"short_name": "[APP_NAME]",
"icons": [
{
"src": "/web-app-manifest-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "/web-app-manifest-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
],
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
}如果静态资源目录中已存在,保留现有的、和值,同时更新、和数组。
site.webmanifesttheme_colorbackground_colordisplaynameshort_nameiconsStep 7: Update HTML/Layout Files
步骤7:更新HTML/布局文件
Based on the detected project type, update the appropriate file. Adjust the paths based on where the static assets directory is relative to the web root:
href- If static files are in or
public/and served from root → usestatic//favicon.ico - If static files are in → use
src/assets//assets/favicon.ico - If static files are in the same directory as HTML → use or just
./favicon.icofavicon.ico
根据检测到的项目类型,更新对应的文件。根据静态资源目录相对于网站根目录的位置调整路径:
href- 如果静态文件位于或
public/并从根目录提供服务 → 使用static//favicon.ico - 如果静态文件位于→ 使用
src/assets//assets/favicon.ico - 如果静态文件与HTML文件在同一目录 → 使用或直接使用
./favicon.icofavicon.ico
For Rails Projects
对于Rails项目
Edit . Find the section and add/replace favicon-related tags with:
app/views/layouts/application.html.erb<head>html
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-title" content="[APP_NAME]" />
<link rel="manifest" href="/site.webmanifest" />Important:
- If the source was NOT an SVG, omit the line
<link rel="icon" type="image/svg+xml" href="/favicon.svg" /> - Remove any existing ,
<link rel="icon",<link rel="shortcut icon", or<link rel="apple-touch-icon"tags before adding the new ones<link rel="manifest" - Place these tags near the top of the section, after
<head>and<meta charset>if present<meta name="viewport">
编辑。找到部分,添加/替换与图标相关的标签:
app/views/layouts/application.html.erb<head>html
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-title" content="[APP_NAME]" />
<link rel="manifest" href="/site.webmanifest" />重要提示:
- 如果源文件不是SVG格式,省略这一行
<link rel="icon" type="image/svg+xml" href="/favicon.svg" /> - 在添加新标签之前,删除所有已存在的、
<link rel="icon">、<link rel="shortcut icon">或<link rel="apple-touch-icon">标签<link rel="manifest"> - 将这些标签放在部分的顶部,位于
<head>和<meta charset>(如果存在)之后<meta name="viewport">
For Next.js Projects
对于Next.js项目
Edit the detected layout file ( or ). Update or add the export to include icons configuration:
app/layout.tsxsrc/app/layout.tsxmetadatatypescript
export const metadata: Metadata = {
// ... keep existing metadata fields
icons: {
icon: [
{ url: '/favicon.ico' },
{ url: '/favicon-96x96.png', sizes: '96x96', type: 'image/png' },
{ url: '/favicon.svg', type: 'image/svg+xml' },
],
shortcut: '/favicon.ico',
apple: '/apple-touch-icon.png',
},
manifest: '/site.webmanifest',
appleWebApp: {
title: '[APP_NAME]',
},
};Important:
- If the source was NOT an SVG, omit the entry from the icon array
{ url: '/favicon.svg', type: 'image/svg+xml' } - If metadata export doesn't exist, create it with just the icons-related fields
- If metadata export exists, merge the icons configuration with existing fields
编辑检测到的布局文件(或)。更新或添加导出,包含图标配置:
app/layout.tsxsrc/app/layout.tsxmetadatatypescript
export const metadata: Metadata = {
// ... 保留现有的metadata字段
icons: {
icon: [
{ url: '/favicon.ico' },
{ url: '/favicon-96x96.png', sizes: '96x96', type: 'image/png' },
{ url: '/favicon.svg', type: 'image/svg+xml' },
],
shortcut: '/favicon.ico',
apple: '/apple-touch-icon.png',
},
manifest: '/site.webmanifest',
appleWebApp: {
title: '[APP_NAME]',
},
};重要提示:
- 如果源文件不是SVG格式,从icon数组中省略这一项
{ url: '/favicon.svg', type: 'image/svg+xml' } - 如果不存在metadata导出,则仅创建包含图标相关字段的metadata
- 如果已存在metadata导出,将图标配置与现有字段合并
For Static HTML Projects
对于静态HTML项目
Edit the detected file. Add the same HTML as Rails within the section.
index.html<head>编辑检测到的文件。在部分添加与Rails项目相同的HTML标签。
index.html<head>If No Project Detected
如果未检测到项目
Skip HTML updates and inform the user they need to manually add the following to their HTML :
<head>html
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-title" content="[APP_NAME]" />
<link rel="manifest" href="/site.webmanifest" />跳过HTML更新步骤,并告知用户需要手动将以下内容添加到HTML的部分:
<head>html
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-title" content="[APP_NAME]" />
<link rel="manifest" href="/site.webmanifest" />Step 8: Summary
步骤8:总结
Report completion with:
- Detected project type and framework
- Static assets directory used
- List of files generated
- App name used in manifest and HTML
- Layout file updated (or note if manual update is needed)
- Note if any existing files were overwritten
操作完成后报告以下内容:
- 检测到的项目类型和框架
- 使用的静态资源目录
- 生成的文件列表
- 在manifest和HTML中使用的应用名称
- 更新的布局文件(或注明需要手动更新)
- 说明是否覆盖了任何现有文件
Error Handling
错误处理
- If ImageMagick is not installed, provide installation instructions and stop
- If the source image doesn't exist, report the exact path that was tried and stop
- If ImageMagick commands fail, report the specific error message
- If the layout file cannot be found for HTML updates, generate files anyway and instruct on manual HTML addition
- 如果未安装ImageMagick,提供安装说明并停止操作
- 如果源图片不存在,报告尝试的具体路径并停止操作
- 如果ImageMagick命令执行失败,报告具体的错误信息
- 如果找不到用于HTML更新的布局文件,仍生成图标文件,并指导用户手动添加HTML标签