Created
March 11, 2024 09:49
-
-
Save try-to-fly/c5aa23029d99750089bee04da3a138d7 to your computer and use it in GitHub Desktop.
使用sips创建png图片
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 使用sips命令生成PNG图片 | |
create_png_with_background() { | |
# 参数检查 | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: create_png_with_background <width> <height> <background_color>" | |
return 1 | |
fi | |
# 获取函数参数 | |
local width=$1 | |
local height=$2 | |
local bgcolor=$3 | |
# 创建JavaScript代码 | |
local js_code=$(cat <<EOF | |
const canvas = new Canvas($width, $height) | |
const ctx = canvas.getContext('2d') | |
// 设置背景颜色,支持rgba | |
ctx.fillStyle = '$bgcolor' | |
ctx.fillRect(0, 0, $width, $height) | |
const output = new Output(canvas, sips.outputPath) | |
output.addToQueue() | |
EOF | |
) | |
# 创建临时JS文件 | |
local tmp_js=$(mktemp /tmp/sips_script.XXXXXX.js) | |
echo "$js_code" > "$tmp_js" | |
# 使用sips命令生成PNG图片 | |
local output_png="output.png" | |
sips -j "$tmp_js" -o "$output_png" | |
# 清理临时文件 | |
rm "$tmp_js" | |
echo "PNG image created: $output_png" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment