【问题标题】:How can I create a favicon locally with a script?如何使用脚本在本地创建网站图标?
【发布时间】:2012-10-09 23:15:29
【问题描述】:
【问题讨论】:
标签:
php
imagemagick
gd
favicon
【解决方案1】:
您可以使用 imagemagik 转换。我从this website 获取了这些命令。
先制作主图:
convert some_image.bmp -resize 256x256 -transparent white
favicon-256.png
然后我们会为您想要包含的每个尺寸制作图像
.ico 文件。
convert favicon-256.png -resize 16x16 favicon-16.png
convert favicon-256.png -resize 32x32 favicon-32.png
convert favicon-256.png -resize 64x64 favicon-64.png
convert favicon-256.png -resize 128x128 favicon-128.png
现在您需要将它们捆绑到 .ico 文件中,我的技巧是
发现是把它设为256色,否则无法正常显示!
convert favicon-16.png favicon-32.png favicon-64.png favicon-128.png
favicon-256.png -colors 256 favicon.ico
【解决方案4】:
我为此创建了一个项目:faviconbuild。
我用两个简单的脚本构建了它(一个 .bat 用于 Windows,一个 .sh 用于 Unix、Mac 和 Windows,如果使用 Cygwin)。它依赖于ImageMagick,因此您可以为您的平台下载最新版本,或者从我包含在releases 中的版本中获取。
我已经在MIT license 下发布了它,所以您可以随意将它用于商业或个人项目,或者只是用作您自己脚本的灵感。
我目前在自己的项目中这样使用它:
./faviconbuild/faviconbuild.sh -i ./source.png
您可以通过-h 或--help 选项获取可用命令的完整列表。
这将获取输入源图像并生成所有输出图像和多分辨率 .ico,以及您的网站所需的 html 标记。
还可以通过提供基于自定义文本的解析文件作为输入来自定义脚本。当前文件可以在项目here中找到。这使得脚本可以轻松地扩展到所有平台上的任何项目。
由于它托管在github,因此我鼓励任何人为该项目做出贡献,即使只是提交功能请求。
我还发布了一个blog post,它涵盖了开发过程,并用作关于 bash/batch 的迷你教程。
【解决方案6】:
此脚本更新现有文件,您可能需要为您的项目更改它。删除“确认”行,或获取我的确认脚本以使用它。
警告:它将破坏当前目录中的任何.png 文件。
#!/bin/bash -eu
# public domain, by Sam Watkins
echo "You need to run this from a directory containing only the png icons."
confirm "Are you in the icons directory?"
source=`readlink -f "$1"`
chmod -w $source
v convert "$source" -resize 512x512 -background white -alpha remove -alpha off tmp-512-white.png
v convert "$source" -resize 512x512 tmp-512.png
sizes="16 32 64 128 256"
favicons=
for W in $sizes; do
F=favicon-${W}x$W.png
v convert tmp-512.png -resize ${W}x$W $F
favicons="$favicons $F"
done
v convert $favicons -colors 256 favicon.ico
for F in *.png; do
case "$F" in
favicon-*)
;;
apple-touch-icon*)
v convert tmp-512-white.png -resize `identify -format "%wx%h" "$F"` "$F"
;;
*)
v convert tmp-512.png -resize `identify -format "%wx%h" "$F"` "$F"
;;
esac
done
rm -f tmp-512-white.png tmp-512.png
https://ucm.dev/b/make-icons
https://ucm.dev/b/confirm