如何从图像文件生成数据URI?


9

有一些在线工具,例如duri.me,可以从图像文件创建数据URI。是否有在Linux上本地运行的工具可以执行相同的操作?

Answers:


14

数据URI通常只是base64编码,并且MIME类型位于前端。我使用此本地脚本,该脚本将文件名作为第一个也是唯一的参数,并返回已经包装在其中的数据uri url('data:')

[0 1026 8:29:38] ~ % cat $(which cssify.sh)
#!/bin/sh
mimetype=$(file -bN --mime-type "$1")
content=$(base64 -w0 < "$1")
echo "url('data:$mimetype;base64,$content')"

我需要替换-w0-b0
感谢

在BSD(macOS)上,base64不带参数使用,因为默认情况下它不会破坏字符串。码: #!/usr/bin/env bash echo "url('data:$(file -bN --mime-type "$1");base64,$(base64 < "$1")')"
iolsmit,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.